mysql-profiling详解

要想优化一条 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” 来获取,各位读者朋友可以自行测试熟悉。

原文地址:https://www.cnblogs.com/tinyj/p/9861239.html