要想優化一條 Query,我們就需要清楚的知道這條 Query 的性能瓶頸到底在哪里,是消耗的 CPU計算太多,還是需要的的 IO 操作太多?要想能夠清楚的了解這些信息,在 MySQL 5.0 和 MySQL 5.1正式版中已經可以非常容易做到了,那就是通過 Query Profiler 功能。
MySQL 的 Query Profiler 是一個使用非常方便的 Query 診斷分析工具,通過該工具可以獲取一條Query 在整個執行過程中多種資源的消耗情況,如 CPU,IO,IPC,SWAP 等,以及發生的 PAGE FAULTS,CONTEXT SWITCHE 等等,同時還能得到該 Query 執行過程中 MySQL 所調用的各個函數在源文件中的位置。
下面我們看看 Query Profiler 的具體用法。
1、 開啟 profiling 參數
mysql> set profiling=1;
Query OK, 0 rows affected
通過執行 “set profiling”命令,可以開啟關閉 Query Profiler 功能。
2、 執行 Query
mysql> select * from user;
+----+---------+------+----------+---------------+-----------------+------+---------------------+-------------+
| id | account | name | password | department_id | department_name | salt | create_time | update_time |
+----+---------+------+----------+---------------+-----------------+------+---------------------+-------------+
| 1 | 1 | 1 | 1 | 1 | 1 | 1s | 2018-10-01 14:46:55 | NULL |
+----+---------+------+----------+---------------+-----------------+------+---------------------+-------------+
1 row in set
在開啟 Query Profiler 功能之后,MySQL 就會自動記錄所有執行的 Query 的 profile 信息了。
3、獲取系統中保存的所有 Query 的 profile 概要信息
mysql> show profiles;
+----------+------------+-----------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------------------------------------+
| 1 | 0.00427225 | select * from user |
| 2 | 0.00023 | select * from user |
| 3 | 0.00019475 | select * from user |
| 4 | 0.000192 | select * from user |
| 5 | 8.725E-5 | select * from user where id='1'
select * from user where id='1' |
| 6 | 0.00028875 | select * from user where id=1 |
| 7 | 7.5E-5 | set profiling=1 |
| 8 | 0.00020075 | select * from user |
+----------+------------+-----------------------------------------------------------------+
8 rows in set
通過執行 “SHOW PROFILE” 命令獲取當前系統中保存的多個 Query 的 profile 的概要信息。
4、針對單個 Query 獲取詳細的 profile 信息。
在獲取到概要信息之后,我們就可以根據概要信息中的 Query_ID 來獲取某個 Query 在執行過程中
詳細的 profile 信息了,具體操作如下:
mysql> show profile cpu,block io for query 8 ; +----------------------+----------+----------+------------+--------------+---------------+ | Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | +----------------------+----------+----------+------------+--------------+---------------+ | starting | 3.1E-5 | 0 | 0 | NULL | NULL | | checking permissions | 6E-6 | 0 | 0 | NULL | NULL | | Opening tables | 1.4E-5 | 0 | 0 | NULL | NULL | | System lock | 6E-6 | 0 | 0 | NULL | NULL | | init | 1.3E-5 | 0 | 0 | NULL | NULL | | optimizing | 3E-6 | 0 | 0 | NULL | NULL | | statistics | 8E-6 | 0 | 0 | NULL | NULL | | preparing | 5E-6 | 0 | 0 | NULL | NULL | | executing | 1E-6 | 0 | 0 | NULL | NULL | | Sending data | 4.6E-5 | 0 | 0 | NULL | NULL | | end | 3E-6 | 0 | 0 | NULL | NULL | | query end | 2E-6 | 0 | 0 | NULL | NULL | | closing tables | 5E-6 | 0 | 0 | NULL | NULL | | freeing items | 5.4E-5 | 0 | 0 | NULL | NULL | | logging slow query | 2E-6 | 0 | 0 | NULL | NULL | | cleaning up | 3E-6 | 0 | 0 | NULL | NULL | +----------------------+----------+----------+------------+--------------+---------------+ 16 rows in set
上面的例子中是獲取 CPU 和 Block IO 的消耗,非常清晰,對於定位性能瓶頸非常適用。希望得到取其他的信息,都可以通過執行 “SHOW PROFILE *** FOR QUERY n” 來獲取,各位讀者朋友可以自行測試熟悉。