【MySQL】如何使用SQL Profiler 性能分析器

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:  MySQL5.0.37版本以上支持profiling,通过使用profiling 功能可以查看到sql语句消耗资源的更详细的信息。通常我们使用explain 查看执行计划,结合profile 功能我们可以定位sql 执行过程中的瓶颈到底出现在哪里?...
 MySQL5.0.37版本以上支持profiling,通过使用profiling 功能可以查看到sql语句消耗资源的更详细的信息。通常我们使用explain 查看执行计划,结合profile 功能我们可以定位sql 执行过程中的瓶颈到底出现在哪里?
一 语法:
show profile 的语法格式如下: 
SHOW PROFILE [type [, type] … ] 
   [FOR QUERY n] 
   [LIMIT row_count [OFFSET offset]] 
type: 
 ALL      - displays all information
 BLOCK IO - displays counts for block input and output Operations
 CONTEXT SWITCHES - displays counts for voluntary and involuntary context switches
 IPC      - displays counts for messages sent and received
 MEMORY   - is not currently implemented
 PAGE FAULTS - displays counts for major and minor page faults
 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
 SWAPS    - displays swap counts
二 如何使用
默认情况下profiling 为0 ,
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)
开启profiling,则设置该值为1
mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)
查询一些sql 
mysql> SELECT t0.id AS id1,  t0.frozen AS frozen6, t0.gmt_create AS gmt_create9, t0.gmt_update AS gmt_update10 
    -> FROM tabname t0 WHERE t0.uid = '46061' LIMIT 1;
+------+---------+---------------------+---------------------+
| id1  | frozen6 | gmt_create9         | gmt_update10        |
+------+---------+---------------------+---------------------+
| 7922 |    0.00 | 2012-10-29 22:45:09 | 2012-10-31 15:23:24 |
+------+---------+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> show profiles;
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration   | Query                                                                                                                                                                                   |
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|        1 | 0.00048900 | SELECT t0.id AS id1,  t0.frozen AS frozen6, t0.gmt_create AS gmt_create9, t0.gmt_update AS gmt_update10 
FROM tabname t0 WHERE t0.uid = '46061' LIMIT 1                    |
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)
Query_ID --- 是执行sql 的编号,如果你执行了7个 ,show profiles的时候,ID 会显示1---7
Duration -----sql 的执行时间
Query      -----具体执行的sql 语句。
mysql> show profile for query 1;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| starting           | 0.000141 |
| Opening tables     | 0.000032 |
| System lock        | 0.000009 |
| Table lock         | 0.000013 |
| init               | 0.000031 |
| optimizing         | 0.000015 |
| statistics         | 0.000074 |
| preparing          | 0.000020 |
| executing          | 0.000006 |
| Sending data       | 0.000088 |
| end                | 0.000007 |
| query end          | 0.000006 |
| freeing items      | 0.000035 |
| logging slow query | 0.000006 |
| cleaning up        | 0.000006 |
+--------------------+----------+
15 rows in set (0.00 sec)
executing,sending data都是很快的,因为该sql 的执行计划很正确了!
查看执行该sql所花费的 block io,cpu数据:
mysql> show profile BLOCK IO ,cpu for query 4;
+--------------------+----------+----------+------------+--------------+---------------+
| Status             | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+--------------------+----------+----------+------------+--------------+---------------+
| starting           | 0.000141 | 0.000000 |   0.000000 |            0 |             0 |
| Opening tables     | 0.000032 | 0.000000 |   0.000000 |            0 |             0 |
| System lock        | 0.000009 | 0.000000 |   0.000000 |            0 |             0 |
| Table lock         | 0.000013 | 0.000000 |   0.000000 |            0 |             0 |
| init               | 0.000031 | 0.000000 |   0.000000 |            0 |             0 |
| optimizing         | 0.000015 | 0.000000 |   0.000000 |            0 |             0 |
| statistics         | 0.000074 | 0.000000 |   0.000000 |            0 |             0 |
| preparing          | 0.000020 | 0.000000 |   0.000000 |            0 |             0 |
| executing          | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |
| Sending data       | 0.000088 | 0.000000 |   0.000000 |            0 |             0 |
| end                | 0.000007 | 0.000000 |   0.000000 |            0 |             0 |
| query end          | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |
| freeing items      | 0.000035 | 0.000000 |   0.000000 |            0 |             0 |
| logging slow query | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |
| cleaning up        | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |
+--------------------+----------+----------+------------+--------------+---------------+
15 rows in set (0.00 sec)

当然也可以使用
show profile SOURCE,MEMORY,CONTEXT SWITCHES for query N; 查看其它信息!
当sql 的性能出现问题之后,除了定位sql的执行计划之外,可以选择使用 profile 来对sql的瓶颈进行定位!
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
Linux Shell
【Linux 进程间通讯 管道】使用Linux管道进行linux进程间通信
【Linux 进程间通讯 管道】使用Linux管道进行linux进程间通信
202 1
|
关系型数据库 数据库 PostgreSQL
PostgreSQL数据库公开课视频及PGCE认证(第1期)(CUUG)(2020年)
一、PostgreSQL 12.2企业级应用公开课(1):备份恢复
1270 0
|
API 网络架构 数据格式
API接口详解及其在电子商务中的应用研究
本文介绍了API接口在电子商务中的重要性,详细阐述了API的定义、分类(RESTful, SOAP, GraphQL)和设计原则。通过实例展示了API如何促进数据交换和系统集成,如商品信息共享、订单自动化处理。同时,讨论了API安全性措施,并提供了一个Python代码示例来演示如何获取电商平台的商品信息。API在电子商务领域的应用对于提升业务效率和推动行业创新具有显著影响。
Qt程序打包发布记录使用windeployqt工具
Qt程序打包发布记录使用windeployqt工具
572 0
|
SQL 关系型数据库 MySQL
MySQL性能分析神器—Profiling
MySQL性能分析神器—Profiling
1375 0
MySQL性能分析神器—Profiling
数据结构单向链表和循环链表的插入 | 第二套
数据结构单向链表和循环链表的插入 | 第二套
126 0
|
小程序 JavaScript
微信小程序-监听屏幕滚动
微信小程序-监听屏幕滚动
447 0
|
弹性计算 负载均衡 容灾
阿里云弹性公网EIP是什么?详细介绍
阿里云eip是什么?阿里云百科分享弹性公网IP详细介绍,阿里云弹性公网EIP是什么意思?EIP是可以独立持有的公网IP地址,EIP可以和阿里云专有网络VPC类型的云服务器ECS、NAT网关、ENI网卡、私网负载均衡SLB等绑定,通过EIP可以让你的实例在公网提供服务。
367 0
阿里云弹性公网EIP是什么?详细介绍
|
存储 机器学习/深度学习 程序员
Golang数据类型之基本数据类型
Golang数据类型之基本数据类型
|
编解码 算法 C#
计算机动画基础 | 学习笔记
快速学习计算机动画基础,介绍了计算机动画基础系统机制, 以及在实际应用过程中如何使用。
计算机动画基础 | 学习笔记