利用show profiles 和 show profile查询SQL语句的资源统计


一般用于会话级,用于收集SQL语句的资源使用情况。

1.首先要设置系统变量profiling为on(作用于当前会话)

mysql> show variables like 'profiling%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| profiling              | OFF   |
| profiling_history_size | 15    |
+------------------------+-------+
2 rows in set (0.01 sec)

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

mysql> 

2.执行SQL语句

mysql> select * from user where user_id=2222;
+---------+--------------+---------------+---------------------+
| user_id | user_name    | user_email    | created             |
+---------+--------------+---------------+---------------------+
|    2222 | bill2222 | 123456@qq.com | 2020-02-09 12:24:32 |
+---------+--------------+---------------+---------------------+
1 row in set (0.00 sec)

3.通过show profiles查看

mysql> show profiles;
+----------+------------+---------------------------------------+
| Query_ID | Duration   | Query                                 |
+----------+------------+---------------------------------------+
|        1 | 0.00041000 | select database()                     |
|        2 | 0.00018500 | SELECT DATABASE()                     |
|        3 | 0.00382575 | show databases                        |
|        4 | 0.00040850 | show tables                           |
|        5 | 0.00106125 | show tables                           |
|        6 | 0.00211250 | desc user                             |
|        7 | 0.00031950 | SELECT * FROM USER WHERE user_id=2222 |
|        8 | 0.00019250 | SELECT DATABASE()                     |
|        9 | 0.00018200 | SELECT * FROM USER WHERE user_id=2222 |
|       10 | 0.00040700 | show databases                        |
|       11 | 0.00016275 | SELECT DATABASE()                     |
|       12 | 0.00031225 | SELECT * FROM USER WHERE user_id=2222 |
|       13 | 0.00041075 | show tables                           |
|       14 | 0.00031975 | select * from user where user_id=2222 |
+----------+------------+---------------------------------------+
14 rows in set, 1 warning (0.00 sec)


4.通过show profile for query 查看指定的SQL语句

mysql> show profile for query 14;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000074 |
| checking permissions | 0.000015 |
| Opening tables       | 0.000023 |
| init                 | 0.000029 |
| System lock          | 0.000013 |
| optimizing           | 0.000012 |
| statistics           | 0.000061 |
| preparing            | 0.000012 |
| executing            | 0.000005 |
| Sending data         | 0.000017 |
| end                  | 0.000005 |
| query end            | 0.000010 |
| closing tables       | 0.000009 |
| freeing items        | 0.000020 |
| cleaning up          | 0.000015 |
+----------------------+----------+
15 rows in set, 1 warning (0.00 sec)

mysql> 

6.查看SQL语句的CPU及资源情况

mysql> show profile cpu,source for query 14;
+----------------------+----------+----------+------------+-----------------------+----------------------+-------------+
| Status               | Duration | CPU_user | CPU_system | Source_function       | Source_file          | Source_line |
+----------------------+----------+----------+------------+-----------------------+----------------------+-------------+
| starting             | 0.000074 | 0.000046 |   0.000025 | NULL                  | NULL                 |        NULL |
| checking permissions | 0.000015 | 0.000008 |   0.000004 | check_access          | sql_authorization.cc |         809 |
| Opening tables       | 0.000023 | 0.000015 |   0.000008 | open_tables           | sql_base.cc          |        5753 |
| init                 | 0.000029 | 0.000019 |   0.000009 | handle_query          | sql_select.cc        |         128 |
| System lock          | 0.000013 | 0.000009 |   0.000005 | mysql_lock_tables     | lock.cc              |         330 |
| optimizing           | 0.000012 | 0.000007 |   0.000004 | optimize              | sql_optimizer.cc     |         158 |
| statistics           | 0.000061 | 0.000041 |   0.000022 | optimize              | sql_optimizer.cc     |         374 |
| preparing            | 0.000012 | 0.000007 |   0.000003 | optimize              | sql_optimizer.cc     |         482 |
| executing            | 0.000005 | 0.000003 |   0.000002 | exec                  | sql_executor.cc      |         126 |
| Sending data         | 0.000017 | 0.000011 |   0.000005 | exec                  | sql_executor.cc      |         202 |
| end                  | 0.000005 | 0.000003 |   0.000003 | handle_query          | sql_select.cc        |         206 |
| query end            | 0.000010 | 0.000007 |   0.000003 | mysql_execute_command | sql_parse.cc         |        4956 |
| closing tables       | 0.000009 | 0.000006 |   0.000003 | mysql_execute_command | sql_parse.cc         |        5009 |
| freeing items        | 0.000020 | 0.000013 |   0.000007 | mysql_parse           | sql_parse.cc         |        5622 |
| cleaning up          | 0.000015 | 0.000010 |   0.000005 | dispatch_command      | sql_parse.cc         |        1931 |
+----------------------+----------+----------+------------+-----------------------+----------------------+-------------+
15 rows in set, 1 warning (0.00 sec)

mysql> 
还可以使用下面的语句查看更多的信息:
mysql> show profile all for query 14;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SHOW PROFILES是MySQL提供的用于分析当前会话中SQL语句执行的资源消耗情况的工具。以下是使用SHOW PROFILES的步骤: 1. 确认是否开启SHOW PROFILES功能。可以通过执行以下语句查看当前是否开启了SHOW PROFILES功能: ``` show variables like 'profiling'; ``` 如果结果为'ON',则表示SHOW PROFILES功能已经开启;如果结果为'OFF',则表示SHOW PROFILES功能未开启。 2. 执行需要分析的SQL语句。在执行SQL语句之后,可以使用以下语句获取到最近执行的SQL语句Query_ID: ``` show profiles; ``` 3. 使用获取到的Query_ID进行SQL语句的分析。可以使用以下语句查看指定Query_ID的SQL语句执行的详细耗时、CPU和内存消耗: ``` show profile cpu,block io for query Query_ID; ``` 在这里,Query_ID是通过执行show profiles语句获取到的查询到的具体Query_ID。 请注意,SHOW PROFILES功能默认是关闭的,并且每次使用前都需要开启。SHOW PROFILES仅在当前会话中有效,不会影响其他会话。此外,SHOW PROFILES会保存最近执行的15条SQL语句的执行结果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mysql优化之show profile的使用及分析](https://blog.csdn.net/weixin_44540711/article/details/125893388)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [MySQL调优利器【show profiles】](https://download.csdn.net/download/weixin_38565818/13687765)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值