mysql-profiling详解

栏目: IT技术 · 发布时间: 6年前

内容简介: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-profiling详解

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

mysql-profiling详解

在开启 Query Profiler 功能之后,MySQL 就会自动记录所有执行的 Query 的 profile 信息了。


3、获取系统中保存的所有 Query 的 profile 概要信息

mysql-profiling详解

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

mysql-profiling详解

通过执行 “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” 来获取,各位读者朋友可以自行测试熟悉。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

标签: mysql profile

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web 2.0 Heroes

Web 2.0 Heroes

Bradley L. Jones / Wiley / 2008-04-14 / USD 24.99

Web 2.0 may be an elusive concept, but one thing is certain: using the Web as merely a means of retrieving and displaying information is history. Today?s Web is immediate, interactive, innovative. It ......一起来看看 《Web 2.0 Heroes》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具