Home  >  Article  >  Database  >  MySQL占用CPU及内存高解决案例

MySQL占用CPU及内存高解决案例

WBOY
WBOYOriginal
2016-06-07 15:55:071039browse

晚上大概7点钟左右,收到播放中心投诉,说视频播放很慢,加载很久不出来。一开始,哥以为是tomcat服务又挂了。所以到tomcat服务器

故障:
      晚上大概7点钟左右,收到播放中心投诉,说视频播放很慢,加载很久不出来。一开始,哥以为是tomcat服务又挂了。所以到tomcat服务器上查看下catalina.out输出日志。却没发现任务错误信息。

分析:
      想了想,视频加载慢,会不会是数据库问题呢?果断上mysql数据库(从库)看下top如下:
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND 
 37258 mysql    20  0 17.2g  12g 5032 S 769.5 81.3  4383:29 mysqld

没想到cpu居然达到769%了!
    然后进入mysql的慢查询语句的目录下面,看下slow.log
select count(*) as col_0_0_ from card_received cardreceiv0_ where (cardreceiv0_.statusCode='1' or cardreceiv0_.statusCode='2') and (cardreceiv0_.ownerCardNum='8757003738566209' or cardreceiv0_.ownerPhoneNum='13724689717') and cardreceiv0_.readStatus=0\G;

发现这条查询语句耗时5秒左右,但是slow.log里面全部是这条语句。所以我觉得很可疑。
再用explain分析下看
mysql> explain select count(*) as col_0_0_ from card_received cardreceiv0_ where (cardreceiv0_.statusCode='1' or cardreceiv0_.statusCode='2') and (cardreceiv0_.ownerCardNum='8757003738566209' or cardreceiv0_.ownerPhoneNum='13724689717') and cardreceiv0_.readStatus=0\G;
 
 
explain结果:
************************** 1. row ***************************
          id: 1
          select_type: SIMPLE
          table: cardreceiv0_
          type: ref
possible_keys: readStatus,ownerCardNum,statusCode,ownerPhoneNum
          key: readStatus
          key_len: 5
          ref: const
          rows: 2394190
          Extra: Using where
1 row in set (0.00 sec)
 
ERROR: 
No query specified

居然扫描了:2394190行。。。。
  最后跟开发沟通后,因为这边表数据并不重要。故将一些旧数据情况了,表示已经清空了200万行!最后mysql的cpu终于降下来了。。。
不过要彻底解决问题,仍需要优化语句,,建立索引哪。。不然数据多了,肯定还会出问题的。
好了,此次故障也仅仅给提供一些解决问题的思路。具体问题还是需要具体分析的。

本文永久更新链接地址

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn