繼上一篇
mysqldumpslow工具的講解,今天來聊聊show profile。也是MySQL服務自帶的分析調優工具,不過這款更高級,比較接近底層硬件參數的調優。
![慢查詢分析調優工具~show profile 慢查詢分析調優工具~show profile](http://static.javashuo.com/static/loading.gif)
查看show profile設置
show variables like 'profiling%';//默認關閉,保存近15次的運行結果
![慢查詢分析調優工具~show profile 慢查詢分析調優工具~show profile](http://static.javashuo.com/static/loading.gif)
開啓
set profiling = on;
![慢查詢分析調優工具~show profile 慢查詢分析調優工具~show profile](http://static.javashuo.com/static/loading.gif)
查看最近15次的運行結果
show profiles;
備註:
show warnings;//能夠顯示警告和報錯的信息
![慢查詢分析調優工具~show profile 慢查詢分析調優工具~show profile](http://static.javashuo.com/static/loading.gif)
診斷運行的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 慢查詢分析調優工具~show profile](http://static.javashuo.com/static/loading.gif)
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 慢查詢分析調優工具~show profile](http://static.javashuo.com/static/loading.gif)
全局查詢日誌(第二種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 慢查詢分析調優工具~show profile](http://static.javashuo.com/static/loading.gif)