Query Profiler是MYSQL自帶的一種query診斷分析工具,經過它能夠分析出一條SQL語句的性能瓶頸在什麼地方。一般咱們是使用的explain,以及slow query log都沒法作到精確分析,可是Query Profiler卻能夠定位出一條SQL語句執行的各類資源消耗狀況,好比CPU,IO等,以及該SQL執行所耗費的時間等。不過該工具只有在MYSQL 5.0.37以及以上版本中才有實現。
默認的狀況下,MYSQL的該功能沒有打開,須要本身手動啓動。能夠經過以下方法查看當前mysql服務器是否開啓了該功能。
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| profiling | OFF |
| profiling_history_size | 15 |
+------------------------+-------+
2 rows in set (0.03 sec)
profiling參數值爲OFF,說明沒有打開該功能。
profiling_history_size參數值爲15表示,記錄最近15次的查詢歷史。該值能夠修改。
下邊說說如何打開profiling功能:
MYSQL提示符下執行以下命令:
mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)
而後再次檢驗下執行的效果:
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| profiling | ON |
| profiling_history_size | 15 |
+------------------------+-------+
2 rows in set (0.00 sec)
profiling值爲ON說明已經啓動該功能。
下邊說說如何使用show profiles;
1.首先執行一查詢語句:
mysql> select * from user;
+------+-----------+
| id | name |
+------+-----------+
| 22 | abc |
| 223 | dabc |
| 2232 | dddabc |
| 45 | asdsagd |
| 23 | ddddddddd |
| 22 | ddd |
| 28 | sssddd |
+------+-----------+
7 rows in set (0.06 sec)
2.經過show profiles命令查看系統中多個query的概要信息:
mysql> show profiles;
+----------+------------+-----------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------+
| 1 | 0.00013600 | set profiling=1 |
| 2 | 0.00092300 | show variables like '%profiling%' |
| 3 | 0.08506075 | show databases |
| 4 | 0.02698550 | SELECT DATABASE() |
| 5 | 0.07408475 | show tables |
| 6 | 0.05769725 | select * from user |
+----------+------------+-----------------------------------+
6 rows in set (0.01 sec)
其中Query_ID表示查詢ID,也就是個編號,Duration表示對應的query語句執行的時間,單位是秒,query表示具體的query語句。咱們能夠看到剛纔咱們最後執行的select * from user語句執行的時間是0.05769725,單位是秒,也就是57ms
3.獲取單個query的詳細profile信息,能夠經過以下語句:
mysql> show profile cpu,block io for query 6;
+--------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+--------------------+----------+----------+------------+--------------+---------------+
| starting | 0.014438 | NULL | NULL | NULL | NULL |
| Opening tables | 0.042860 | NULL | NULL | NULL | NULL |
| System lock | 0.000007 | NULL | NULL | NULL | NULL |
| Table lock | 0.000012 | NULL | NULL | NULL | NULL |
| init | 0.000019 | NULL | NULL | NULL | NULL |
| optimizing | 0.000005 | NULL | NULL | NULL | NULL |
| statistics | 0.000018 | NULL | NULL | NULL | NULL |
| preparing | 0.000011 | NULL | NULL | NULL | NULL |
| executing | 0.000004 | NULL | NULL | NULL | NULL |
| Sending data | 0.000232 | NULL | NULL | NULL | NULL |
| end | 0.000007 | NULL | NULL | NULL | NULL |
| query end | 0.000005 | NULL | NULL | NULL | NULL |
| freeing items | 0.000073 | NULL | NULL | NULL | NULL |
| logging slow query | 0.000003 | NULL | NULL | NULL | NULL |
| cleaning up | 0.000004 | NULL | NULL | NULL | NULL |
+--------------------+----------+----------+------------+--------------+---------------+
15 rows in set (0.02 sec)
總結:Query Profiler對於SQL性能分析和診斷費用有用。另外,該命令還有以下參數能夠選擇:
- ALL - displays all information
- BLOCK IO - displays counts for block input and output operations
- CONTEXT SWITCHES - displays counts for voluntary and involuntary context switches
- IPC - displays counts for messages sent and received
- MEMORY - is not currently implemented
- PAGE FAULTS - displays counts for major and minor page faults
- SOURCE - displays the names of functions from the source code, together with the name and line number of the file in which the function occurs
- SWAPS - displays swap counts
使用SQLyog等工具能夠直接看到Profiler頁面,輸入 Show profiles;語句後會顯示更爲詳細的信息。 mysql