MySQL使用profile分析语句性能消耗

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: MySQL使用profile分析语句性能消耗 --查看profile是否开启mysql> show variables like '%profil%';+------------------------+-...

MySQL使用profile分析语句性能消耗



--查看profile是否开启
mysql> show variables like '%profil%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| profiling              | OFF   |         --开启SQL语句剖析功能                                                  
| profiling_history_size | 15    |         --设置保留profiling的数目,缺省为15,范围为0至100,为0时将禁用profiling
+------------------------+-------+
2 rows in set (0.00 sec)

--基于会话级别开启
mysql> set profiling = 1;          --关闭则用set profiling = off
Query OK, 0 rows affected (0.00 sec)
 
mysql> show variables like '%profil%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| profiling              | ON    |
| profiling_history_size | 15    |
+------------------------+-------+
2 rows in set (0.00 sec)

mysql>  select distinct d.account,a.server_id from tab_appserver_user a
    -> inner join tab_department_parent b on a.key_id = b.parent_id
    -> inner join tab_department_member c on b.department_id = c.department_id and c.state=1
    -> and c.isdefault=1  inner join tab_user_info d on c.user_id = d.user_id and d.state=1
    -> where a.type=1  
    -> union                   
    -> select distinct b.account,a.server_id from tab_appserver_user a
    -> inner join tab_user_info b on a.key_id = b.user_id and b.state=1
    -> where a.type=0;

--显示缓存的profile    
mysql> show profiles;
+----------+------------+-------------------------------------------------------+
| Query_ID | Duration   | Query
         1 | 0.86754250 | select distinct d.account,a.server_id from tab_appserver_user a
+----------+------------+-------------------------------------------------------+
4 rows in set (0.00 sec)
从上面可以看到时间的消耗为0.8秒

以下是具体的消耗,进行详细的列出
mysql> show profile for query 1;       --1是query_id
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000018 |
| checking query cache for query | 0.000099 |
| Opening tables                 | 0.000963 |
| System lock                    | 0.000015 |
| Table lock                     | 0.000169 |
| optimizing                     | 0.000020 |
| statistics                     | 0.000027 |
| preparing                      | 0.000018 |
| Creating tmp table             | 0.000055 |
| executing                      | 0.000003 |
| Copying to tmp table           | 0.704845 |       --最主要的消耗点
| Sending data                   | 0.130039 |
| optimizing                     | 0.000029 |
| statistics                     | 0.000029 |
| preparing                      | 0.000020 |
| Creating tmp table             | 0.000142 |
| executing                      | 0.000003 |
| Copying to tmp table           | 0.000086 |
| Sending data                   | 0.000067 |
| optimizing                     | 0.000004 |
| statistics                     | 0.000005 |
| preparing                      | 0.000005 |
| executing                      | 0.000002 |
| Sending data                   | 0.023963 |
| removing tmp table             | 0.003420 |
| Sending data                   | 0.000005 |
| removing tmp table             | 0.003308 |
| Sending data                   | 0.000006 |
| removing tmp table             | 0.000007 |
| Sending data                   | 0.000009 |
| query end                      | 0.000003 |
| freeing items                  | 0.000144 |
| storing result in query cache  | 0.000011 |
| logging slow query             | 0.000003 |
| cleaning up                    | 0.000006 |
+--------------------------------+----------+
35 rows in set (0.00 sec)

--查看cpu的消耗情况    
mysql> show profile cpu for query 1;
+--------------------------------+----------+----------+------------+
| Status                         | Duration | CPU_user | CPU_system |
+--------------------------------+----------+----------+------------+
| starting                       | 0.000018 |     NULL |       NULL |
| checking query cache for query | 0.000099 |     NULL |       NULL |
| Opening tables                 | 0.000963 |     NULL |       NULL |
| System lock                    | 0.000015 |     NULL |       NULL |
| Table lock                     | 0.000169 |     NULL |       NULL |
| optimizing                     | 0.000020 |     NULL |       NULL |
| statistics                     | 0.000027 |     NULL |       NULL |
| preparing                      | 0.000018 |     NULL |       NULL |
| Creating tmp table             | 0.000055 |     NULL |       NULL |
| executing                      | 0.000003 |     NULL |       NULL |
| Copying to tmp table           | 0.704845 |     NULL |       NULL |  --此项消耗cpu最多
| Sending data                   | 0.130039 |     NULL |       NULL |
| optimizing                     | 0.000029 |     NULL |       NULL |
| statistics                     | 0.000029 |     NULL |       NULL |
| preparing                      | 0.000020 |     NULL |       NULL |
| Creating tmp table             | 0.000142 |     NULL |       NULL |
| executing                      | 0.000003 |     NULL |       NULL |
| Copying to tmp table           | 0.000086 |     NULL |       NULL |
| Sending data                   | 0.000067 |     NULL |       NULL |
| optimizing                     | 0.000004 |     NULL |       NULL |
| statistics                     | 0.000005 |     NULL |       NULL |
| preparing                      | 0.000005 |     NULL |       NULL |
| executing                      | 0.000002 |     NULL |       NULL |
| Sending data                   | 0.023963 |     NULL |       NULL |
| removing tmp table             | 0.003420 |     NULL |       NULL |
| Sending data                   | 0.000005 |     NULL |       NULL |
| removing tmp table             | 0.003308 |     NULL |       NULL |
| Sending data                   | 0.000006 |     NULL |       NULL |
| removing tmp table             | 0.000007 |     NULL |       NULL |
| Sending data                   | 0.000009 |     NULL |       NULL |
| query end                      | 0.000003 |     NULL |       NULL |
| freeing items                  | 0.000144 |     NULL |       NULL |
| storing result in query cache  | 0.000011 |     NULL |       NULL |
| logging slow query             | 0.000003 |     NULL |       NULL |
| cleaning up                    | 0.000006 |     NULL |       NULL |
+--------------------------------+----------+----------+------------+
35 rows in set (0.00 sec)

--查看内存消耗
mysql> show profile memory for query 1;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000018 |
| checking query cache for query | 0.000099 |
| Opening tables                 | 0.000963 |
| System lock                    | 0.000015 |
| Table lock                     | 0.000169 |
| optimizing                     | 0.000020 |
| statistics                     | 0.000027 |
| preparing                      | 0.000018 |
| Creating tmp table             | 0.000055 |
| executing                      | 0.000003 |
| Copying to tmp table           | 0.704845 |
| Sending data                   | 0.130039 |
| optimizing                     | 0.000029 |
| statistics                     | 0.000029 |
| preparing                      | 0.000020 |
| Creating tmp table             | 0.000142 |
| executing                      | 0.000003 |
| Copying to tmp table           | 0.000086 |
| Sending data                   | 0.000067 |
| optimizing                     | 0.000004 |
| statistics                     | 0.000005 |
| preparing                      | 0.000005 |
| executing                      | 0.000002 |
| Sending data                   | 0.023963 |
| removing tmp table             | 0.003420 |
| Sending data                   | 0.000005 |
| removing tmp table             | 0.003308 |
| Sending data                   | 0.000006 |
| removing tmp table             | 0.000007 |
| Sending data                   | 0.000009 |
| query end                      | 0.000003 |
| freeing items                  | 0.000144 |
| storing result in query cache  | 0.000011 |
| logging slow query             | 0.000003 |
| cleaning up                    | 0.000006 |
+--------------------------------+----------+

--查看io及cpu的消耗
mysql> show profile block io,cpu for query 1;
+--------------------------------+----------+----------+------------+--------------+---------------+
| Status                         | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+--------------------------------+----------+----------+------------+--------------+---------------+
| starting                       | 0.000018 |     NULL |       NULL |         NULL |          NULL |
| checking query cache for query | 0.000099 |     NULL |       NULL |         NULL |          NULL |
| Opening tables                 | 0.000963 |     NULL |       NULL |         NULL |          NULL |
| System lock                    | 0.000015 |     NULL |       NULL |         NULL |          NULL |
| Table lock                     | 0.000169 |     NULL |       NULL |         NULL |          NULL |
| optimizing                     | 0.000020 |     NULL |       NULL |         NULL |          NULL |
| statistics                     | 0.000027 |     NULL |       NULL |         NULL |          NULL |
| preparing                      | 0.000018 |     NULL |       NULL |         NULL |          NULL |
| Creating tmp table             | 0.000055 |     NULL |       NULL |         NULL |          NULL |
| executing                      | 0.000003 |     NULL |       NULL |         NULL |          NULL |
| Copying to tmp table           | 0.704845 |     NULL |       NULL |         NULL |          NULL |
| Sending data                   | 0.130039 |     NULL |       NULL |         NULL |          NULL |
| optimizing                     | 0.000029 |     NULL |       NULL |         NULL |          NULL |
| statistics                     | 0.000029 |     NULL |       NULL |         NULL |          NULL |
| preparing                      | 0.000020 |     NULL |       NULL |         NULL |          NULL |
| Creating tmp table             | 0.000142 |     NULL |       NULL |         NULL |          NULL |
| executing                      | 0.000003 |     NULL |       NULL |         NULL |          NULL |
| Copying to tmp table           | 0.000086 |     NULL |       NULL |         NULL |          NULL |
| Sending data                   | 0.000067 |     NULL |       NULL |         NULL |          NULL |
| optimizing                     | 0.000004 |     NULL |       NULL |         NULL |          NULL |
| statistics                     | 0.000005 |     NULL |       NULL |         NULL |          NULL |
| preparing                      | 0.000005 |     NULL |       NULL |         NULL |          NULL |
| executing                      | 0.000002 |     NULL |       NULL |         NULL |          NULL |
| Sending data                   | 0.023963 |     NULL |       NULL |         NULL |          NULL |
| removing tmp table             | 0.003420 |     NULL |       NULL |         NULL |          NULL |
| Sending data                   | 0.000005 |     NULL |       NULL |         NULL |          NULL |
| removing tmp table             | 0.003308 |     NULL |       NULL |         NULL |          NULL |
| Sending data                   | 0.000006 |     NULL |       NULL |         NULL |          NULL |
| removing tmp table             | 0.000007 |     NULL |       NULL |         NULL |          NULL |
| Sending data                   | 0.000009 |     NULL |       NULL |         NULL |          NULL |
| query end                      | 0.000003 |     NULL |       NULL |         NULL |          NULL |
| freeing items                  | 0.000144 |     NULL |       NULL |         NULL |          NULL |
| storing result in query cache  | 0.000011 |     NULL |       NULL |         NULL |          NULL |
| logging slow query             | 0.000003 |     NULL |       NULL |         NULL |          NULL |
| cleaning up                    | 0.000006 |     NULL |       NULL |         NULL |          NULL |
+--------------------------------+----------+----------+------------+--------------+---------------+
35 rows in set (0.00 sec)


--使用查询语句对消耗进行排序

mysql> SELECT STATE,
    ->        SUM(DURATION) AS TOTAL_R,
    ->        ROUND(100 * SUM(DURATION) /
    ->              (SELECT SUM(DURATION)
    ->                 FROM INFORMATION_SCHEMA.PROFILING
    ->                WHERE QUERY_ID = 1),
    ->              2) AS PCT_R,
    ->        COUNT(*) AS CALLS,
    ->        SUM(DURATION) / COUNT(*) AS "R/Call"
    ->   FROM INFORMATION_SCHEMA.PROFILING
    ->  WHERE QUERY_ID = 1
    ->  GROUP BY STATE
    ->  ORDER BY TOTAL_R DESC;

+--------------------------------+----------+-------+-------+--------------+
| STATE                          | Total_R  | Pct_R | Calls | R/Call       |
+--------------------------------+----------+-------+-------+--------------+
| Copying to tmp table           | 0.704931 | 81.26 |     2 | 0.3524655000 |
| Sending data                   | 0.154089 | 17.76 |     6 | 0.0256815000 |
| removing tmp table             | 0.006735 |  0.78 |     3 | 0.0022450000 |
| Opening tables                 | 0.000963 |  0.11 |     1 | 0.0009630000 |
| Creating tmp table             | 0.000197 |  0.02 |     2 | 0.0000985000 |
| Table lock                     | 0.000169 |  0.02 |     1 | 0.0001690000 |
| freeing items                  | 0.000144 |  0.02 |     1 | 0.0001440000 |
| checking query cache for query | 0.000099 |  0.01 |     1 | 0.0000990000 |
| statistics                     | 0.000061 |  0.01 |     3 | 0.0000203333 |
| optimizing                     | 0.000053 |  0.01 |     3 | 0.0000176667 |
| preparing                      | 0.000043 |  0.00 |     3 | 0.0000143333 |
| starting                       | 0.000018 |  0.00 |     1 | 0.0000180000 |
| System lock                    | 0.000015 |  0.00 |     1 | 0.0000150000 |
| storing result in query cache  | 0.000011 |  0.00 |     1 | 0.0000110000 |
| executing                      | 0.000008 |  0.00 |     3 | 0.0000026667 |
| cleaning up                    | 0.000006 |  0.00 |     1 | 0.0000060000 |
| logging slow query             | 0.000003 |  0.00 |     1 | 0.0000030000 |
| query end                      | 0.000003 |  0.00 |     1 | 0.0000030000 |
+--------------------------------+----------+-------+-------+--------------+
18 rows in set (0.01 sec)


--最后说明:

    profile是一个非常量化的




>

>

>

>

>

>

>

>

   


























 

    

    

     

>

   

     

               

            

             

  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

>>>>>
>
>
>
 
  

    

     

 
  

 

         

   

   
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

  •  

  •    

  •  

  •  

  •    

  •    

  •    

  •    

  •    

  •  

  •    



    

img_e3029f287d989cd04bd75432ecc1c172.png
DBA笔试面试讲解
欢迎与我联系

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
10天前
|
关系型数据库 MySQL 索引
mysql 分析5语句的优化--索引添加删除
mysql 分析5语句的优化--索引添加删除
11 0
|
2月前
|
安全 关系型数据库 MySQL
mysql安全性能
mysql安全性能
37 10
|
21天前
|
SQL 关系型数据库 MySQL
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
94 0
|
10天前
|
SQL 缓存 关系型数据库
mysql性能优化-慢查询分析、优化索引和配置
mysql性能优化-慢查询分析、优化索引和配置
76 0
|
16天前
|
存储 关系型数据库 MySQL
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)
|
16天前
|
缓存 关系型数据库 MySQL
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
|
16天前
|
缓存 关系型数据库 MySQL
MySQL 查询优化:提速查询效率的13大秘籍(索引设计、查询优化、缓存策略、子查询优化以及定期表分析和优化)(中)
MySQL 查询优化:提速查询效率的13大秘籍(索引设计、查询优化、缓存策略、子查询优化以及定期表分析和优化)(中)
|
9天前
|
关系型数据库 MySQL 数据库
MySQL之show profile相关总结
综上所述,`SHOW PROFILE`是MySQL提供的一个用于查询性能分析的工具,可以帮助开发人员定位查询性能问题,并进行优化。通过分析每个阶段的执行时间和资源消耗情况,可以更好地理解查询的执行过程,从而提升数据库性能。
11 0
|
18天前
|
SQL 关系型数据库 MySQL
【MySQL】慢SQL分析流程
【4月更文挑战第1天】【MySQL】慢SQL分析流程
|
21天前
|
SQL 关系型数据库 MySQL
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(数据恢复补充篇)(一)
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(数据恢复补充篇)
29 0