繼上一篇 mysqldumpslow 工具的講解,今天來聊聊show profile。也是MySQL服務自帶的分析調優工具,不過這款更進階,比較接近底層硬體參數的調優。
慢查詢分析調優工具~show profile 檢視show profile設定
show variables like 'profiling%';//預設關閉,儲存近15次的運作結果
慢查詢分析調優工具~show profile 開啟
set profiling = on;
慢查詢分析調優工具~show profile 檢視最近15次的運作結果
show profiles;
備注:
show warnings;//可以顯示警告和報錯的資訊
慢查詢分析調優工具~show profile 診斷運作的SQL
指令:show profile cpu,block io for query query_id;
例子:
show profile cpu,block io for query 3;
通過Status一列,可以看到整條SQL的運作過程
1. starting //開始
2. checking permissions //檢查權限
3. Opening tables //打開資料表
4. init //初始化
5. System lock //鎖機制
6. optimizing //優化器
7. statistics //分析文法樹
8. prepareing //預準備
9. executing //引擎執行開始
10. end //引擎執行結束
11. query end //查詢結束
......
12. closing tables //釋放資料表
13. freeing items //釋放記憶體
14. cleaning up //徹底清理
慢查詢分析調優工具~show profile Type:
ALL //顯示索引的開銷資訊
BLOCK IO //顯示塊IO相關開銷
CONTEXT SWITCHES //上下文切換相關開銷
CPU //顯示CPU相關開銷資訊
IPC //顯示發送和接收相關開銷資訊
MEMORY //顯示記憶體相關開銷資訊
PAGE FAULTS //顯示頁面錯誤相關開銷資訊
SOURCE //顯示和source_function,source_file,source_line相關的開銷資訊
SWAPS //顯示交換次數相關開銷的資訊
如出現以下一種或者幾種情況,說明SQL執行性能極其低下,亟需優化
* converting HEAP to MyISAM //查詢結果太大,記憶體都不夠用了往磁盤上搬了
* Creating tmp table //建立臨時表:拷貝資料到臨時表,用完再删
* Copying to tmp table on disk //把記憶體中臨時表複制到磁盤,危險
* locked //出現死鎖
通過查詢資料表來診斷SQL(第二種查詢方式)
select * from information_schema.profiling;
慢查詢分析調優工具~show profile 全局查詢日志(第二種SQL診斷方式)
此方式診斷較簡單(參數少,适合定位有問題的SQL),記錄到資料庫(建議隻在測試庫環境進行)
設定
方式1:指令行
1. set global general_log = 1;
2. set global log_output = 'TABLE';
方式2:配置檔案
* vim my.cnf
general_log =1
general_log_file = /path/logfile
log_output = FILE
* 重新開機MySQL服務
診斷SQL
select * from mysql.general_log;
慢查詢分析調優工具~show profile