show profile
show profile 是mysql的一个性能分析命令,可以跟踪sql的各种资源消耗。
查看是否支持show profile 功能,yes表示支持。从msyql5.0.37开始,MySQL支持show profile。
select @@have_profiling;
查看当前是否启用了show profile,0表示未启用,1表示已启用。
select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 0 |
+-------------+
使用如下命令为当前会话开启或关闭性能分析,设成1表示开启,0表示关闭。
set @@profiling=1;
使用show profiles命令,可以为最近发送的sql语句做一个概要的性能分析。展示的条目数有profiling_history_size会话变量控制,该变量的默认值为15,最大值为100,将值设置为0具有禁用分析的实际效果。
-- 默认设置15条
show profiles;
-- 使用profiling_history_size调整展示的条目数
select profiling_history_size=20
使用show profile分析指定查询:
-- 找到要分析的sql对应的 QUERY_ID
mysql> show profiles;
+----------+------------+---------------------------------+
| Query_ID | Duration | Query |
+----------+------------+---------------------------------+
| 1 | 0.00007875 | show database |
| 2 | 0.00084850 | show databases |
| 3 | 0.00013550 | SELECT DATABASE() |
| 4 | 0.00047850 | show databases |
| 5 | 0.00082775 | show tables |
| 6 | 0.00109725 | show tables |
| 7 | 0.00026550 | select * from RECOVER_YOUR_DATA |
+----------+------------+---------------------------------+
7 rows in set, 1 warning (0.00 sec)
-- 根据query_id查询sql各执行阶段的cpu开销
mysql> show profile for query 7;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000089 |
| Executing hook on transaction | 0.000005 |
| starting | 0.000007 |
| checking permissions | 0.000006 |
| Opening tables | 0.000028 |
| init | 0.000005 |
| System lock | 0.000010 |
| optimizing | 0.000004 |
| statistics | 0.000012 |
| preparing | 0.000016 |
| executing | 0.000037 |
| end | 0.000003 |
| query end | 0.000003 |
| waiting for handler commit | 0.000007 |
| closing tables | 0.000006 |
| freeing items | 0.000018 |
| cleaning up | 0.000012 |
+--------------------------------+----------+
17 rows in set, 1 warning (0.00 sec)
分析完成后,记得关闭 show profile功能
set @@profiling