MYSQL profiling分析SQL慢的具体原因

本文介绍了如何使用MySQL的profiling功能来分析SQL查询的性能问题。通过查看profile状态,打开profile,执行SQL,然后通过show profiles获取query_id,最后详细分析特定query的性能,从而定位慢查询的原因。重点关注的性能指标包括CPU、Block IO、Memory和Swaps。
摘要由CSDN通过智能技术生成
  • profile是session级生效,当退出窗口时就失效了

1.查看profile是否打开
select @@profiling;

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |
+-------------+
1 row in set, 1 warning (0.00 sec)

mysql>

0代表关闭;1代表打开。

2.打开profile

mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

如果想打开,可以执行set profiling=1
3.使用
profiles一般为已经定位到慢sql的情况下,分析sql到底慢到哪里时使用。步骤如下:
第一:开启profile
第二:执行sql(定位到的慢sql)
第三:show profiles 查看自己执行的主要sql的query_id

mysql> show profiles;
+----------+------------+--------------------+
| Query_ID | Duration   | Query              |
+----------+------------+--------------------+
|        1 | 0.00010325 | select @@profiling |
|        2 | 0.00011750 | SELECT DATABASE()  |
|        3 | 0.00023675 | show databases     |
|        4 | 0.00015475 | show tables        |
|        5 | 0.00026000 | select * from user |
+----------+------------+--------------------+
5 rows in set, 1 warning (0.00 sec)

第四步:分析sql的性能
这里我主要关注query_id为5的sql

mysql> show profile for query 5;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000048 |
| checking permissions | 0.000006 |
| Opening tables       | 0.000018 |
| init                 | 0.000020 |
| System lock          | 0.000008 |
| optimizing           | 0.000005 |
| statistics           | 0.000011 |
| preparing            | 0.000011 |
| executing            | 0.000002 |
| Sending data         | 0.000053 |
| end                  | 0.000004 |
| query end            | 0.000006 |
| closing tables       | 0.000008 |
| freeing items        | 0.000011 |
| logging slow query   | 0.000040 |
| cleaning up          | 0.000010 |
+----------------------+----------+
16 rows in set, 1 warning (0.00
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值