Mysql表优化,用于慢速运行查询

时间:2014-07-29 01:28:33

标签: mysql query-optimization

我正在尝试为客户端优化数据库,但我不确定什么是最佳的行动方案。 基本上cpu的使用率非常高,我想把它搞定。我在慢日志文件上运行了pt-query-digest,下面是联盟表:

- Rank Query ID           Response time  Calls R/Call V/M   Item
- ==== ================== ============== ===== ====== ===== ==============
-    1 0x4BBFEF1052D40140 342.6526 62.5%   360 0.9518 16.46 SELECT ip?nation ip?nationCountries
-    2 0x9F5A9B08988802BD  45.0751  8.2%   138 0.3266  0.72 SELECT ads
-    3 0xDB8A7E8604D61518  39.9419  7.3%   140 0.2853  0.89 UPDATE ads
-    4 0x6831D124742FAC56  15.3811  2.8%   103 0.1493  0.74 SELECT ads user user_ad_totals
-    5 0xC6A7A922146425E6  12.4285  2.3%     7 1.7755  0.64 SELECT ads user
-    6 0x4B63D9AE8F737190  10.7904  2.0%     7 1.5415  0.35 SELECT ads
-    7 0x56C2AE386137C062   9.7491  1.8%    55 0.1773  0.75 SELECT ads user user_ad_totals
-    8 0x8513FC4603E1A52B   8.2973  1.5%   148 0.0561  0.01 SELECT area
-    9 0x00E60C4A5C644B07   7.6636  1.4%     4 1.9159  0.41 SELECT ads user
-   10 0x875BC364BB26F45D   7.4405  1.4%     4 1.8601  0.41 SELECT ads
-   11 0xEFFCBB067FDD2875   7.0685  1.3%     2 3.5343  0.28 SELECT user_watchlist
-   12 0xFA337D172DFC2872   6.0098  1.1%    51 0.1178  1.08 SELECT ads ads_category_options_data
-   13 0x19B834B80B6F40BA   4.8357  0.9%    87 0.0556  0.99 SELECT ads
-   14 0xD45FFE2D07FF901C   2.6509  0.5%     1 2.6509  0.00 SELECT ads
-   15 0x15B47A97B613E072   1.9279  0.4%     2 0.9639  0.68 SELECT ads
- MISC 0xMISC              26.1904  4.8%  9575 0.0027   0.0 

我认为1号修正应该是我的主要优先事项。

以下是pt-query-digest的更多细节:

- Query 1: 1.90 QPS, 1.81x concurrency, ID 0x4BBFEF1052D40140 at byte 2940398
- This item is included in the report because it matches --limit.
- Scores: V/M = 16.46
- Time range: 2014-07-29 00:22:39 to 00:25:48
- Attribute    pct   total     min     max     avg     95%  stddev  median
- ============ === ======= ======= ======= ======= ======= ======= =======
- Count          3     360
- Exec time     62    343s    24us     31s   952ms      6s      4s    44us
- Lock time      0    57ms       0    50ms   158us   103us     3ms       0
- Rows sent      0      34       0       1    0.09    0.99    0.29       0
- Rows examine  30   4.00M       0 236.40k  11.39k 112.33k  40.50k       0
- Query size     2 105.42k     297     302  299.87  299.03    2.50  299.03
- String:
- Databases    bahtsold
- Hosts
- Users        bahtsold_dbuser
- Query_time distribution
-   1us
-  10us  ----------------------------------------------------------------
- 100us  -
-   1ms  -
-  10ms
- 100ms  -
-    1s  --
-  10s+  --
- Tables
-    SHOW TABLE STATUS FROM `bahtsold` LIKE 'ip2nation'\G
-    SHOW CREATE TABLE `bahtsold`.`ip2nation`\G
-    SHOW TABLE STATUS FROM `bahtsold` LIKE 'ip2nationCountries'\G
-    SHOW CREATE TABLE `bahtsold`.`ip2nationCountries`\G
EXPLAIN /*!50100 PARTITIONS*/
SELECT ip2nation.country, ip2nationCountries.code, ip2nationCountries.iso_code_2, ip2nationCountries.country as country_name 
FROM ip2nation 
LEFT JOIN ip2nationCountries 
       ON ip2nation.country = ip2nationCountries.code 
WHERE ip2nation.ip < INET_ATON('220.181.108.151') 
ORDER BY ip2nation.ip DESC LIMIT 0,1\G

这是解释声明的结果:

-*************************** 1. row *************************** 
-           id: 1
-  select_type: SIMPLE
-        table: ip2nation
-   partitions: NULL
-         type: range
-possible_keys: ip
-          key: ip
-      key_len: 4
-          ref: NULL
-         rows: 120994
-        Extra: Using index condition; Using temporary; Using filesort
-*************************** 2. row ***************************
-           id: 1
-  select_type: SIMPLE
-        table: ip2nationCountries
-   partitions: NULL
-         type: ALL
-possible_keys: NULL
-          key: NULL
-      key_len: NULL
-          ref: NULL
-         rows: 246
-        Extra: Using where; Using join buffer (Block Nested Loop)

优化此功能的最佳方法是什么?

0 个答案:

没有答案