Mysql使用profile分析sql開銷的代碼

本篇文章給大家帶來的內容是關于mysql使用profile分析sql開銷的代碼,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

Mysql使用profile分析sql開銷

1.使用之前先查看當前數據庫的版本信息,低版本無法使用.     show version();  或者 show variables like '%version%' 2.查看profiling     show variables like '%profil%'    ;      result:         +------------------------+-------+           | Variable_name          | Value |           +------------------------+-------+           | have_profiling         | YES   |   --只讀變量,用于控制是否由系統變量開啟或禁用profiling           | profiling              | OFF   |   --開啟SQL語句剖析功能           | profiling_history_size | 15    |   --設置保留profiling的數目,缺省為15,范圍為0至100,為0時將禁用p      show profiles; 查看是否開啟,效果同上. 3.查看使用說明      help profile; 4.開啟profile     set profiling=1; 賦值時候不要有多余的空格. 5.運行sql,查看對應的profile     select * from test ;     show profiles;                  result:     +----------+------------+--------------------------------------------------------------------------------------------------------------------------+     | Query_ID | Duration   | Query                                                                                                                    |     +----------+------------+--------------------------------------------------------------------------------------------------------------------------+     |       28 | 0.00033575 | select * from test                                                                                        |     +----------+------------+--------------------------------------------------------------------------------------------------------------------------+      分析sql性能,分析的時候可以加上對應的開銷字段     show profile [cpu,io][all] for query 28 ;      show profile for query 28 ;      +----------------------+----------+     | Status               | Duration |     +----------------------+----------+     | starting             | 5.7E-5   |     | checking permissions | 7E-6     |     | Opening tables       | 1.7E-5   |     | init                 | 2.3E-5   |     | System lock          | 8E-6     |     | optimizing           | 5E-6     |     | statistics           | 1.1E-5   |     | preparing            | 9E-6     |     | executing            | 3E-6     |     | Sending data         | 8.8E-5   |     | end                  | 5E-6     |     | query end            | 6E-6     |     | closing tables       | 5E-6     |     | freeing items        | 7.8E-5   |     | cleaning up          | 1.5E-5   |     +----------------------+----------+ 6.關閉     set profiling=off;

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