經過腳本,刷新觀察mysql的status,觀察是否有周期性故障活波動,
通常由訪問高峯或者緩存失效引發,家緩存並更改緩存失效策略,是失效時間分散或頁面定時失,mysql
SHOW PROCESSLIST顯示哪些線程正在運行。
您也能夠使用mysqladmin processlist語句獲得此信息。若是您有SUPER權限,您能夠看到全部線程。不然,您只能看到您本身的線程
mysql 開啓慢查詢日誌sql
slow_query_log 這個參數設置爲ON,能夠捕獲執行時間超過必定數值的SQL語句。 long_query_time 當SQL語句執行時間超過此數值時,就會被記錄到日誌中,建議設置爲1或者更短 slow_query_log_file 記錄日誌的文件名。 log_queries_not_using_indexes 這個參數設置爲ON,能夠捕獲到全部未使用索引的SQL語句,儘管這個SQL語句有可能執行得挺快。
使用profiler來分析一條query的執行時間和性能瓶頸,
開啓 profiling ;緩存
set profiling=1;
隨便執行一條語句 select count(*) from user where id>2;服務器
show profiles;
獲得性能
+----------+------------+--------------------------------------+ | Query_ID | Duration | Query | +----------+------------+--------------------------------------+ | 2 | 0.00009200 | set profiling=1 | | 5 | 0.02003525 | select count(*) from user where id>2 | +----------+------------+--------------------------------------+
包含一個query_id和執行時間和query語句
經過query_id能夠查看到更詳細的信息;優化
show profile cpu ,block io for query 5;
+----------------------+----------+----------+------------+--------------+---------------+ | Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | +----------------------+----------+----------+------------+--------------+---------------+ | starting | 0.000170 | 0.000000 | 0.000000 | 0 | 0 | | checking permissions | 0.000019 | 0.000000 | 0.000000 | 0 | 0 | | Opening tables | 0.000033 | 0.000000 | 0.000000 | 0 | 0 | | init | 0.000061 | 0.000000 | 0.000000 | 0 | 0 | | System lock | 0.000021 | 0.000000 | 0.000000 | 0 | 0 | | optimizing | 0.000021 | 0.000000 | 0.000000 | 0 | 0 | | statistics | 0.010826 | 0.000000 | 0.000000 | 184 | 0 | | preparing | 0.000041 | 0.000000 | 0.000000 | 0 | 0 | | executing | 0.000005 | 0.000000 | 0.000000 | 0 | 0 | | Sending data | 0.008731 | 0.008000 | 0.000000 | 0 | 0 | | end | 0.000020 | 0.000000 | 0.000000 | 0 | 0 | | query end | 0.000018 | 0.000000 | 0.000000 | 0 | 0 | | closing tables | 0.000012 | 0.000000 | 0.000000 | 0 | 0 | | freeing items | 0.000032 | 0.000000 | 0.000000 | 0 | 0 | | cleaning up | 0.000027 | 0.000000 | 0.000000 | 0 | 0 | +----------------------+----------+----------+------------+--------------+---------------+
對mysql服務器的優化不要一上來就去優化sql語句,應該首先觀察全局狀況,至少要先搞清楚
問題出在哪,應該使用腳原本觀察服務器一段時間(一天或更長)的健康情況,好比cpu,io,進程鏈接數等
最後才分析具體緣由處在哪裏;針對解決;spa
經過 mysqladmin來查看mysql的狀態;線程
mysqladmin -P3306 -uroot -p123456 -h127.0.0.1 -r -i 1 ext |\ awk -F"|" \ "BEGIN{ count=0; }"\ '{ if($2 ~ /Variable_name/ && ((++count)%20 == 1)){\ print "----------|---------|--- MySQL Command Status --|----- Innodb row operation ----|-- Buffer Pool Read --";\ print "---Time---|---QPS---|select insert update delete| read inserted updated deleted| logical physical";\ }\ else if ($2 ~ /Queries/){queries=$3;}\ else if ($2 ~ /Com_select /){com_select=$3;}\ else if ($2 ~ /Com_insert /){com_insert=$3;}\ else if ($2 ~ /Com_update /){com_update=$3;}\ else if ($2 ~ /Com_delete /){com_delete=$3;}\ else if ($2 ~ /Innodb_rows_read/){innodb_rows_read=$3;}\ else if ($2 ~ /Innodb_rows_deleted/){innodb_rows_deleted=$3;}\ else if ($2 ~ /Innodb_rows_inserted/){innodb_rows_inserted=$3;}\ else if ($2 ~ /Innodb_rows_updated/){innodb_rows_updated=$3;}\ else if ($2 ~ /Innodb_buffer_pool_read_requests/){innodb_lor=$3;}\ else if ($2 ~ /Innodb_buffer_pool_reads/){innodb_phr=$3;}\ else if ($2 ~ /Uptime / && count >= 2){\ printf("|%6d %6d %6d %6d",com_select,com_insert,com_update,com_delete);\ printf("|%6d %8d %7d %7d",innodb_rows_read,innodb_rows_inserted,innodb_rows_updated,innodb_rows_deleted);\ printf("|%10d %11d\n",innodb_lor,innodb_phr);\ }}'
還能夠在 printf的輸出重定向到一個文件,這樣就能夠經過文件數據進行可視化分析;日誌