如何使用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 ...
随机推荐
- 使用SSH框架遇到的错误总结
1.org.hibernate.exception.ConstraintViolationException: could not insert: 如果是主键是自增序列,映射文件 指定主键生成器< ...
- 获取验证码的URL后边为什么要加上一个值不断变化的参数?
$(function(){ $("#change").click(function(){ //修改src属性 $("#imgCode").attr(" ...
- 彻底理解mysql服务器的字符集转换问题
主要参考这三个文章: https://www.xiariboke.com/article/4147.html http://blog.sina.com.cn/s/blog_690c46500100k1 ...
- kubeadm 生成的token过期后,集群增加节点
通过kubeadm初始化后,都会提供node加入的token: You should now deploy a pod network to the cluster. Run "kubect ...
- C# 控制台运行 应用运行
https://blog.csdn.net/Koala_Ivy/article/details/79577830 开发遇到的问题 记录一下 前段时间捣鼓dotnetty框架,服务端写了一个控制台程序来 ...
- P3159 [CQOI2012]交换棋子
思路 相当神奇的费用流拆点模型 最开始我想到把交换黑色棋子看成一个流流动的过程,流从一个节点流向另一个节点就是交换两个节点,然后把一个位置拆成两个点限制流量,然后就有了这样的建图方法 S向所有初始是黑 ...
- IAR中的 identifier "FILE" is undefined 问题
最近由于希望使用IAR的printf()函数方便进行打印字符,出现IAR报错,即:identifier "FILE" is undefined,问题得以解决. (1)进行pr ...
- Derek解读Bytom源码-P2P网络 upnp端口映射
作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...
- 洛谷P1164 小A点菜 DP入门
原题传输门>>https://www.luogu.org/problem/show?pid=1164<< 前几天开始联系DP的,一路水题做到这,发现这题套不了模板了QAQ 在大 ...
- Selenium 库
自动化测试工具,支持多种浏览器.爬虫中主要用来解决JavaScript渲染问题. 用法 基本使用 from selenium import webdriver #浏览器驱动对象 from seleni ...