免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: zcongwei

急求,mysql 经常导致CPU使用率100%  关闭 [复制链接]

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
发表于 2008-07-27 12:44 |显示全部楼层
原帖由 zcongwei 于 2008-7-27 12:02 发表



哎,就不知道怎么去优化这些

所以要请教一下各位大虾呢

谢谢

用explain看下。

贴下结果。

论坛徽章:
0
发表于 2008-07-27 13:04 |显示全部楼层
原帖由 枫影谁用了 于 2008-7-27 12:44 发表

用explain看下。

贴下结果。


好,我运行了那几句,结果如下

mysql> explain SELECT attach.aid, attach.attachment, t.tid, t.fid, t.subject FROM cdb_attachments attach LEFT JOIN cdb_threads t ON t.tid=attach.tid WHERE attach.readperm='0' AND displayorder>='0' AND filetype like 'image/%g%'  GROUP BY attach.tid ORDER BY attach.dateline DESC LIMIT 0, 50;
+----+-------------+--------+--------+---------------+---------+---------+-----------------------+-------+----------------------------------------------+
| id | select_type | table  | type   | possible_keys | key     | key_len | ref                   | rows  | Extra                                        |
+----+-------------+--------+--------+---------------+---------+---------+-----------------------+-------+----------------------------------------------+
|  1 | SIMPLE      | attach | index  | tid           | tid     | 3       | NULL                  | 28650 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | t      | eq_ref | PRIMARY       | PRIMARY | 3       | szxndiscuz.attach.tid |     1 | Using where                                  |
+----+-------------+--------+--------+---------------+---------+---------+-----------------------+-------+----------------------------------------------+
2 rows in set (0.05 sec)

mysql> explain SELECT t.*, f.name FROM cdb_threads t, cdb_forums f WHERE f.fid=t.fid AND f.fid not in (41) AND t.displayorder not in (-1,-2) AND t.closed NOT LIKE 'moved|%' AND t.replies !=0 ORDER BY t.lastpost DESC LIMIT 0, 10;
+----+-------------+-------+--------+---------------------+--------------+---------+------------------+-------+-----------------------------+
| id | select_type | table | type   | possible_keys       | key          | key_len | ref              | rows  | Extra                       |
+----+-------------+-------+--------+---------------------+--------------+---------+------------------+-------+-----------------------------+
|  1 | SIMPLE      | t     | range  | displayorder,typeid | displayorder | 2       | NULL             | 30457 | Using where; Using filesort |
|  1 | SIMPLE      | f     | eq_ref | PRIMARY             | PRIMARY      | 2       | szxndiscuz.t.fid |     1 |                             |
+----+-------------+-------+--------+---------------------+--------------+---------+------------------+-------+-----------------------------+
2 rows in set (0.00 sec)


mysql> explain SELECT t.tid, t.readperm, t.price, t.author, t.dateline, t.subject, p.message, p.status
    ->   FROM cdb_threads t
    ->   LEFT JOIN cdb_posts p ON p.tid=t.tid AND p.first='1'
    ->   WHERE t.fid='45' AND t.displayorder>='0'
    ->   ORDER BY t.dateline DESC LIMIT 20;
+----+-------------+-------+-------+---------------------+--------------+---------+------------------------+------+-----------------------------+
| id | select_type | table | type  | possible_keys       | key          | key_len | ref                    | rows | Extra                       |
+----+-------------+-------+-------+---------------------+--------------+---------+------------------------+------+-----------------------------+
|  1 | SIMPLE      | t     | range | displayorder,typeid | displayorder | 3       | NULL                   |  680 | Using where; Using filesort |
|  1 | SIMPLE      | p     | ref   | displayorder,first  | first        | 4       | szxndiscuz.t.tid,const |   46 |                             |
+----+-------------+-------+-------+---------------------+--------------+---------+------------------------+------+-----------------------------+
2 rows in set (0.03 sec)

论坛徽章:
0
发表于 2008-07-27 13:06 |显示全部楼层
不能对得很齐

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
发表于 2008-07-27 13:22 |显示全部楼层
原帖由 zcongwei 于 2008-7-27 13:06 发表
不能对得很齐



explain sql语句  \G;

后面加上\G

论坛徽章:
0
发表于 2008-07-27 13:28 |显示全部楼层
原帖由 枫影谁用了 于 2008-7-27 13:22 发表



explain sql语句  \G;

后面加上\G



列出是这样子, 你看一下:

mysql> explain SELECT t.tid, t.readperm, t.price, t.author, t.dateline, t.subject, p.message, p.status   FROM cdb_threads t   LEFT JOIN cdb_posts p ON p.tid=t.tid AND p.first='1'   WHERE t.fid='45' AND t.displayorder>='0'   ORDER BY t.dateline DESC LIMIT 20 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t
         type: range
possible_keys: displayorder,typeid
          key: displayorder
      key_len: 3
          ref: NULL
         rows: 680
        Extra: Using where; Using filesort
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: p
         type: ref
possible_keys: displayorder,first
          key: first
      key_len: 4
          ref: szxndiscuz.t.tid,const
         rows: 46
        Extra:
2 rows in set (0.00 sec)

mysql> explain SELECT t.*, f.name FROM cdb_threads t, cdb_forums f WHERE f.fid=t.fid AND f.fid not in (41) AND t.displayorder not in (-1,-2) AND t.closed NOT LIKE 'moved|%' AND t.replies !=0 ORDER BY t.lastpost DESC LIMIT 0, 10 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t
         type: range
possible_keys: displayorder,typeid
          key: typeid
      key_len: 2
          ref: NULL
         rows: 30458
        Extra: Using where; Using filesort
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: f
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 2
          ref: szxndiscuz.t.fid
         rows: 1
        Extra:
2 rows in set (0.00 sec)

mysql> explain SELECT t.tid, t.readperm, t.price, t.author, t.dateline, t.subject, p.message, p.status
    ->   FROM cdb_threads t
    ->   LEFT JOIN cdb_posts p ON p.tid=t.tid AND p.first='1'
    ->   WHERE t.fid='13' AND t.displayorder>='0'
    ->   ORDER BY t.dateline DESC LIMIT 20 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t
         type: range
possible_keys: displayorder,typeid
          key: displayorder
      key_len: 3
          ref: NULL
         rows: 2078
        Extra: Using where; Using filesort
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: p
         type: ref
possible_keys: displayorder,first
          key: first
      key_len: 4
          ref: szxndiscuz.t.tid,const
         rows: 46
        Extra:
2 rows in set (0.02 sec)

mysql> explain SELECT attach.aid, attach.attachment, t.tid, t.fid, t.subject FROM cdb_attachments attach LEFT JOIN cdb_threads t ON t.tid=attach.tid WHERE attach.readperm='0' AND displayorder>='0' AND filetype like 'image/%g%'  GROUP BY attach.tid ORDER BY attach.dateline DESC LIMIT 0, 50 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: attach
         type: index
possible_keys: tid
          key: tid
      key_len: 3
          ref: NULL
         rows: 28658
        Extra: Using where; Using temporary; Using filesort
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: t
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: szxndiscuz.attach.tid
         rows: 1
        Extra: Using where
2 rows in set (0.09 sec)

[ 本帖最后由 zcongwei 于 2008-7-27 13:32 编辑 ]

论坛徽章:
0
发表于 2008-07-27 13:30 |显示全部楼层
还有这个
mysql> explain SELECT p.*, m.uid, m.username, m.groupid, m.adminid, m.regdate, m.lastactivity, m.posts, m.digestposts, m.oltime,
    ->                 m.pageviews, m.credits, m.extcredits1, m.extcredits2, m.extcredits3, m.extcredits4, m.extcredits5, m.extcredits6,
    ->                 m.extcredits7, m.extcredits8, m.email, m.gender, m.showemail, m.invisible, mf.nickname, mf.site,
    ->                 mf.icq, mf.qq, mf.yahoo, mf.msn, mf.taobao, mf.alipay, mf.location, mf.fromcity, mf.fromtown, mf.nowcity, mf.nowtown, mf.medals,
    ->                 mf.sightml AS signature, mf.customstatus, mf.spacename
    ->                 FROM cdb_posts p
    ->                 LEFT JOIN cdb_members m ON m.uid=p.authorid
    ->                 LEFT JOIN cdb_memberfields mf ON mf.uid=m.uid
    ->                 WHERE p.tid='21220' AND p.invisible='0'   ORDER BY dateline LIMIT 10, 10 \G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: p
         type: ref
possible_keys: invisible,displayorder,first
          key: displayorder
      key_len: 4
          ref: const,const
         rows: 38
        Extra: Using where
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: m
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: szxndiscuz.p.authorid
         rows: 1
        Extra:
*************************** 3. row ***************************
           id: 1
  select_type: SIMPLE
        table: mf
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: szxndiscuz.m.uid
         rows: 1
        Extra:
3 rows in set (0.00 sec)

ERROR:
No query specified

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
发表于 2008-07-27 13:52 |显示全部楼层
mysql> explain SELECT attach.aid, attach.attachment, t.tid, t.fid, t.subject FROM cdb_attachments attach LEFT JOIN cdb_threads t ON t.tid=attach.tid WHERE attach.readperm='0' AND displayorder>='0' AND filetype like 'image/%g%'  GROUP BY attach.tid ORDER BY attach.dateline DESC LIMIT 0, 50 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: attach
         type: index
possible_keys: tid
          key: tid
      key_len: 3
          ref: NULL
         rows: 28658
        Extra: Using where; Using temporary; Using filesort

像这个优化比较高难度了,看来还不会太差啊

是优化sql语句

like
in
group by
order by

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
发表于 2008-07-27 13:52 |显示全部楼层
explain 结果字段对应的解释

http://blog.chinaunix.net/u/9817/showart_1008236.html

论坛徽章:
0
发表于 2008-07-27 13:54 |显示全部楼层
原帖由 枫影谁用了 于 2008-7-27 13:52 发表
explain 结果字段对应的解释

http://blog.chinaunix.net/u/9817/showart_1008236.html



好的,非常感谢~

论坛徽章:
0
发表于 2008-07-27 13:56 |显示全部楼层
有一个问题
我发现慢的时候主要是 等待 I/O 太高

[root@localhost log]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
0  1 119092 763724   1900  26528   14   14    34    32   46    29  3  2 82 13
1  1 119092 762712   1924  26488   76    0   712     0 1577   391 16  6 34 44
0  0 119092 762776   1944  26728    0    0   256   188 1689   134  0  2 90  8
0  0 119092 762520   1956  26976    0    0   320     0 1440    84  1  1 94  5
0  0 119092 762136   1968  27224    0    0   360     0 1460   108  0  1 93  6
0  0 119092 761944   1984  27728    0    0   276     0 1471    90  0  1 94  5
0  0 119092 761736   2004  27968    0    0   340     0 1412    93  0  1 93  7
0  3 119092 765448   2024  28124   84    0   392    16 1449   231 10  2 65 23
0  1 119092 767932   2044  28860   24    0   648   156 1645   376 11  3 30 55
0  0 119084 771676   2060  29108    0    0   360     0 1551   168  2  0 83 16
0  0 118700 775140   2084  29740   32    0   308     0 1407   104  0  1 89 10
0  0 118628 778672   2096  29800    0    0   224     0 1322    70  0  1 95  5
1  1 118628 778912   2104  29792    0    0   176    20 1382   144  3  1 91  6
1  1 118628 780784   2116  30108  192    0   504   584 1563   180  9  5 67 19
0  0 118628 780432   2140  30388  216    0   576     0 1468   230  5  3 59 33
0  0 118628 780336   2144  30644    0    0   108     0 1284   146  2  1 95  2
0  0 118628 779620   2164  30884    0    0   328     0 1507   248  5  1 84 10
0  0 118628 779516   2180  31128    0    0   260     0 1250   140  2  0 94  4
1  0 118628 779548   2192  31376    0    0   184   144 1339    80  2  2 93  4
0  1 118576 778588   2204  32132   64    0   920     0 1622   604 21  6 33 40
0  1 118576 777820   2216  33160    0    0   960     0 1593   497  3  2 46 49
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
1  1 118576 776656   2224  34452    0    0  1160     0 1482   514  1  2 51 47
0  2 118576 775672   2228  35228    0    0  1080     0 1472   524  1  1 49 49
0  0 118576 775160   2244  35732    0    0   500   156 1440   188  2  2 67 29
0  0 118336 778700   2248  36228    0    0   244     0 1440    93  0  1 97  2
0  0 118336 778104   2276  36316  144    0   504     0 1442   185  1  1 79 19
0  0 118336 775456   2280  35264   68    0   284     0 1435   280 13  3 46 38
0  0 118336 775504   2288  35256    0    0   332     0 1471   108  1  0 93  6
0  0 118336 775416   2304  35500    0    0   216   148 1361    92  0  2 94  5
0  2 118336 772764   2312  34860    0    0   372     0 1488   203  6  1 70 24
0  0 118336 773100   2316  34336    0    0   204     0 1463   129  0  1 79 20
1  2 118336 768120   2320  33556    0    0   264     0 1406   158  8  1 71 20
0  0 118336 767488   2324  31928   64    0   324     0 1282   252  6  3 39 52
0  0 118336 770924   2340  31912    0    0   220   140 1308    82  0  2 94  5
0  1 118284 770896   2344  31908   52    0   268     0 1377   108  0  1 93  7
0  0 118284 770896   2348  32164    0    0   284     0 1408    92  1  1 94  4
0  0 118284 774252   2360  32412    0    0   288     0 1357    79  0  0 96  4
1  0 118284 771920   2272  32208   64   28   492    28 1426   267 13  3 55 29
0  0 118284 771728   2292  32448    0    0   328   140 1429   179  4  2 89  5
0  0 118284 771744   2300  32440    0    0   276     0 1327    73  1  0 96  4
0  0 118284 771808   2304  32696    0    0   196     0 1343    70  0  0 97  3


你可以看一下,wa很高

另外,从top看也是CPU 中wa占用比例很高

不知道是为什么呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP