如何使用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 ...
随机推荐
- 解决Tax discount configure 报出异常
If your tax calculation is based on a problematic configuration, the following warnings appear: Warn ...
- AndroidO Treble架构分析【转】
本文转载自:https://blog.csdn.net/yangwen123/article/details/79835965 从AndroidO开始,google引入了Treble架构,目的是为了方 ...
- dart基础语法
.关于 runApp() 上面的实例代码中使用了 runApp() 方法,runApp 方法接收的指定参数类型为 Widget,即: runApp(Widget).在 Flutter 的组件树(wid ...
- MongoDB NoSQL 常用指令
查询 日期区间 db.<collections>.find({"service_name":"xxx"}).sort({ update_time:- ...
- php知识点-1
global 是在函数内部 声明一个 函数外部的变量(即所谓的全局变量, 而所谓的超全局变量是指 像 $_POST, $GLOBALS等之类的自动系统变量) 的一个别名. 在函数内部使用 unset( ...
- P4782 【模板】2-SAT 问题
https://www.luogu.org/problemnew/show/P4782 链接 https://www.luogu.org/problemnew/show/P4782 思路 选a就必须选 ...
- UML类图中箭头的含义
Explanation of the UML arrows Here's some explanations from the Visual Studio 2015 docs: UML Class D ...
- html 之 position 绝对定位与相对定位(待补充)
相对定位:对于区块标签而言,占着原有的空间 绝对定位:对于网页而言,不占原来的空间
- 题解——洛谷P2613 【模板】有理数取余(扩展欧几里得算法+逆元)
题面 题目描述 给出一个有理数 c=\frac{a}{b} ,求 c mod19260817 的值. 输入输出格式 输入格式: 一共两行. 第一行,一个整数 \( a \) .第二行,一个整 ...
- ifconfig 输出里没有IP地址
转载: http://blog.csdn.net/johnstrive/article/details/5625121 inet addr:....Bcast:.....Mask:255.255.25 ...