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 相关文章推荐
一文带你探究MySQL中的NULL
Nov 11 MySQL
浅谈mysql哪些情况会导致索引失效
Nov 20 MySQL
MySQL分区表管理命令汇总
Mar 21 MySQL
详解MySQL的主键查询为什么这么快
Apr 03 MySQL
MySQL事务操作的四大特性以及并发事务问题
Apr 12 MySQL
MySQL创建管理KEY分区
Apr 13 MySQL
mysql使用FIND_IN_SET和group_concat两个方法查询上下级机构
Apr 20 MySQL
MySQL索引 高效获取数据的数据结构
May 02 MySQL
详细介绍MySQL中limit和offset的用法
May 06 MySQL
MySQL的意向共享锁、意向排它锁和死锁
Jul 15 MySQL
SQLServer常见数学函数梳理总结
Aug 05 MySQL
MySQL count(*)统计总数问题汇总
Sep 23 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
php4的session功能评述(一)
2006/10/09 PHP
如何在PHP程序中防止盗链
2008/04/09 PHP
利用 fsockopen() 函数开放端口扫描器的实例
2017/08/19 PHP
Laravel中Facade的加载过程与原理详解
2017/09/22 PHP
js加解密 脚本解密
2008/02/22 Javascript
JavaScript限定复选框的选择个数示例代码
2013/08/25 Javascript
Jquery 动态生成表格示例代码
2013/12/24 Javascript
javascript函数中参数传递问题示例探讨
2014/07/31 Javascript
jQuery设置Cookie及删除Cookie实例分析
2016/04/15 Javascript
javascript特殊文本输入框网页特效
2016/09/13 Javascript
Bootstrap简单表单显示学习笔记
2016/11/15 Javascript
微信小程序之MaterialDesign--input组件详解
2017/02/15 Javascript
bootstrap Validator 模态框、jsp、表单验证 Ajax提交功能
2017/02/17 Javascript
webpack开发跨域问题解决办法
2017/08/03 Javascript
React学习笔记之列表渲染示例详解
2017/08/22 Javascript
nodejs 图片预览和上传的示例代码
2017/09/30 NodeJs
vue组件发布到npm简单步骤
2017/11/30 Javascript
Vue.js点击切换按钮改变内容的实例讲解
2018/08/22 Javascript
Vue项目数据动态过滤实践及实现思路
2018/09/11 Javascript
jQuery实现数字华容道小游戏(实例代码)
2020/01/16 jQuery
ES6 Symbol在对象中的作用实例分析
2020/06/06 Javascript
jQuery使用jsonp实现百度搜索的示例代码
2020/07/08 jQuery
python生成特定分布数的实例
2019/12/05 Python
Django 限制访问频率的思路详解
2019/12/24 Python
Django model.py表单设置默认值允许为空的操作
2020/05/19 Python
PythonPC客户端自动化实现原理(pywinauto)
2020/05/28 Python
运行Python编写的程序方法实例
2020/10/21 Python
python 中 .py文件 转 .pyd文件的操作
2021/03/04 Python
使用html5 canvas 画时钟代码实例分享
2015/11/11 HTML / CSS
简述使用ftp进行文件传输时的两种登录方式?它们的区别是什么?常用的ftp文件传输命令是什么?
2016/11/20 面试题
提高EJB性能都有哪些技巧
2012/03/25 面试题
毕业生应聘幼儿园的自荐信
2013/11/20 职场文书
酒店仓管员岗位职责
2015/04/01 职场文书
成绩单家长意见
2015/06/03 职场文书
用Python简陋模拟n阶魔方
2021/04/17 Python
Python加密与解密模块hashlib与hmac
2022/06/05 Python