MySQL 5.5中SHOW PROFILE、SHOW PROFILES语句怎么用

47次阅读
没有评论

共计 3558 个字符,预计需要花费 9 分钟才能阅读完成。

这篇文章将为大家详细讲解有关 MySQL 5.5 中 SHOW PROFILE、SHOW PROFILES 语句怎么用,丸趣 TV 小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

关于“MySQL 5.5 中 SHOW PROFILE、SHOW PROFILES 语句怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-27发表,共计3558字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)