Mysql Show Profile


Posted in MySQL onApril 05, 2021

简介

mysql提供可以用来分析当前会话中语句执行的资源消耗情况,可以用于SQL的调优的测量。

官网:https://dev.mysql.com/doc/refman/8.0/en/show-profile.html

默认情况下,参数处于关闭状态,并保存最近15次的运行结果。

分析步骤

  1. 是否支持,看看当前的mysql版本是否支持
mysql> Show  variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling     | OFF   |
+---------------+-------+
1 row in set (0.04 sec)
# 默认是关闭,使用前需要开启
  1. 开启功能,默认是关闭,使用前需要开启
mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)

mysql> Show  variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling     | ON    |
+---------------+-------+
1 row in set (0.05 sec)
  1. 运行SQL
mysql> select * from emp group by id%20  order by 2;
+----+-------+--------+----------+-----+------------+---------+--------+--------+
| id | empno | ename  | job      | mgr | hiredate   | sal     | comm   | deptno |
+----+-------+--------+----------+-----+------------+---------+--------+--------+
|  1 |  1002 | Oobqzz | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    100 |
|  2 |  1003 | ESDoGs | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    108 |
|  3 |  1004 | qZXPfi | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    104 |
|  4 |  1005 | ZxrpzB | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    101 |
|  5 |  1006 | tpqNQQ | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    106 |
|  6 |  1007 | JCHGkg | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    100 |
|  7 |  1008 | QVjinQ | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    102 |
|  8 |  1009 | cqwnXX | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    109 |
|  9 |  1010 | YVLQWn | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    104 |
| 10 |  1011 | uKopHs | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    108 |
| 11 |  1012 | mEknMS | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    101 |
| 12 |  1013 | XyzdPR | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    107 |
| 13 |  1014 | kRBijz | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    108 |
| 14 |  1015 | RGFiZA | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    105 |
| 15 |  1016 | bEVOht | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    104 |
| 16 |  1017 | qdwVnE | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    101 |
| 17 |  1018 | YvhAdQ | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    108 |
| 18 |  1019 | asPVhZ | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    105 |
| 19 |  1020 | KTpTzR | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    106 |
| 20 |  1021 | SnOcYH | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    103 |
+----+-------+--------+----------+-----+------------+---------+--------+--------+
20 rows in set (2.12 sec)

mysql> select * from emp group by id%10 limit 150000;
+----+-------+--------+----------+-----+------------+---------+--------+--------+
| id | empno | ename  | job      | mgr | hiredate   | sal     | comm   | deptno |
+----+-------+--------+----------+-----+------------+---------+--------+--------+
| 10 |  1011 | uKopHs | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    108 |
|  1 |  1002 | Oobqzz | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    100 |
|  2 |  1003 | ESDoGs | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    108 |
|  3 |  1004 | qZXPfi | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    104 |
|  4 |  1005 | ZxrpzB | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    101 |
|  5 |  1006 | tpqNQQ | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    106 |
|  6 |  1007 | JCHGkg | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    100 |
|  7 |  1008 | QVjinQ | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    102 |
|  8 |  1009 | cqwnXX | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    109 |
|  9 |  1010 | YVLQWn | salesman |   1 | 2021-04-04 | 2000.00 | 400.00 |    104 |
+----+-------+--------+----------+-----+------------+---------+--------+--------+
10 rows in set (1.91 sec)
  1. 查看结果
mysql> show profiles;
+----------+------------+-----------------------------------------------+
| Query_ID | Duration   | Query                                         |
+----------+------------+-----------------------------------------------+
|        1 | 0.00059150 | Show  variables like 'profiling'              |
|        2 | 1.82311475 | select * from emp group by id%10 limit 150000 |
|        3 | 1.92254050 | select * from emp group by id%20  order by 5  |
|        4 | 1.91409800 | select * from emp group by id%20  order by 2  |
|        5 | 1.81238075 | select * from emp group by id%10 limit 150000 |
+----------+------------+-----------------------------------------------+
5 rows in set (0.14 sec)
  1. 诊断SQL
    show profile cpu,block io for query n (n为上一步前面的问题SQL数字号码);
mysql> show profile cpu,block io for query 4;
+----------------------+----------+----------+------------+--------------+---------------+
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting             | 0.000037 | 0.000000 | 0.000000   | NULL         | NULL          |
| checking permissions | 0.000005 | 0.000000 | 0.000000   | NULL         | NULL          |
| Opening tables       | 0.000016 | 0.000000 | 0.000000   | NULL         | NULL          |
| System lock          | 0.000008 | 0.000000 | 0.000000   | NULL         | NULL          |
| init                 | 0.000018 | 0.000000 | 0.000000   | NULL         | NULL          |
| optimizing           | 0.000003 | 0.000000 | 0.000000   | NULL         | NULL          |
| statistics           | 0.000017 | 0.000000 | 0.000000   | NULL         | NULL          |
| preparing            | 0.000006 | 0.000000 | 0.000000   | NULL         | NULL          |
| Creating tmp table   | 0.000308 | 0.000000 | 0.000000   | NULL         | NULL          |
| executing            | 0.000003 | 0.000000 | 0.000000   | NULL         | NULL          |
| Copying to tmp table | 1.913459 | 1.921875 | 0.000000   | NULL         | NULL          |
| Sorting result       | 0.000025 | 0.000000 | 0.000000   | NULL         | NULL          |
| Sending data         | 0.000044 | 0.000000 | 0.000000   | NULL         | NULL          |
| end                  | 0.000004 | 0.000000 | 0.000000   | NULL         | NULL          |
| removing tmp table   | 0.000007 | 0.000000 | 0.000000   | NULL         | NULL          |
| end                  | 0.000002 | 0.000000 | 0.000000   | NULL         | NULL          |
| query end            | 0.000003 | 0.000000 | 0.000000   | NULL         | NULL          |
| closing tables       | 0.000010 | 0.000000 | 0.000000   | NULL         | NULL          |
| freeing items        | 0.000124 | 0.000000 | 0.000000   | NULL         | NULL          |
| logging slow query   | 0.000002 | 0.000000 | 0.000000   | NULL         | NULL          |
| cleaning up          | 0.000001 | 0.000000 | 0.000000   | NULL         | NULL          |
+----------------------+----------+----------+------------+--------------+---------------+
21 rows in set (0.19 sec)

参数:

类型 含义
ALL 显示所有的开销信息
BLOCK IO 显示块IO相关开销
CONTEXT SWITCHES 上下文切换相关开销
CPU 显示CPU相关开销信息
IPC 显示发送和接收相关开销信息
MEMORY 显示内存相关开销信息
PAGE FAULTS 显示页面错误相关开销信息
SOURCE 显示和Source_function,Source_file,Source_line相关的开销信息
SWAPS 显示交换次数相关开销的信息

展示个最全的:

mysql> show profile all for query 4;
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
| 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.000037 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | NULL            | NULL          | NULL        |
| checking permissions | 0.000005 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_parse.cc  |        4833 |
| Opening tables       | 0.000016 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_base.cc   |        4888 |
| System lock          | 0.000008 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | lock.cc       |         299 |
| init                 | 0.000018 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |        2594 |
| optimizing           | 0.000003 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |         865 |
| statistics           | 0.000017 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |        1071 |
| preparing            | 0.000006 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |        1093 |
| Creating tmp table   | 0.000308 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |        1616 |
| executing            | 0.000003 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |        1851 |
| Copying to tmp table | 1.913459 | 1.921875 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |        1998 |
| Sorting result       | 0.000025 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |        2282 |
| Sending data         | 0.000044 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |        2395 |
| end                  | 0.000004 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |        2630 |
| removing tmp table   | 0.000007 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |       11304 |
| end                  | 0.000002 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_select.cc |       11329 |
| query end            | 0.000003 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_parse.cc  |        4512 |
| closing tables       | 0.000010 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_parse.cc  |        4564 |
| freeing items        | 0.000124 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_parse.cc  |        5840 |
| logging slow query   | 0.000002 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_parse.cc  |        1512 |
| cleaning up          | 0.000001 | 0.000000 | 0.000000   | NULL              | NULL                | NULL         | NULL          | NULL          | NULL              | NULL              | NULL              | NULL  | <unknown>       | sql_parse.cc  |        1468 |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
21 rows in set (0.23 sec)
  1. 特别需要注意的结论
  • converting HEAP to MyISAM 查询结果太大,内存都不够用了往磁盘上搬了。
  • Creating tmp table 创建临时表,拷贝数据到临时表,用完再删除。
  • Copying to tmp table on disk 把内存中临时表复制到磁盘,很危险!
  • locked 锁了都
MySQL 相关文章推荐
数据库的高级查询六:表连接查询:外连接(左外连接,右外连接,UNION关键字,连接中ON与WHERE的不同)
Apr 05 MySQL
详解mysql三值逻辑与NULL
May 19 MySQL
如何设计高效合理的MySQL查询语句
May 26 MySQL
MySQL Router实现MySQL的读写分离的方法
May 27 MySQL
MySQL连表查询分组去重的实现示例
Jul 01 MySQL
MySQL中几种插入和批量语句实例详解
Sep 14 MySQL
MySQL数据库超时设置配置的方法实例
Oct 15 MySQL
深入解析MySQL索引数据结构
Oct 16 MySQL
MySQL七种JOIN类型小结
Oct 24 MySQL
mysql5.6主从搭建以及不同步问题详解
Dec 04 MySQL
MySQL数据库优化之通过索引解决SQL性能问题
Apr 10 MySQL
delete in子查询不走索引问题分析
Jul 07 MySQL
Mysql - 常用函数 每天积极向上
Apr 05 #MySQL
mysql多表查询-笔记七
Apr 05 #MySQL
mysql部分操作
Apr 05 #MySQL
left join、inner join、right join的区别
数据库的高级查询六:表连接查询:外连接(左外连接,右外连接,UNION关键字,连接中ON与WHERE的不同)
mysql字符串截取函数小结
MySQL索引篇之千万级数据实战测试
You might like
PHP 动态随机生成验证码类代码
2010/04/09 PHP
php简单对象与数组的转换函数代码(php多层数组和对象的转换)
2011/05/18 PHP
javascript vvorld 在线加密破解方法
2008/11/13 Javascript
Javascript类库的顶层对象名用户体验分析
2010/10/24 Javascript
重写javascript中window.confirm的行为
2012/10/21 Javascript
Extjs grid panel自带滚动条失效的解决方法
2014/09/11 Javascript
bootstrap table 服务器端分页例子分享
2015/02/10 Javascript
vue 2.0组件与v-model详解
2017/03/27 Javascript
vue.js学习之vue-cli定制脚手架详解
2017/07/02 Javascript
JavaScript获取tr td 的三种方式全面总结(推荐)
2017/08/15 Javascript
快速解决brew安装特定版本flow的问题
2018/05/17 Javascript
layui radio性别单选框赋值方法
2018/08/15 Javascript
JSON字符串操作移除空串更改key/value的介绍
2019/01/05 Javascript
node.js中ws模块创建服务端和客户端,网页WebSocket客户端
2019/03/06 Javascript
简单易扩展可控性强的Jquery转盘抽奖程序
2019/03/16 jQuery
微信小程序实现侧边分类栏
2019/10/21 Javascript
js代码编写无缝轮播图
2020/09/13 Javascript
node.js 基于 STMP 协议和 EWS 协议发送邮件
2021/02/14 Javascript
Python制作Windows系统服务
2017/03/25 Python
Python切片工具pillow用法示例
2018/03/30 Python
Python socket模块ftp传输文件过程解析
2019/11/05 Python
Python基于DB-API操作MySQL数据库过程解析
2020/04/23 Python
pytorch cuda上tensor的定义 以及减少cpu的操作详解
2020/06/23 Python
Html5实现移动端、PC端 刮刮卡效果
2016/06/30 HTML / CSS
世界上最值得信赖的多日游在线市场:TourRadar
2018/07/20 全球购物
夏威夷灵感服装及配饰:Reyn Spooner
2018/09/18 全球购物
生物制药自我鉴定
2014/01/25 职场文书
党校培训自我鉴定
2014/02/01 职场文书
学生会主席竞聘书
2014/03/31 职场文书
幸福家庭标语
2014/06/27 职场文书
2015年度团总支工作总结
2015/04/23 职场文书
银行文明优质服务培训心得体会
2016/01/09 职场文书
2016民族团结先进个人事迹材料
2016/02/26 职场文书
2019年大学生学年自我鉴定!
2019/03/25 职场文书
MySQL 数据恢复的多种方法汇总
2021/06/21 MySQL
django 认证类配置实现
2021/11/11 Python