Query Profiler是MYSQL自带的一种query诊断分析工具,通过它可以分析出一条SQL语句的性能瓶颈在什么地方。通常我们是使用的explain,以及slow query log都无法做到精确分析,但是Query Profiler却可以定位出一条SQL语句执行的各种资源消耗情况,比如CPU,IO等,以及该SQL执行所耗费的时间等。不过该工具只有在MYSQL 5.0.37以及以上版本中才有实现。
默认的情况下,MYSQL的该功能没有打开,需要自己手动启动。可以通过如下方法查看当前mysql服务器是否开启了该功能。
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| profiling | OFF |
| profiling_history_size | 15 |
+------------------------+-------+
2 rows in set (0.03 sec)
profiling参数值为OFF,说明没有打开该功能。
profiling_history_size参数值为15表示,记录最近15次的查询历史。该值可以修改。
下边说说如何打开profiling功能:
MYSQL提示符下执行如下命令:
mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)
然后再次检验下执行的效果:
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| profiling | ON |
| profiling_history_size | 15 |
+------------------------+-------+
2 rows in set (0.00 sec)
profiling值为ON说明已经启动该功能。
下边说说如何使用show profiles;
1.首先执行一查询语句:
mysql> select * from user;
+------+-----------+
| id | name |
+------+-----------+
| 22 | abc |
| 223 | dabc |
| 2232 | dddabc |
| 45 | asdsagd |
| 23 | ddddddddd |
| 22 | ddd |
| 28 | sssddd |
+------+-----------+
7 rows in set (0.06 sec)
2.通过show profiles命令查看系统中多个query的概要信息:
mysql> show profiles;
+----------+------------+-----------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------+
| 1 | 0.00013600 | set profiling=1 |
| 2 | 0.00092300 | show variables like '%profiling%' |
| 3 | 0.08506075 | show databases |
| 4 | 0.02698550 | SELECT DATABASE() |
| 5 | 0.07408475 | show tables |
| 6 | 0.05769725 | select * from user |
+----------+------------+-----------------------------------+
6 rows in set (0.01 sec)
其中Query_ID表示查询ID,也就是个编号,Duration表示对应的query语句执行的时间,单位是秒,query表示具体的query语句。我们可以看到刚才我们最后执行的select * from user语句执行的时间是0.05769725,单位是秒,也就是57ms
3.获取单个query的详细profile信息,可以通过如下语句:
mysql> show profile cpu,block io for query 6;
+--------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+--------------------+----------+----------+------------+--------------+---------------+
| starting | 0.014438 | NULL | NULL | NULL | NULL |
| Opening tables | 0.042860 | NULL | NULL | NULL | NULL |
| System lock | 0.000007 | NULL | NULL | NULL | NULL |
| Table lock | 0.000012 | NULL | NULL | NULL | NULL |
| init | 0.000019 | NULL | NULL | NULL | NULL |
| optimizing | 0.000005 | NULL | NULL | NULL | NULL |
| statistics | 0.000018 | NULL | NULL | NULL | NULL |
| preparing | 0.000011 | NULL | NULL | NULL | NULL |
| executing | 0.000004 | NULL | NULL | NULL | NULL |
| Sending data | 0.000232 | NULL | NULL | NULL | NULL |
| end | 0.000007 | NULL | NULL | NULL | NULL |
| query end | 0.000005 | NULL | NULL | NULL | NULL |
| freeing items | 0.000073 | NULL | NULL | NULL | NULL |
| logging slow query | 0.000003 | NULL | NULL | NULL | NULL |
| cleaning up | 0.000004 | NULL | NULL | NULL | NULL |
+--------------------+----------+----------+------------+--------------+---------------+
15 rows in set (0.02 sec)
总结:Query Profiler对于SQL性能分析和诊断费用有用。另外,该命令还有如下参数可以选择:
- ALL - displays all information
- BLOCK IO - displays counts for block input and output operations
- CONTEXT SWITCHES - displays counts for voluntary and involuntary context switches
- IPC - displays counts for messages sent and received
- MEMORY - is not currently implemented
- PAGE FAULTS - displays counts for major and minor page faults
- SOURCE - displays the names of functions from the source code, together with the name and line number of the file in which the function occurs
- SWAPS - displays swap counts
使用SQLyog等工具可以直接看到Profiler页面,输入 Show profiles;语句后会显示更为详细的信息。
- [MySQL]--查询性能分析工具-explain关键字
explain显示了MySQL如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. explain的使用方法很简单,只需要在select查询语句前面加上expl ...
- Mysql慢查询(分析工具)
慢查询分析工具[mysqldumpslow] 常用的慢查询日志分析工具 汇总除查询条件外其他完全相同的SQL,并将分析结果按照参数中所指定的顺序输出 语法: mysqldumpslow -s r -t ...
- mysql slow log分析工具的比较
mysql 中的 slow log 是用来记录执行时间较长(超过 long_query_time 秒)的 sql 的一种日志工具. 启用 slow log 在 my.cnf 中设置 [mysqld] ...
- 慢查询日志分析工具之pt-query-digest
简介 pt-query-digest 是用于分析mysql慢查询的一个工具,与mysqldumpshow工具相比,py-query_digest 工具的分析结果更具体,更完善. 有时因为 ...
- 26、mysqlsla慢查询日志分析工具
1.介绍: mysqlsla是hackmysql.com推出的一款MySQL的日志分析工具,可以分析mysql的慢查询日志.分析慢查询非常好用,能针 对库分析慢查询语句的执行频率.扫描的数据量.消耗时 ...
- pt-query-digest查询日志分析工具
1.工具介绍 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcp ...
- gops —— Go 程序诊断分析工具
GitHub: https://github.com/google/gops 一个用于列出和诊断分析系统中正在运行的 Go 程序的命令行工具 安装 go get -u github.com/googl ...
- MySQL查询截取分析
一.查询优化 1,mysql的调优大纲 慢查询的开启并捕获 explain+慢SQL分析 show profile查询SQL在Mysql服务器里面的执行细节和生命周期情况 SQL数据库服务器的参数调优 ...
- mysql慢查询日志分析工具 mysqlsla(转)
mysql数据库的慢查询日志是非常重要的一项调优辅助日志,但是mysql默认记录的日志格式阅读时不够友好,这是由mysql日志记录规则所决定的,捕获一条就记录一条,虽说记录的信息足够详尽,但如果将浏览 ...
随机推荐
- Python3解leetcode Isomorphic Strings
问题描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- JS中数据结构之散列表
散列是一种常用的数据存储技术,散列后的数据可以快速地插入或取用.散列使用的数据 结构叫做散列表.在散列表上插入.删除和取用数据都非常快. 下面的散列表是基于数组进行设计的,数组的长度是预先设定的,如有 ...
- tarjan-LCA模板
洛谷P3379 #include <cstdio> using namespace std; ; struct etype{ int t,next; }; struct qtype{ in ...
- 【BZOJ3522&BZOJ4543】Hotel加强版(长链剖分,树形DP)
题意:求一颗树上三点距离两两相等的三元组对数 n<=1e5 思路:From https://blog.bill.moe/bzoj4543-hotel/ f[i][j]表示以i为根的子树中距离i为 ...
- 前端每日实战:46# 视频演示如何用纯 CSS 创作一个在容器中反弹的小球
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/jKVbyE 可交互视频教程 此视频 ...
- 使用mysql应该注意的细节
一.表及字段的命名规范 1.可读性原则 使用大写和小写来格式化的库对象名字以获得良好的可读性. 例如:使用CustAdress而不是custaddress来提高可读性.(这里注意有些DBMS系统对表名 ...
- 杂项:JFB-权限设置
ylbtech-杂项:JFB-权限设置 1. 家政经理返回顶部 1. if (UserContext.GetTeamId() == (int)UserType.Manager) { condition ...
- Flink水印机制(watermark)
Flink流处理时间方式 EventTime 时间发生的时间,例如:点击网站上的某个链接的时间 IngestionTime 某个Flink节点的source operator接收到数据的时间,例如:某 ...
- CPU的架构:x86、arm、mips、龙芯等
在公司经常听其他工程师讲x86,arm平台啥的,作为一个算法工程师,我听过却不知道这是啥!!!(汗颜) 现在偷偷学起: x86,arm,mips等这些都是CPU的架构,不管是手机电脑还是一些嵌入式的设 ...
- selenium快捷键操作
常用的键盘操作 send_keys(Keys.BACK_SPACE) 删除键(BackSpace) send_keys(Keys.SPACE) 空格键(Space) send_keys(Keys.TA ...