MySQL佔用CPU及記憶體高解決案例

來源:互聯網
上載者:User

MySQL佔用CPU及記憶體高解決案例

故障:
      晚上大概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終於降下來了。。。
不過要徹底解決問題,仍需要最佳化語句,建立索引哪。。不然資料多了,肯定還會出問題的。
好了,此次故障也僅僅給提供一些解決問題的思路。具體問題還是需要具體分析的。

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.