[筆記] mysql優化前的診斷

不能盲目的作優化,要先找準問題所在,是cpu,內存,io?仍是峯值,語句?mysql

1、mysql優化套路

2、相關命令

觀察服務器狀態

mysql> show status; 或 mysqladmin exts;sql

觀察鏈接的工做狀態

mysql> show processlist;服務器

狀態說明:mysql優化

  • converting HRAP to MyIsAM 把查詢結果放在磁盤(取得數據太多)工具

  • create tmp table 建立臨時表(索引須要優化)性能

  • copying to tmp table to disk 把內存臨時表複製到磁盤(索引須要優化,索引字段)測試

  • locked 被其餘查詢鎖住(事務)優化

  • logging slow query 記錄慢查詢spa

觀察具體語句執行

準備:日誌

  1. 檢查是否開啓profiling

show variables like 'profiling';

  1. 開啓profiling

set profiling=on;

show profiles;    //顯示執行語句的執行信息(含語句query_id)
show profile for query 1; //獲取 query_id 爲1的語句的執行信息
explain (sql語句) \G;  //解析sql執行計劃

3、慢查詢日誌

mysql> show variables like 'show_query%';  //能夠看到是否開啓,和日誌路徑 
mysql> show variables like '%long_query%'; //慢查詢臨界值,超過該值則記入慢查詢

//開啓
mysql> set global slow_query_log=1;

4、awk腳本 簡便的文本處理工具

mysqladmin -uroot ext|awk 'Queries/{q=$4}/Threads_connected/{c=$4}/Threads_running/{r=$4}END{printf("%d %d %d\n",q,c,r)}'

5、sysbench 測試CPU性能

待續……

相關文章
相關標籤/搜索