使用mysql profiling功能剖析单条查询
5.1版本开始引入show profile剖析单条语句功能,支持show profiles和show profile语句,参数have_profiling;控制是否开启:
查看是否支持这个功能(查询为yes表示支持):
mysql > show variables like 'have_profiling';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| have_profiling | YES |
+----------------+-------+
1 row in set (0.00 sec)
需要临时使用时直接sql命令行中输入:set profiling=1;来开启
mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
然后在服务器上执行SQL语句,都会被测量其消耗的时间和其他一些查询执行状态变更相关的数据
mysql> select count(*) from xx;
+----------+
| count(*) |
+----------+
| 262144 |
+----------+
1 row in set (0.05 sec)
然后再执行:show prifiles;命令,所有的查询SQL都会被列出来
mysql> show profiles;
+----------+------------+-------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------+
| 1 | 0.05645950 | select count(*) from xx |
+----------+------------+-------------------------+
1 row in set, 1 warning (0.00 sec)
然后根据编号查询具体SQL的执行过程,这里演示只执行了一句,那就选项query id为1
mysql> show profile for query 1;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000041 |
| checking permissions | 0.000004 |
| Opening tables | 0.000017 |
| init | 0.000010 |
| System lock | 0.000006 |
| optimizing | 0.000004 |
| statistics | 0.000009 |
| preparing | 0.000008 |
| executing | 0.000001 |
| Sending data | 0.056110 |
| end | 0.000009 |
| query end | 0.000007 |
| closing tables | 0.000011 |
| freeing items | 0.000121 |
| logging slow query | 0.000001 |
| logging slow query | 0.000093 |
| cleaning up | 0.000010 |
+----------------------+----------+
17 rows in set, 1 warning (0.00 sec)
当查到最耗时的线程状态时,可以进一步选择all或者cpu,block io,page faults等明细类型来查看mysql在每个线程状态中使用什么资源上耗费了过高的时间:
show profile cpu for query 2;
上面的输出中可以以很高的精度显示了查询的响应时间,列出了查询执行的每个步骤花费的时间,其结果很难确定哪个步骤花费的时间太多,因为输出是按照执行顺序排序,而不是按照花费大小来排序的,如果要按照花费大小排序,就不能使用show prifile命令,而是直接使用information_schema.profiling表。如:
mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select count(*) from xx;
+----------+
| count(*) |
+----------+
| 262144 |
+----------+
1 row in set (0.05 sec)
mysql> show profiles;
+----------+------------+-------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------+
| 1 | 0.05509950 | select count(*) from xx |
+----------+------------+-------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> set @query_id=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select state,sum(duration) as total_r,round(100*sum(duration)/(select sum(duration) from information_schema.profiling where query_id=@query_id),2) as pct_r,count(*) as calls,sum(duration)/count(*) as "r/call" from information_schema.profiling where query_id=@query_id group by state order by total_r desc;
+----------------------+----------+-------+-------+--------------+
| state | total_r | pct_r | calls | r/call |
+----------------------+----------+-------+-------+--------------+
| Sending data | 0.054629 | 99.14 | 1 | 0.0546290000 |
| freeing items | 0.000267 | 0.48 | 1 | 0.0002670000 |
| logging slow query | 0.000070 | 0.13 | 2 | 0.0000350000 |
| starting | 0.000040 | 0.07 | 1 | 0.0000400000 |
| Opening tables | 0.000016 | 0.03 | 1 | 0.0000160000 |
| closing tables | 0.000011 | 0.02 | 1 | 0.0000110000 |
| init | 0.000010 | 0.02 | 1 | 0.0000100000 |
| cleaning up | 0.000010 | 0.02 | 1 | 0.0000100000 |
| end | 0.000009 | 0.02 | 1 | 0.0000090000 |
| statistics | 0.000009 | 0.02 | 1 | 0.0000090000 |
| preparing | 0.000008 | 0.01 | 1 | 0.0000080000 |
| query end | 0.000007 | 0.01 | 1 | 0.0000070000 |
| System lock | 0.000006 | 0.01 | 1 | 0.0000060000 |
| checking permissions | 0.000005 | 0.01 | 1 | 0.0000050000 |
| optimizing | 0.000004 | 0.01 | 1 | 0.0000040000 |
| executing | 0.000001 | 0.00 | 1 | 0.0000010000 |
+----------------------+----------+-------+-------+--------------+
16 rows in set (0.01 sec)
从上面的结果中可以看到,第一个是sending data(如果产生了临时表,第一就不是它了,那么临时表也是优先要解决的优化问题),另外还有sorting result(结果排序)也要注意,如果占比比较高,也要想办法优化,一般不建议在tuning sort buffer(优化排序缓冲区)或者类似的活动上花时间去优化。
如果要查询query id为1的Sending data状态的详细信息,可以使用如下SQL查询:
select * from information_schema.profiling where query_id=1 and state='Sending data'\G;
最后,做完剖析测试别忘记断开你的连接或者set profiling=0关闭这个功能。
使用mysql profiling功能剖析单条查询的更多相关文章
- MySQL剖析单条查询
使用SHOW PROFILE SHOW PROFILE命令默认是禁用的,可以通过以下命令修改 SET profiling=1; 当一条查询提交给服务器时,,此工具会记录剖析信息到一张临时表,并且给查询 ...
- MySql数据库列表数据分页查询、全文检索API零代码实现
数据条件查询和分页 前面文档主要介绍了元数据配置,包括表单定义和表关系管理,以及表单数据的录入,本文主要介绍数据查询和分页在crudapi中的实现. 概要 数据查询API 数据查询主要是指按照输入条件 ...
- MySQL 5.5开启慢查询功能
vim /etc/my.cnf [mysqld] slow-query-log = on # 开启慢查询功能 slow_query_log_file = /usr/local/mysql/data/s ...
- JDBC连接(MySql)数据库步骤,以及查询、插入、删除、更新等十一个处理数据库信息的功能
主要内容: JDBC连接数据库步骤. 一个简单详细的查询数据的例子. 封装连接数据库,释放数据库连接方法. 实现查询,插入,删除,更新等十一个处理数据库信息的功能.(包括事务处理,批量更新等) 把十 ...
- MySQL 服务器性能剖析
这是<高性能 MySQL(第三版)>第三章的读书笔记. 关于服务,常见的问题有: 如何确认服务器是否发挥了最大性能 找出执行慢的语句,为何执行慢 为何在用户端发生间歇性的停顿.卡死 通过性 ...
- 高性能MySQL笔记 第6章 查询性能优化
6.1 为什么查询速度会慢 查询的生命周期大致可按照顺序来看:从客户端,到服务器,然后在服务器上进行解析,生成执行计划,执行,并返回结果给客户端.其中“执行”可以认为是整个生命周期中最重要的阶段. ...
- [mysql] mysql 5.6.X 慢查询日志
慢查询日志 一篇好文章,学习保存.... 打开慢查询日志 慢查询日志,顾名思义就是记录执行比较慢查询的日志. 查看是否开启慢查询日志: show variables like '%slow%'; 打开 ...
- MySQL 性能优化之慢查询
性能优化的思路 首先需要使用慢查询功能,去获取所有查询时间比较长的SQL语句 其次使用explain命令去查询由问题的SQL的执行计划(脑补链接:点我直达1,点我直达2) 最后可以使用show pro ...
- MySQL Profiling 的使用
MySQL Profiling 的使用 在本章第一节中我们还提到过通过 Query Profiler 来定位一条 Query 的性能瓶颈,这里我们再详细介绍一下 Profiling 的用途及使用方法. ...
随机推荐
- django url 软编码
期初用django 开发应用的时候,完全是在urls.py 中硬编码配置地址,在views.py中HttpResponseRedirect()也是硬编码转向地址,当然在template 中也是一样了, ...
- display:inline-block; 到底是个啥玩意?
display:inline; 内联元素,简单来说就是在同一行显示.display:block; 块级元素,简单来说就是就是有换行,会换到第二行.display:inline-block; 就是在同一 ...
- composer很慢修改镜像
有两种方式启用本镜像服务: 系统全局配置: 即将配置信息添加到 Composer 的全局配置文件 config.json 中.见“例1” 单个项目配置: 将配置信息添加到某个项目的 composer. ...
- 如何使用Jlink
下载程序: 1. 连上Jlink的USB到PC上.连接JTAG到GT2440开发板上,选择从Nor Flash 启动. 2. 板子上电后,启动J-Flash ARM .File -> New P ...
- java ajax传值 中文乱码
String remark = new String(this.getRequest().getParameter("remark").getBytes("iso885 ...
- HDU 4718 The LCIS on the Tree(树链剖分)
Problem Description For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i < ...
- Codeforces Round #284 (Div. 1)
A. Crazy Town 这一题只需要考虑是否经过所给的线,如果起点和终点都在其中一条线的一侧,那么很明显从起点走点终点是不需要穿过这条线的,否则则一定要经过这条线,并且步数+1.用叉积判断即可. ...
- Facebook开源项目:我们为什么要用Fresco框架?
(Facebook开源项目)Fresco:一个新的Android图像处理类库 在Facebook的Android客户端上快速高效的显示图片是非常重要的.然而多年来,我们遇到了很多如何高效存储图片的问题 ...
- Java实现文件的读写,复制
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStr ...
- 关于mybatis的参数2个使用经验(类似于struts2的通配所有页面的action配置,xmlsq语句参数类型为基本类型时的快捷指定办法)
1.我们都知道在struts2中为防止浏览器绕过struts过滤器直接请求页面,所以我们都会配置一个拦截所有页面的action,如下: <action name="*"> ...