如何使用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 ...
随机推荐
- 网络 --- 1 c/s (b/s)架构 ip 初始socket
一.c/s b/s c/s架构:客户端(client)/服务器(server) 软件cs架构:微信,陌陌,qq等 硬件cs架构:打印机 b/s架构:浏览器(browser)/服务器(server) ...
- Jenkins中的邮件配置
摘自http://blog.csdn.net/fullbug/article/details/53024562 Jenkins是一个很受欢迎的CI持续集成工具,能够实现项目的自动构建.打包.测试.发布 ...
- memalign的作用【转】
本文转载自:https://blog.csdn.net/lvwx369/article/details/41726415 转自:http://hi.baidu.com/narshben/item/ca ...
- Git学习笔记---安装与初始化 连接远程版本库
1.Git的安装 sudo apt-get install git 用的是linux(ubuntu)系统,安装非常简单,上面一条命令就够了. 2.初次运行的配置 Git 提供了一个叫做 git con ...
- POJ 1873 The Fortified Forest(凸包)题解
题意:二维平面有一堆点,每个点有价值v和删掉这个点能得到的长度l,问你删掉最少的价值能把剩余点围起来,价值一样求删掉的点最少 思路:n<=15,那么直接遍历2^15,判断每种情况.这里要优化一下 ...
- ZOJ 1602 Multiplication Puzzle(区间DP)题解
题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...
- C# 递归模型定义。赋值
https://blog.csdn.net/wumiju/article/details/80429412 public partial class ProductTypeModel:DbEntity ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- 【Python】【数据库】
#[[数据库]]'''MySQL是Web世界中使用最广泛的数据库服务器.SQLite的特点是轻量级.可嵌入,但不能承受高并发访问,适合桌面和移动应用.而MySQL是为服务器端设计的数据库,能承受高并发 ...
- JS快速构建数组方法
一.常用(普通)数组的构建 1.1 直接构建 let arr = ['mock1', 'mock2', 'mock3'] 1.2 通过new Array let arr = newArray('moc ...