如何使用mysql profiling功能分析单条查询语句
Mysql从5.1版本开始引入show profile来剖析单条语句功能
一、 查看是否支持这个功能
yes表示支持
mysql> show variables like 'have_profiling';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| have_profiling | YES |
+----------------+-------+
1 row in set (0.01 sec)
二、使用步骤
1.在 sql命令行中输入:set profiling=1;来开启(当前会话关闭前,只需要执行一次)
mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
2.然后在服务器上执行你的SQL语句,都会被测量其消耗的时间和其他一些查询执行状态变更相关的数据
mysql> select count(*) from he_store_discount;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
3. 然后再执行:show prifiles;命令,所有的查询SQL都会被列出来
mysql> show profiles;
+----------+------------+----------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+----------------------------------------+
| 1 | 0.00013000 | select count(*) from he_store_discount |
| 2 | 0.00290900 | show databases |
| 3 | 0.00016200 | SELECT DATABASE() |
| 4 | 0.00052800 | show databases |
| 5 | 0.00204500 | show tables |
| 6 | 0.00027000 | select count(*) from he_store_discount |
+----------+------------+----------------------------------------+
6 rows in set, 1 warning (0.00 sec)
4.然后再根据编号(即Query_ID)查询具体SQL的执行过程
mysql> show profile for query 1;
+---------------+----------+
| Status | Duration |
+---------------+----------+
| starting | 0.000091 |
| freeing items | 0.000028 |
| cleaning up | 0.000011 |
+---------------+----------+
3 rows in set, 1 warning (0.01 sec) mysql> show profile for query 2;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000044 |
| checking permissions | 0.000011 |
| Opening tables | 0.000039 |
| init | 0.000013 |
| System lock | 0.000008 |
| optimizing | 0.000006 |
| statistics | 0.000012 |
| preparing | 0.000012 |
| executing | 0.002696 |
| Sending data | 0.000021 |
| end | 0.000004 |
| query end | 0.000006 |
| closing tables | 0.000003 |
| removing tmp table | 0.000006 |
| closing tables | 0.000003 |
| freeing items | 0.000017 |
| cleaning up | 0.000008 |
+----------------------+----------+
17 rows in set, 1 warning (0.00 sec) mysql> show profile for query 3;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000046 |
| checking permissions | 0.000009 |
| Opening tables | 0.000008 |
| init | 0.000014 |
| optimizing | 0.000008 |
| executing | 0.000012 |
| end | 0.000006 |
| query end | 0.000008 |
| closing tables | 0.000007 |
| freeing items | 0.000027 |
| cleaning up | 0.000017 |
+----------------------+----------+
11 rows in set, 1 warning (0.00 sec)
5.当查到最耗时的线程状态时,可以进一步选择all或者cpu,block io,page faults等明细类型来查看mysql在每个线程状态中使用什么资源上耗费了过高的时间:
mysql> show profile all for query 1;
+---------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+------------------+--------------+-------------+
| Status | Duration | CPU_user | CPU_system | Context_voluntary | Context_involuntary | Block_ops_in | Block_ops_out | Messages_sent | Messages_received | Page_faults_major | Page_faults_minor | Swaps | Source_function | Source_file | Source_line |
+---------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+------------------+--------------+-------------+
| starting | 0.000091 | 0.000073 | 0.000012 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | NULL | NULL | NULL |
| freeing items | 0.000028 | 0.000016 | 0.000011 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | mysql_parse | sql_parse.cc | 5593 |
| cleaning up | 0.000011 | 0.000008 | 0.000003 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | dispatch_command | sql_parse.cc | 1902 |
+---------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+------------------+--------------+-------------+
3 rows in set, 1 warning (0.00 sec) mysql> show profile cpu for query 1;
+---------------+----------+----------+------------+
| Status | Duration | CPU_user | CPU_system |
+---------------+----------+----------+------------+
| starting | 0.000091 | 0.000073 | 0.000012 |
| freeing items | 0.000028 | 0.000016 | 0.000011 |
| cleaning up | 0.000011 | 0.000008 | 0.000003 |
+---------------+----------+----------+------------+
3 rows in set, 1 warning (0.00 sec) mysql> show profile block io for query 1;
+---------------+----------+--------------+---------------+
| Status | Duration | Block_ops_in | Block_ops_out |
+---------------+----------+--------------+---------------+
| starting | 0.000091 | 0 | 0 |
| freeing items | 0.000028 | 0 | 0 |
| cleaning up | 0.000011 | 0 | 0 |
+---------------+----------+--------------+---------------+
3 rows in set, 1 warning (0.00 sec) mysql> show profile page faults for query 1;
+---------------+----------+-------------------+-------------------+
| Status | Duration | Page_faults_major | Page_faults_minor |
+---------------+----------+-------------------+-------------------+
| starting | 0.000091 | 0 | 0 |
| freeing items | 0.000028 | 0 | 0 |
| cleaning up | 0.000011 | 0 | 0 |
+---------------+----------+-------------------+-------------------+
3 rows in set, 1 warning (0.00 sec)
上面的输出中可以以很高的精度显示了查询的响应时间,列出了查询执行的每个步骤花费的时间
如何使用mysql profiling功能分析单条查询语句的更多相关文章
- mysql怎么限制某些查询语句的执行?
mysql怎么限制某些查询语句的执行? 比如某些sql语句执行时间很长,超过10s,怎么样超过10s就不让其执行? 后续更新中...
- mysql 存储过程:提供查询语句并返回查询执行影响的行数
mysql 存储过程:提供查询语句并返回查询执行影响的行数DELIMITER $$ DROP PROCEDURE IF EXISTS `p_get_select_row_number`$$ CREAT ...
- 高性能MySql进化论(十一):常见查询语句的优化
总结一下常见查询语句的优化方式 1 COUNT 1. COUNT的作用 · COUNT(table.filed)统计的该字段非空值的记录行数 · ...
- MySQL基础架构之查询语句执行流程
这篇笔记主要记录mysql的基础架构,一条查询语句是如何执行的. 比如,在我们从student表中查询一个id=2的信息 select * from student where id=2; 在解释这条 ...
- mysql一些常用的查询语句总结
工作中会遇到一些比较有用的mysql查询语句,有了它,可以对mysql进行更全面的维护和管理,下面就写一下我记录的 1.按照字段ru_id查询dsc_order_goods表中ru_id出现次数由多到 ...
- MYSQL数据库中的查询语句
查询的方法 *简单查询:select * from 表名 (* = 所有的) *读取特定列:select 字段一,字段二 from 表名 *条件查询:select * from 表名 where (多 ...
- 深入学习MySQL 01 一条查询语句的执行过程
在学习SpringCloud的同时,也在深入学习MySq中,听着<mysql45讲>,看着<高性能MySQL>,本系列文章是本人学习过程的总结,水平有限,仅供参考,若有不对之处 ...
- 当程序执行一条查询语句时,MySQL内部到底发生了什么? (说一下 MySQL 执行一条查询语句的内部执行过程?
先来个最基本的总结阐述,希望各位小伙伴认真的读一下,哈哈: 1)客户端(运行程序)先通过连接器连接到MySql服务器. 2)连接器通过数据库权限身份验证后,会先查询数据库缓存是否存在(之前执行过相同条 ...
- mysql系列-⼀条SQL查询语句是如何执⾏的?
⼀条SQL查询语句是如何执⾏的? ⼤体来说,MySQL 可以分为 Server 层和存储引擎层两部分 Server 层 Server 层包括连接器.查询缓存.分析器.优化器.执⾏器等,涵盖 MySQL ...
随机推荐
- C# 监控代码执行效率
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); stop ...
- 颠倒的价牌|2013年蓝桥杯A组题解析第四题-fishers
颠倒的价牌 小李的店里专卖其它店中下架的样品电视机,可称为:样品电视专卖店. 其标价都是4位数字(即千元不等). 小李为了标价清晰.方便,使用了预制的类似数码管的标价签,只要用颜色笔涂数字就可以了(参 ...
- POJ 2226 Muddy Fields(最小点覆盖)题解
题意:一片r*c的地,有些地方是泥地,需要铺地板.这些地板宽1,长无限,但只能铺在泥地上不能压到其他地方,问你铺满所有泥地最少几块 思路:我们把一行中连续的泥地看成整体,并把所有横的整体里的点编成一个 ...
- Cannot retrieve metalink for repository: epel 错误解决办法
centos下安装完EPEL源, 然后更新一下yum缓存, 如果发现这样的错误:Error: Cannot retrieve metalink for repository: epel. Please ...
- [bzoj 4034][HAOI 2015]树上操作
Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中 ...
- P2057 [SHOI2007]善意的投票
思路 简单的最小割模型 最小割的模型就是选出一些边,把点集划分成S和T两个部分,使得代价最小 到这题上就是板子了 代码 #include <cstdio> #include <alg ...
- 题解——牛客网OI赛制测试赛2
T1 规律题 考虑先全部选中再去重即可 #include <cstdio> #include <algorithm> #include <cstring> #inc ...
- 题解——loj6280 数列分块入门4 (分块)
分块维护一个区间和 然后记得更新的时候左边角块的tag不要打错到右边角块 #include <cstdio> #include <algorithm> #include < ...
- Hadoop之mapreduce
doc Hadoop初探之Stream Hadoop Stream 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试 用python + ...
- StringBuilder的三种删除方法比较
分别用一千万次循环来比较StringBuilder的三种删除方法所用时间 未避免偶然性,再循环一百次来比较总时间 --主类 public class StringBuilderRemove { pub ...