SHOW PROFILES
mysql提供用来分析当前SQL执行的资源消耗情况。用于SQL调优
一、开启SHOW PROFILES
mysql默认是关闭的,可以通过变量开启这个服务,此服务和日志服务一样,只针对本次启动有效,重启恢复默认关闭。
- 查看是否开启
SHOW VARIABLES LIKE "profiling";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | ON |
+---------------+-------+
1 row in set (0.00 sec)
- 开启服务
-- 开启方式和其他服务一样
SET GLOBAL profiling = 1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
二、查看SHOW PROFILES
- 使用
SHOW PROFILE
语句来获取已经存储的剖析数据。如果不加参数,会显示状态以及它们持续的时间。
注意⚠️:SHOW PROFILES显示最近发给服务器的多条语句,条数根据会话变量profiling_history_size定义,默认是15,最大值为100。设为0等价于关闭分析功能。
SHOW PROFILES;
+----------+------------+-----------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------+
| 1 | 0.00050600 | set profiling = 1 |
| 2 | 0.00763100 | show proSHOW PROFILES |
| 3 | 0.00371200 | SHOW VARIABLES LIKE "%profile%" |
| 4 | 0.00222200 | SHOW VARIABLES LIKE "%pro%" |
| 5 | 0.00380000 | SHOW VARIABLES LIKE "%profil%" |
| 6 | 0.00305400 | SHOW VARIABLES LIKE "%profiling%" |
| 7 | 0.00159100 | SHOW VARIABLES LIKE "profiling" |
| 8 | 0.00030800 | SET GLOBAL profiling = 1 |
| 9 | 0.00020800 | SET GLOBAL profiling = 1 |
+----------+------------+-----------------------------------+
9 rows in set, 1 warning (0.00 sec)
SHOW PROFILE
可以深入的查看服务器执行语句的工作情况。以及也能帮助你理解执行语句消耗时间的情况
参数详解释:
-- ‘FOR QUERY 5’ === ‘FOR QUERY Query_ID’
SHOW PROFILE cpu, -- 显示cpu相关开销
block io, -- 显示IO相关开销
memory, -- 显示内存相关开销
swaps, -- 显示交换去相关开销
context switches, -- 显示上下文切换相关开销
ipc, -- 显示发送和接收相关开销
page faults, -- 显示错误相关开销
source FOR QUERY 5;
加参数查询:
-- 一般情况只需要查看 cpu、io相关信息
SHOW PROFILE cpu, block io FOR QUERY 5;
不加参数查询:
-- 不加参数,只显示每个过程消耗时间,不显示硬件消耗时间
SHOW PROFILE FOR QUERY 6;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.001212 |
| checking permissions | 0.000028 |
| Opening tables | 0.000113 |
| init | 0.000010 |
| System lock | 0.000008 |
| optimizing | 0.000006 |
| optimizing | 0.000009 |
| statistics | 0.000027 |
| preparing | 0.000017 |
| statistics | 0.000006 |
| preparing | 0.000011 |
| executing | 0.001100 |
| end | 0.000015 |
| query end | 0.000007 |
| removing tmp table | 0.000426 |
| query end | 0.000009 |
| removing tmp table | 0.000004 |
| closing tables | 0.000010 |
| freeing items | 0.000020 |
| cleaning up | 0.000016 |
+----------------------+----------+
20 rows in set, 1 warning (0.00 sec)
这里就简单介绍这个功能结束了,具体使用还是再生产环境中,就出现问题具体分析⚠️