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

最新文章

  1. UI/UE/ID/UED/UCD的区别
  2. CSS3硬件加速需要注意的事项
  3. LGLProgressHUD
  4. 【BZOJ】1089: [SCOI2003]严格n元树(递推+高精度/fft)
  5. html 二级联动(省市联动)
  6. DAS 原文出自【比特网】
  7. 全球最低功耗蓝牙单芯片(DA14580)系统架构和应用开发框架分析
  8. QT笔记(1)--QT编程环境搭建
  9. JavaScript-2.2 document.write 输出到页面的内容
  10. 如何基于对话框的project基于改变BCG的
  11. SmartCoder每日站立会议05
  12. python3学习笔记1---引用http://python3-cookbook.readthedocs.io/zh_CN/latest/
  13. 2025战略,中秋送福利!免费开源ERP Odoo Windows 一键傻瓜式安装版发布
  14. 1705: 小明在工作(zzuli)
  15. vector和list
  16. source、sh、bash
  17. 虚拟机centos 7联网设置之NAT方式
  18. Hadoop学习笔记之四:HDFS客户端
  19. GMIS 2017 大会陈雨强演讲:机器学习模型,宽与深的大战
  20. Eclipse和Tomcat使用过程的一些配置、错误等的总结记录

热门文章

  1. Ubuntu的中文乱码问题
  2. nginx处理高并发请求强于apache
  3. 自己实现一个shell
  4. [转](译)KVO的内部实现
  5. inception_web
  6. WPF中的DataGridTemplateColumn实现点击列标题排序
  7. 【算法笔记】B1045 快速排序
  8. DP 免费馅饼 HDU1176
  9. win7 64位英文版 ado驱动
  10. linux 搭建gitlab server