MYSQL profiling分析SQL慢的具体原因

本文介绍了如何使用MySQL的profiling功能来分析SQL查询的性能问题。通过查看profile状态,打开profile,执行SQL,然后通过show profiles获取query_id,最后详细分析特定query的性能,从而定位慢查询的原因。重点关注的性能指标包括CPU、Block IO、Memory和Swaps。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 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 sec)

这里举例的sql没有性能问题,真实场景的sql可能会有性能问题,具体问题具体分析。

如果想查所有的字段如下:

mysql> show profile all for query 5; 
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------------+------------------+-------------+
| 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.000043 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | NULL                  | NULL             |        NULL |
| checking permissions | 0.000005 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | check_access          | sql_parse.cc     |        5326 |
| Opening tables       | 0.000015 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | open_tables           | sql_base.cc      |        5102 |
| init                 | 0.000019 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_prepare_select  | sql_select.cc    |        1058 |
| System lock          | 0.000008 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_lock_tables     | lock.cc          |         311 |
| optimizing           | 0.000006 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | optimize              | sql_optimizer.cc |         146 |
| statistics           | 0.000014 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | optimize              | sql_optimizer.cc |         372 |
| preparing            | 0.000013 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | optimize              | sql_optimizer.cc |         495 |
| executing            | 0.000002 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | exec                  | sql_executor.cc  |         117 |
| Sending data         | 0.000048 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | exec                  | sql_executor.cc  |         197 |
| end                  | 0.000003 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_execute_select  | sql_select.cc    |        1113 |
| query end            | 0.000005 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_execute_command | sql_parse.cc     |        5024 |
| closing tables       | 0.000008 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_execute_command | sql_parse.cc     |        5073 |
| freeing items        | 0.000010 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_parse           | sql_parse.cc     |        6501 |
| logging slow query   | 0.000040 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             8 |             0 |                 0 |                 0 |                 0 |     0 | log_slow_do           | sql_parse.cc     |        1942 |
| cleaning up          | 0.000013 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | dispatch_command      | sql_parse.cc     |        1844 |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------------+------------------+-------------+
16 rows in set, 1 warning (0.00 sec)

如果想查看关键的几个字段,cpu,block io,memory,swaps的时间消耗,如下:

mysql> show profile cpu,block io,memory,swaps for query 5;  
+----------------------+----------+----------+------------+--------------+---------------+-------+
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | Swaps |
+----------------------+----------+----------+------------+--------------+---------------+-------+
| starting             | 0.000048 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| checking permissions | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| Opening tables       | 0.000018 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| init                 | 0.000020 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| System lock          | 0.000008 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| optimizing           | 0.000005 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| statistics           | 0.000011 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| preparing            | 0.000011 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| executing            | 0.000002 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| Sending data         | 0.000053 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| end                  | 0.000004 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| query end            | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| closing tables       | 0.000008 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| freeing items        | 0.000011 | 0.000000 |   0.000000 |            0 |             0 |     0 |
| logging slow query   | 0.000040 | 0.000000 |   0.000000 |            0 |             8 |     0 |
| cleaning up          | 0.000010 | 0.000000 |   0.000000 |            0 |             0 |     0 |
+----------------------+----------+----------+------------+--------------+---------------+-------+
16 rows in set, 1 warning (0.00 sec)

mysql>
``
**如果明细里出现以下四种,则需要进行优化**


![在这里插入图片描述](https://img-blog.csdnimg.cn/20201125174324411.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zOTE1NjczOA==,size_16,color_FFFFFF,t_70#pic_center)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值