mysql 下 show profile 备忘

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: mysql 中可利用  show profile 进行性能分析 show profile 功能默认状态下为关闭, 因消耗一定资源   查询方法 mysql> select @@profiling; +-------------+ | @@profiling | +-------------+ |           0 |  0 = 关闭  1 = 启用 +----

mysql 中可利用  show profile 进行性能分析

show profile 功能默认状态下为关闭, 因消耗一定资源

 

查询方法

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |  0 = 关闭  1 = 启用
+-------------+
1 row in set (0.00 sec)

打开方法

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

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

启用 profiling 功能后就可以对 SQL 进行分析。

为避免之前已经把 SQL 存放在 QCACHE 中, 建议在执行 SQL 时, 强制 SELECT 语句不进行 QCACHE 检测

如下面 SQL

mysql> select sql_no_cache distinct prewarebro0_.ID as ID471_, prewarebro0_.SPECIFICATION_ID as SPECIFIC2_471_, prewarebro0_.SPECIES_ID as SPECIES3_471_, prewarebro0_.SPECIES_CODE as SPECIES4_471_, prewarebro0_.DETAIL_URL as DETAIL5_471_, prewarebro0_.SALEPRICE as SALEPRICE471_, prewarebro0_.TYPE as TYPE471_, prewarebro0_.CREATE_TIME as CREATE8_471_, prewarebro0_.PICTUREURL as PICTUREURL471_, prewarebro0_.USER_KO as USER10_471_ from PRE_WARE_BROSWERS prewarebro0_ cross join WARE_DIRECTORY_SUMA waredirect1_ cross join WARE_CON_DIECTORY warecondir2_ cross join WARE_MOUNTED waremounte3_ cross join WARE_CATALOG specificat4_ where waredirect1_.ID=warecondir2_.SUMA_ID and warecondir2_.ID=waremounte3_.CON_CODE and prewarebro0_.SPECIFICATION_ID=waremounte3_.MOUNT_CODE and prewarebro0_.SPECIFICATION_ID=specificat4_.ID and waredirect1_.STATUS='1' and waremounte3_.STATUS='1' and specificat4_.STATUS=1 and prewarebro0_.USER_KO='38a89ba4e0595ff0d06ab0744f64344f' order by prewarebro0_.CREATE_TIME desc limit 3 ;
Empty set (0.51 sec)

当执行完 SQL 之后,可以对该 SQL 进行分析, 如

mysql> show profile;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000793 |
| checking permissions | 0.000012 |
| checking permissions | 0.000006 |
| checking permissions | 0.000005 |
| checking permissions | 0.000005 |
| checking permissions | 0.000010 |
| Opening tables       | 0.000132 |
| System lock          | 0.000047 |
| init                 | 0.000160 |
| optimizing           | 0.000084 |
| statistics           | 0.000264 |
| preparing            | 0.000146 |
| Creating tmp table   | 0.000100 |
| executing            | 0.000008 |
| Copying to tmp table | 0.515277 |
| Sorting result       | 0.000045 |
| Sending data         | 0.000039 |
| end                  | 0.000012 |
| removing tmp table   | 0.000019 |
| end                  | 0.000008 |
| query end            | 0.000014 |
| closing tables       | 0.000055 |
| freeing items        | 0.000087 |
| logging slow query   | 0.000005 |
| cleaning up          | 0.000014 |
+----------------------+----------+
25 rows in set (0.00 sec)

我们看到了, 明显就是 copying to tmp table 值比较不正常,不进行字面解释了

看看当前我们电脑缓存了多少可以进行分析的 SQL

mysql> show profiles;
+----------+------------+------------------------------------------+
| Query_ID | Duration   | Query                                    |
+----------+------------+------------------------------------------+
|        1 | 0.00026925 | select * from ware_stocks where id < 15  |
|        2 | 0.00032250 | SELECT DATABASE()                        |
|        3 | 0.00102075 | show databases                           |
|        4 | 0.00388575 | show tables                              |
|        5 | 0.00021200 | select * from ware_stocks where id < 15  |
|        6 | 0.01826000 | select * from ware_stocks where id < 55  |
|        7 | 0.02039825 | select * from ware_stocks where id < 155 |
|        8 | 0.03401675 | select * from ware_stocks where id > 3   |
+----------+------------+------------------------------------------+
8 rows in set (0.00 sec)

上面记录了一共 8 条 SQL 分析, 假如要分析某一条, 那么可以执行 (参考上表中 ID 值进行分析)

mysql> show profile for query 8;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000164 |
| Waiting for query cache lock   | 0.000010 |
| checking query cache for query | 0.000197 |
| checking permissions           | 0.000026 |
| Opening tables                 | 0.000094 |
| System lock                    | 0.000038 |
| Waiting for query cache lock   | 0.000095 |
| init                           | 0.000122 |
| optimizing                     | 0.000025 |
| statistics                     | 0.000073 |
| preparing                      | 0.000032 |
| executing                      | 0.000008 |
| Sending data                   | 0.010331 |
| Waiting for query cache lock   | 0.000020 |
| Sending data                   | 0.005885 |
| Waiting for query cache lock   | 0.000011 |
| Sending data                   | 0.005877 |
| Waiting for query cache lock   | 0.000010 |
| Sending data                   | 0.005998 |
| Waiting for query cache lock   | 0.000011 |
| Sending data                   | 0.004589 |
| end                            | 0.000022 |
| query end                      | 0.000031 |
| closing tables                 | 0.000050 |
| freeing items                  | 0.000038 |
| Waiting for query cache lock   | 0.000010 |
| freeing items                  | 0.000213 |
| Waiting for query cache lock   | 0.000009 |
| freeing items                  | 0.000005 |
| storing result in query cache  | 0.000009 |
| logging slow query             | 0.000006 |
| cleaning up                    | 0.000010 |
+--------------------------------+----------+
32 rows in set (0.01 sec)

另外,还可以独立地分析某项资源,方便参考

o ALL displays all information

o BLOCK IO displays counts for block input and output operations

o CONTEXT SWITCHESdisplays counts for voluntary and involuntary
  context switches

o CPU displays user and system CPU usage times

o IPC displays counts for messages sent and received

o MEMORY is not currently implemented

o PAGE FAULTS displays counts for major and minor page faults

o SOURCE displays the names of functions from the source code, together
  with the name and line number of the file in which the function
  occurs

o SWAPS displays swap counts

参考之前信息,我们看看 Copying to tmp table的IO情况

mysql> show profile block io for query 9;
+----------------------+----------+--------------+---------------+
| Status               | Duration | Block_ops_in | Block_ops_out |
+----------------------+----------+--------------+---------------+
| starting             | 0.000483 |            0 |             0 |
| checking permissions | 0.000012 |            0 |             0 |
| checking permissions | 0.000006 |            0 |             0 |
| checking permissions | 0.000006 |            0 |             0 |
| checking permissions | 0.000006 |            0 |             0 |
| checking permissions | 0.000010 |            0 |             0 |
| Opening tables       | 0.000162 |            0 |             0 |
| System lock          | 0.000056 |            0 |             0 |
| init                 | 0.000179 |            0 |             0 |
| optimizing           | 0.000074 |            0 |             0 |
| statistics           | 0.000225 |            0 |             0 |
| preparing            | 0.000173 |            0 |             0 |
| Creating tmp table   | 0.000127 |            0 |             0 |
| executing            | 0.000008 |            0 |             0 |
| Copying to tmp table | 0.837154 |            0 |             8 | 产生 8 次的 IO 了用于创建 TMP TABLE 了
| Sorting result       | 0.000059 |            0 |             0 |
| Sending data         | 0.000050 |            0 |             0 |
| end                  | 0.000017 |            0 |             0 |
| removing tmp table   | 0.000025 |            0 |             0 |
| end                  | 0.000012 |            0 |             0 |
| query end            | 0.000018 |            0 |             0 |
| closing tables       | 0.000071 |            0 |             0 |
| freeing items        | 0.000097 |            0 |             0 |
| logging slow query   | 0.000006 |            0 |             0 |
| cleaning up          | 0.000012 |            0 |             0 |
+----------------------+----------+--------------+---------------+
25 rows in set (0.00 sec)

参考 CPU

mysql> show profile CPU for query 9;
+----------------------+----------+----------+------------+
| Status               | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| starting             | 0.000483 | 0.000000 |   0.000000 |
| checking permissions | 0.000012 | 0.000000 |   0.000000 |
| checking permissions | 0.000006 | 0.000000 |   0.000000 |
| checking permissions | 0.000006 | 0.000000 |   0.000000 |
| checking permissions | 0.000006 | 0.000000 |   0.000000 |
| checking permissions | 0.000010 | 0.000000 |   0.000000 |
| Opening tables       | 0.000162 | 0.002000 |   0.000000 |
| System lock          | 0.000056 | 0.000000 |   0.000000 |
| init                 | 0.000179 | 0.000000 |   0.000000 |
| optimizing           | 0.000074 | 0.000000 |   0.000000 |
| statistics           | 0.000225 | 0.000000 |   0.000000 |
| preparing            | 0.000173 | 0.000000 |   0.000000 |
| Creating tmp table   | 0.000127 | 0.000000 |   0.000000 |
| executing            | 0.000008 | 0.000000 |   0.000000 |
| Copying to tmp table | 0.837154 | 1.166823 |   0.001000| 也严重消耗了 CPU
| Sorting result       | 0.000059 | 0.000000 |   0.000000 |
| Sending data         | 0.000050 | 0.000000 |   0.000000 |
| end                  | 0.000017 | 0.000000 |   0.000000 |
| removing tmp table   | 0.000025 | 0.000000 |   0.000000 |
| end                  | 0.000012 | 0.000000 |   0.000000 |
| query end            | 0.000018 | 0.000000 |   0.000000 |
| closing tables       | 0.000071 | 0.000000 |   0.000000 |
| freeing items        | 0.000097 | 0.000000 |   0.000000 |
| logging slow query   | 0.000006 | 0.000000 |   0.000000 |
| cleaning up          | 0.000012 | 0.000000 |   0.000000 |
+----------------------+----------+----------+------------+
25 rows in set (0.01 sec)

总结, 分析后,切记 set profiling = 0 , 另外可以配合  explain 更方便地对执行计划进行分析。

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
关系型数据库 MySQL 数据库
MySQL之show profile相关总结
综上所述,`SHOW PROFILE`是MySQL提供的一个用于查询性能分析的工具,可以帮助开发人员定位查询性能问题,并进行优化。通过分析每个阶段的执行时间和资源消耗情况,可以更好地理解查询的执行过程,从而提升数据库性能。
17 0
|
5月前
|
SQL 关系型数据库 MySQL
MySQL调优之show profile 应用
MySQL调优之show profile 应用
37 0
|
SQL 存储 监控
【MySQL从入门到精通】【高级篇】(二十二)慢查询日志分析,SHOW PROFILE查看SQL执行成本
上一篇文章我们介绍数据库的优化步骤【MySQL从入门到精通】【高级篇】(二十一)数据库优化步骤_查看系统性能参数,其中,说到了通过开启慢查询日志来分析慢查询的SQL。这篇文章就是具体来介绍如何开启慢查询日志以及如何分析慢查询日志。
370 0
【MySQL从入门到精通】【高级篇】(二十二)慢查询日志分析,SHOW PROFILE查看SQL执行成本
|
SQL 存储 监控
Mysql中 慢查询日志和show profile进行sql分析
MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中。
340 0
|
SQL 存储 JSON
《MySQL高级篇》七、性能分析工具的使用(慢查询日志 | EXPLAIN | SHOW PROFILING | 视图分析 )(四)
《MySQL高级篇》七、性能分析工具的使用(慢查询日志 | EXPLAIN | SHOW PROFILING | 视图分析 )
《MySQL高级篇》七、性能分析工具的使用(慢查询日志 | EXPLAIN | SHOW PROFILING | 视图分析 )(四)
|
SQL 存储 关系型数据库
《MySQL高级篇》七、性能分析工具的使用(慢查询日志 | EXPLAIN | SHOW PROFILING | 视图分析 )(三)
《MySQL高级篇》七、性能分析工具的使用(慢查询日志 | EXPLAIN | SHOW PROFILING | 视图分析 )
《MySQL高级篇》七、性能分析工具的使用(慢查询日志 | EXPLAIN | SHOW PROFILING | 视图分析 )(三)
|
SQL 存储 关系型数据库
《MySQL高级篇》七、性能分析工具的使用(慢查询日志 | EXPLAIN | SHOW PROFILING | 视图分析 )(二)
《MySQL高级篇》七、性能分析工具的使用(慢查询日志 | EXPLAIN | SHOW PROFILING | 视图分析 )
《MySQL高级篇》七、性能分析工具的使用(慢查询日志 | EXPLAIN | SHOW PROFILING | 视图分析 )(二)
|
监控 算法 安全
MySQL:5.6 大事务show engine innodb status故障一例
MySQL:5.6 大事务show engine innodb status故障一例
173 0
MySQL:5.6 大事务show engine innodb status故障一例
|
SQL 关系型数据库 MySQL
mysql show processlist Time为负数的思考
mysql show processlist Time为负数的思考
138 0
mysql show processlist Time为负数的思考
|
SQL 关系型数据库 MySQL
mysql show processlist Time为负数的思考
mysql show processlist Time为负数的思考
mysql show processlist Time为负数的思考