mysql 5.7 profile_MySQL 5.5 SHOW PROFILE、SHOW PROFILES语句介绍

本文介绍如何使用SHOWPROFILE和SHOWPROFILES命令来分析MySQL查询性能。通过调整profiling参数,可以查看SQL语句的资源消耗情况,包括各个阶段的时间消耗及CPU使用情况。

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

SHOW PROFILE 和 SHOW PROFILES 语句显示 MySQL 数据库语句的资源消耗情况。

需要注意的是:这两条命令均显示当前会话的SQL资源消耗情况,不能显示其他会话的资源消耗情况。

--需要打开Profiling参数

mysql> show variables like 'profil%';

+------------------------+-------+

| Variable_name          | Value |

+------------------------+-------+

| profiling              | OFF   |

| profiling_history_size | 15    |

+------------------------+-------+

2 rows in set (0.10 sec)

mysql> set profiling = 1;

Query OK, 0 rows affected (0.00 sec)

mysql> show session variables like 'profil%';

+------------------------+-------+

| Variable_name          | Value |

+------------------------+-------+

| profiling              | ON    |

| profiling_history_size | 15    |

+------------------------+-------+

2 rows in set (0.00 sec)

SHOW PROFILES显示最近发送到MySQL服务器的语句。显示语句的条数由profiling_history_size参数设定,默认值是15,最大值是100,将参数的值设为0并不会关闭profiling功能。除了SHOW PROFILE 和 SHOW PROFILES,其他的语句都会显示在列表中。

SHOW PROFIL会显示一条语句的具体信息。如果没有指定FOR QUERY语句,输出结果会显示最后执行的语句。如果指定了FOR QUERY和语句编号,则会显示指定语句的信息。语句编号对应SHOW PROFILES显示结果中的Query_ID字段。LIMIT语句会限制输出的行数。默认,SHOW PROFILE语句只显示状态和运行时间这两个字段,状态字段和SHOW PROCESSLIST里面的状态字段相似。

--示例

mysql> select * from t9;

mysql> select * from t_arc1 limit 10;

mysql> show profiles;

+----------+------------+-------------------------------+

| Query_ID | Duration   | Query                         |

+----------+------------+-------------------------------+

|        1 | 0.00019375 | select * from t9              |

|        2 | 0.00023150 | select * from t_arc1 limit 10 |

+----------+------------+-------------------------------+

2 rows in set (0.00 sec)

mysql> show profile for query 2;

+----------------------+----------+

| Status               | Duration |

+----------------------+----------+

| starting             | 0.000058 |

| checking permissions | 0.000005 |

| Opening tables       | 0.000011 |

| System lock          | 0.000006 |

| init                 | 0.000015 |

| optimizing           | 0.000003 |

| statistics           | 0.000011 |

| preparing            | 0.000010 |

| executing            | 0.000002 |

| Sending data         | 0.000094 |

| end                  | 0.000003 |

| query end            | 0.000003 |

| closing tables       | 0.000003 |

| freeing items        | 0.000007 |

| logging slow query   | 0.000001 |

| cleaning up          | 0.000002 |

+----------------------+----------+

16 rows in set (0.00 sec)

mysql> show profile cpu for query 2;

+----------------------+----------+----------+------------+

| Status               | Duration | CPU_user | CPU_system |

+----------------------+----------+----------+------------+

| starting             | 0.000058 | 0.000000 |   0.000000 |

| checking permissions | 0.000005 | 0.000000 |   0.000000 |

| Opening tables       | 0.000011 | 0.000000 |   0.000000 |

| System lock          | 0.000006 | 0.000000 |   0.000000 |

| init                 | 0.000015 | 0.000000 |   0.000000 |

| optimizing           | 0.000003 | 0.000000 |   0.000000 |

| statistics           | 0.000011 | 0.000000 |   0.000000 |

| preparing            | 0.000010 | 0.000000 |   0.000000 |

| executing            | 0.000002 | 0.000000 |   0.000000 |

| Sending data         | 0.000094 | 0.000000 |   0.000000 |

| end                  | 0.000003 | 0.000000 |   0.000000 |

| query end            | 0.000003 | 0.000000 |   0.000000 |

| closing tables       | 0.000003 | 0.000000 |   0.000000 |

| freeing items        | 0.000007 | 0.000000 |   0.000000 |

| logging slow query   | 0.000001 | 0.000000 |   0.000000 |

| cleaning up          | 0.000002 | 0.000000 |   0.000000 |

+----------------------+----------+----------+------------+

16 rows in set (0.00 sec)

--查看SQL解析执行过程中每个步骤对应的源码的文件、函数名以及具体的源文件行数

MariaDB [test]> show profile source for query 31\G

*************************** 1. row ***************************

Status: starting

Duration: 0.000034

Source_function: NULL

Source_file: NULL

Source_line: NULL

*************************** 2. row ***************************

Status: checking permissions

Duration: 0.000005

Source_function: check_access

Source_file: sql_parse.cc

Source_line: 6043

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26506993/viewspace-2097692/,如需转载,请注明出处,否则将追究法律责任。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值