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表的增删改查基础教程
Apr 07 MySQL
MySQL主从搭建(多主一从)的实现思路与步骤
May 13 MySQL
为什么MySQL分页用limit会越来越慢
Jul 25 MySQL
MySQL 用 limit 为什么会影响性能
Sep 15 MySQL
MySQL连接控制插件介绍
Sep 25 MySQL
一篇文章看懂MySQL主从复制与读写分离
Nov 07 MySQL
Mysql中有关Datetime和Timestamp的使用总结
Dec 06 MySQL
Mysql数据库表中为什么有索引却没有提高查询速度
Feb 24 MySQL
MYSQL事务的隔离级别与MVCC
May 25 MySQL
mysql数据库实现设置字段长度
Jun 10 MySQL
MySQL下载安装配置详细教程 附下载资源
Sep 23 MySQL
MySQL中TIMESTAMP类型返回日期时间数据中带有T的解决
Dec 24 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
WHOIS类的修改版
2006/10/09 PHP
PHP容易忘记的知识点分享
2013/04/30 PHP
php启用sphinx全文搜索的实现方法
2014/12/24 PHP
Laravel中扩展Memcached缓存驱动实现使用阿里云OCS缓存
2015/02/10 PHP
php提供实现反射的方法和实例代码
2019/09/17 PHP
PHP 判断字符串是中文还是英文, 或者是中英混合
2021/03/09 PHP
用JavaScript实现页面重定向功能的教程
2015/06/04 Javascript
有关json_decode乱码及NULL的问题
2015/10/13 Javascript
javascript HTML5 canvas实现打砖块游戏
2020/06/18 Javascript
Nodejs 发送Post请求功能(发短信验证码例子)
2017/02/09 NodeJs
Angular.js中控制器之间的传值详解
2017/04/24 Javascript
ReactJS实现表单的单选多选和反选的示例
2017/10/13 Javascript
使用vue的v-for生成table并给table加上序号的实例代码
2017/10/27 Javascript
React 组件转 Vue 组件的命令写法
2018/02/28 Javascript
从零开始学习搭建React脚手架项目
2018/08/23 Javascript
详解ES6 系列之异步处理实战
2018/10/26 Javascript
JS实现将对象转化为数组的方法分析
2019/01/21 Javascript
python实现在目录中查找指定文件的方法
2014/11/11 Python
python实现获取客户机上指定文件并传输到服务器的方法
2015/03/16 Python
深入理解Python中的*重复运算符
2017/10/28 Python
Python中optparser库用法实例详解
2018/01/26 Python
TensorFlow数据输入的方法示例
2018/06/19 Python
Python面向对象程序设计之继承、多态原理与用法详解
2020/03/23 Python
Pytest框架之fixture的详细使用教程
2020/04/07 Python
Python调用C语言程序方法解析
2020/07/07 Python
Python 实现二叉查找树的示例代码
2020/12/21 Python
对Pytorch 中的contiguous理解说明
2021/03/03 Python
SmartBuyGlasses台湾:名牌眼镜,名牌太阳眼镜及隐形眼镜
2017/01/04 全球购物
物理研修随笔感言
2014/02/14 职场文书
秋季校运动会广播稿
2014/02/23 职场文书
优秀班集体先进事迹材料
2014/05/28 职场文书
励志演讲稿500字
2014/08/21 职场文书
公民授权委托书范本
2014/09/17 职场文书
大学迎新生的欢迎词
2019/06/25 职场文书
告别网页搜索!教你用python实现一款属于自己的翻译词典软件
2021/06/03 Python
React配置子路由的实现
2021/06/03 Javascript