天天看點

mysql查詢診斷分析工具

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查詢診斷分析工具