Mysql分析-profile詳解

一。前言

當我們要對某一條sql的性能進行分析時,可以使用它。

Profiling是從 mysql5.0.3版本以后才開放的。

啟動profile之后,所有查詢包括錯誤的語句都會記錄在內。

關閉會話或者set profiling=0 就關閉了。(如果將profiling_history_size參數設置為0,同樣具有關閉MySQL的profiling效果。)

此工具可用來查詢SQL執行狀態,System lock和Table lock 花多少時間等等,

對定位一條語句的I/O消耗CPU消耗 非常重要。(SQL?語句執行所消耗的最大兩部分資源就是IOCPU)

–在mysql5.7之后,profile信息將逐漸被廢棄,mysql推薦使用performance schema

二。流程

簡易流程大概如下:

set?profiling=1;??				//打開分析    run?your?sql1;    run?your?sql2;    show?profiles;					//查看sql1,sql2的語句分析    show?profile?for?query?1;		//查看sql1的具體分析    show?profile?ALL?for?query?1;	//查看sql1相關的所有分析【主要看i/o與cpu,下邊分析中有各項意義介紹】    set?profiling=0;??				//關閉分析


Mysql分析-profile詳解

Mysql分析-profile詳解

三。含義分析

Mysql分析-profile詳解

Mysql分析-profile詳解

上圖中橫向欄意義

+———————-+———-+———-+————+

“Status”: “query end”, 狀態

“Duration”: “1.751142”, 持續時間

“CPU_user”: “0.008999”, cpu用戶

“CPU_system”: “0.003999”, cpu系統

“Context_voluntary”: “98”, 上下文主動切換

“Context_involuntary”: “0”, 上下文被動切換

“Block_ops_in”: “8”, 阻塞的輸入操作

“Block_ops_out”: “32”, 阻塞的輸出操作

“Messages_sent”: “0”, 消息發出

“Messages_received”: “0”, 消息接受

“Page_faults_major”: “0”, 主分頁錯誤

“Page_faults_minor”: “0”, 次分頁錯誤

“Swaps”: “0”, 交換次數

“Source_function”: “mysql_execute_command”, 源功能

“Source_file”: “sql_parse.cc”, 源文件

“Source_line”: “4465” 源代碼行

+———————-+———-+———-+————+

上圖中縱向欄意義

+———————-+———-+———-+————+

starting:開始

checking permissions:檢查權限

Opening tables:打開表

init : 初始化

System lock :系統鎖

optimizing : 優化

statistics : 統計

preparing :準備

executing :執行

Sending data :發送數據

Sorting result :排序

end :結束

query end :查詢 結束

closing tables : 關閉表 /去除TMP 表

freeing items : 釋放物品

cleaning up :清理

+———————-+———-+———-+————+

一般情況下,常用以下語句也就夠了 :

mysql->SHOW?profile?CPU,BLOCK?IO?io?FOR?query?2;

?以上就是Mysql分析-profile詳解的內容,更多相關內容請關注PHP中文網(www.php.cn)!

? 版權聲明
THE END
喜歡就支持一下吧
點贊8 分享