sql分析 show profile,performance_schema,optimizer_trace

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值