慢查詢分析調優工具~show profile

zhangdeTalk發表於2020-01-02

繼上一篇mysqldumpslow工具的講解,今天來聊聊show profile。也是MySQL服務自帶的分析調優工具,不過這款更高階,比較接近底層硬體引數的調優。

檢視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

本作品採用《CC 協議》,轉載必須註明作者和本文連結

阿德

相關文章