分析SQL执行带来的开销是优化SQL的重要手段。在MySQL数据库中,可以通过配置profiling参数来启用SQL剖析。该参数可以在全局和session级别来设置。对于全局级别则作用于整个MySQL实例,而session级别紧影响当前session。该参数开启后,后续执行的SQL语句都将记录其资源开销,诸如IO,上下文切换,CPU,Memory等等。根据这些开销进一步分析当前SQL瓶颈从而进行优化与调整。本文描述了如何使用MySQL profile,不涉及具体的样例分析。

1、有关profile的描述

复制代码代码如下:

--当前版本  
root@localhost[sakila]> show variables like 'version';  
+---------------+---------------------------------------+  
| Variable_name | Value                                 |  
+---------------+---------------------------------------+  
| version       | 5.6.17-enterprise-commercial-advanced |  
+---------------+---------------------------------------+  
  
--查看profiling系统变量  
root@localhost[sakila]> show variables like '%profil%';  
+------------------------+-------+  
| Variable_name          | Value |  
+------------------------+-------+  
| have_profiling         | YES   |   --只读变量,用于控制是否由系统变量开启或禁用profiling  
| profiling              | OFF   |   --开启SQL语句剖析功能  
| profiling_history_size | 15    |   --设置保留profiling的数目,缺省为15,范围为0至100,为0时将禁用profiling  
+------------------------+-------+  
  
profiling [539]  
If set to 0 or OFF (the default), statement profiling is disabled. If set to 1 or ON, statement prof  
is enabled and the SHOW PROFILE and SHOW PROFILES statements provide access to prof  
information. See Section 13.7.5.32, “SHOW PROFILES Syntax”.  
  
This variable is deprecated in MySQL 5.6.8 and will be removed in a future MySQL release.  
profiling_history_size [539]  
The number of statements for which to maintain profiling information if profiling [539] is  
enabled. The default value is 15. The maximum value is 100. Setting the value to 0 effectively  
disables profiling. See Section 13.7.5.32, “SHOW PROFILES Syntax”.  
This variable is deprecated in MySQL 5.6.8 and will be removed in a future MySQL release.  
  
  
--获取profile的帮助  
root@localhost[sakila]> help profile;  
Name: 'SHOW PROFILE'  
Description:  
Syntax:  
SHOW PROFILE [type [, type] ... ]  
    [FOR QUERY n]  
    [LIMIT row_count [OFFSET offset]]  
  
type:  
    ALL                --显示所有的开销信息  
  | BLOCK IO           --显示块IO相关开销  
  | CONTEXT SWITCHES   --上下文切换相关开销  
  | CPU                --显示CPU相关开销信息  
  | IPC                --显示发送和接收相关开销信息  
  | MEMORY             --显示内存相关开销信息  
  | PAGE FAULTS        --显示页面错误相关开销信息  
  | SOURCE             --显示和Source_function,Source_file,Source_line相关的开销信息  
  | SWAPS              --显示交换次数相关开销的信息   
  
The SHOW PROFILE and SHOW PROFILES statements display profiling  
information that indicates resource usage for statements executed  
during the course of the current session.  
  
*Note*: These statements are deprecated as of MySQL 5.6.7 and will be  
removed in a future MySQL release. Use the Performance Schema instead;  
see http://dev.mysql.com/doc/refman/5.6/en/performance-schema.html.  
--上面描述从5.6.7开始该命令将会被移除,用Performance Schema instead代替  
--在Oracle数据库中,是通过autotrace来剖析单条SQL并获取真实的执行计划以及其开销信息  

2、开启porfiling

复制代码代码如下:

--启用session级别的profiling  
root@localhost[sakila]> set profiling=1;  
Query OK, 0 rows affected, 1 warning (0.00 sec)  
  
--验证修改后的结果  
root@localhost[sakila]> show variables like '%profil%';  
+------------------------+-------+  
| Variable_name          | Value |  
+------------------------+-------+  
| have_profiling         | YES   |  
| profiling              | ON    |  
| profiling_history_size | 15    |  
+------------------------+-------+  
  
--发布SQL查询  
root@localhost[sakila]> select count(*) from customer;  
+----------+  
| count(*) |  
+----------+  
|      599 |  
+----------+  
  
--查看当前session所有已产生的profile  
root@localhost[sakila]> show profiles;  
+----------+------------+--------------------------------+  
| Query_ID | Duration   | Query                          |  
+----------+------------+--------------------------------+  
|        1 | 0.00253600 | show variables like '%profil%' |  
|        2 | 0.00138150 | select count(*) from customer  |  
+----------+------------+--------------------------------+  
2 rows in set, 1 warning (0.01 sec)  
  
--我们看到有2个warning,之前一个,现在一个  
root@localhost[sakila]> show warnings;    --下面的结果表明SHOW PROFILES将来会被Performance Schema替换掉  
+---------+------+--------------------------------------------------------------------------------------------------------------+  
| Level   | Code | Message                                                                                                      |  
+---------+------+--------------------------------------------------------------------------------------------------------------+  
| Warning | 1287 | 'SHOW PROFILES' is deprecated and will be removed in a future release. Please use Performance Schema instead |  
+---------+------+--------------------------------------------------------------------------------------------------------------+  

3、获取SQL语句的开销信息

复制代码代码如下:

--可以直接使用show profile来查看上一条SQL语句的开销信息  
--注,show profile之类的语句不会被profiling,即自身不会产生Profiling  
--我们下面的这个show profile查看的是show warnings产生的相应开销  
root@localhost[sakila]> show profile;    
+----------------+----------+  
| Status         | Duration |  
+----------------+----------+  
| starting       | 0.000141 |  
| query end      | 0.000058 |  
| closing tables | 0.000014 |  
| freeing items  | 0.001802 |  
| cleaning up    | 0.000272 |  
+----------------+----------+  
  
--如下面的查询show warnings被添加到profiles  
root@localhost[sakila]> show profiles;  
+----------+------------+--------------------------------+  
| Query_ID | Duration   | Query                          |  
+----------+------------+--------------------------------+  
|        1 | 0.00253600 | show variables like '%profil%' |  
|        2 | 0.00138150 | select count(*) from customer  |  
|        3 | 0.00228600 | show warnings                  |  
+----------+------------+--------------------------------+  
  
--获取指定查询的开销  
root@localhost[sakila]> show profile for query 2;  
+----------------------+----------+  
| Status               | Duration |  
+----------------------+----------+  
| starting             | 0.000148 |  
| checking permissions | 0.000014 |  
| Opening tables       | 0.000047 |  
| init                 | 0.000023 |  
| System lock          | 0.000035 |  
| optimizing           | 0.000012 |  
| statistics           | 0.000019 |  
| preparing            | 0.000014 |  
| executing            | 0.000006 |  
| Sending data         | 0.000990 |  
| end                  | 0.000010 |  
| query end            | 0.000011 |  
| closing tables       | 0.000010 |  
| freeing items        | 0.000016 |  
| cleaning up          | 0.000029 |  
+----------------------+----------+  
  
--查看特定部分的开销,如下为CPU部分的开销  
root@localhost[sakila]> show profile cpu for query 2 ;  
+----------------------+----------+----------+------------+  
| Status               | Duration | CPU_user | CPU_system |  
+----------------------+----------+----------+------------+  
| starting             | 0.000148 | 0.000000 |   0.000000 |  
| checking permissions | 0.000014 | 0.000000 |   0.000000 |  
| Opening tables       | 0.000047 | 0.000000 |   0.000000 |  
| init                 | 0.000023 | 0.000000 |   0.000000 |  
| System lock          | 0.000035 | 0.000000 |   0.000000 |  
| optimizing           | 0.000012 | 0.000000 |   0.000000 |  
| statistics           | 0.000019 | 0.000000 |   0.000000 |  
| preparing            | 0.000014 | 0.000000 |   0.000000 |  
| executing            | 0.000006 | 0.000000 |   0.000000 |  
| Sending data         | 0.000990 | 0.001000 |   0.000000 |  
| end                  | 0.000010 | 0.000000 |   0.000000 |  
| query end            | 0.000011 | 0.000000 |   0.000000 |  
| closing tables       | 0.000010 | 0.000000 |   0.000000 |  
| freeing items        | 0.000016 | 0.000000 |   0.000000 |  
| cleaning up          | 0.000029 | 0.000000 |   0.000000 |  
+----------------------+----------+----------+------------+  
  
--如下为MEMORY部分的开销  
root@localhost[sakila]> show profile memory for query 2 ;  
+----------------------+----------+  
| Status               | Duration |  
+----------------------+----------+  
| starting             | 0.000148 |  
| checking permissions | 0.000014 |  
| Opening tables       | 0.000047 |  
| init                 | 0.000023 |  
| System lock          | 0.000035 |  
| optimizing           | 0.000012 |  
| statistics           | 0.000019 |  
| preparing            | 0.000014 |  
| executing            | 0.000006 |  
| Sending data         | 0.000990 |  
| end                  | 0.000010 |  
| query end            | 0.000011 |  
| closing tables       | 0.000010 |  
| freeing items        | 0.000016 |  
| cleaning up          | 0.000029 |  
+----------------------+----------+  
  
--同时查看不同资源开销  
root@localhost[sakila]> show profile block io,cpu for query 2;  
+----------------------+----------+----------+------------+--------------+---------------+  
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |  
+----------------------+----------+----------+------------+--------------+---------------+  
| starting             | 0.000148 | 0.000000 |   0.000000 |            0 |             0 |  
| checking permissions | 0.000014 | 0.000000 |   0.000000 |            0 |             0 |  
| Opening tables       | 0.000047 | 0.000000 |   0.000000 |            0 |             0 |  
| init                 | 0.000023 | 0.000000 |   0.000000 |            0 |             0 |  
| System lock          | 0.000035 | 0.000000 |   0.000000 |            0 |             0 |  
| optimizing           | 0.000012 | 0.000000 |   0.000000 |            0 |             0 |  
| statistics           | 0.000019 | 0.000000 |   0.000000 |            0 |             0 |  
| preparing            | 0.000014 | 0.000000 |   0.000000 |            0 |             0 |  
| executing            | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |  
| Sending data         | 0.000990 | 0.001000 |   0.000000 |            0 |             0 |  
| end                  | 0.000010 | 0.000000 |   0.000000 |            0 |             0 |  
| query end            | 0.000011 | 0.000000 |   0.000000 |            0 |             0 |  
| closing tables       | 0.000010 | 0.000000 |   0.000000 |            0 |             0 |  
| freeing items        | 0.000016 | 0.000000 |   0.000000 |            0 |             0 |  
| cleaning up          | 0.000029 | 0.000000 |   0.000000 |            0 |             0 |  
+----------------------+----------+----------+------------+--------------+---------------+  
  
  
--下面的SQL语句用于查询query_id为2的SQL开销,且按最大耗用时间倒序排列  
root@localhost[sakila]> set @query_id=2;  
  
root@localhost[sakila]> SELECT STATE, SUM(DURATION) AS Total_R,  
    ->   ROUND(  
    ->        100 * SUM(DURATION) /  
    ->           (SELECT SUM(DURATION)  
    ->            FROM INFORMATION_SCHEMA.PROFILING  
    ->            WHERE QUERY_ID = @query_id  
    ->        ), 2) AS Pct_R,  
    ->     COUNT(*) AS Calls,  
    ->     SUM(DURATION) / COUNT(*) AS "R/Call"  
    ->  FROM INFORMATION_SCHEMA.PROFILING  
    ->  WHERE QUERY_ID = @query_id  
    ->  GROUP BY STATE  
    ->  ORDER BY Total_R DESC;  
+----------------------+----------+-------+-------+--------------+  
| STATE                | Total_R  | Pct_R | Calls | R/Call       |  
+----------------------+----------+-------+-------+--------------+  
| Sending data         | 0.000990 | 71.53 |     1 | 0.0009900000 |--最大耗用时间部分为发送数据  
| starting             | 0.000148 | 10.69 |     1 | 0.0001480000 |  
| Opening tables       | 0.000047 |  3.40 |     1 | 0.0000470000 |  
| System lock          | 0.000035 |  2.53 |     1 | 0.0000350000 |  
| cleaning up          | 0.000029 |  2.10 |     1 | 0.0000290000 |  
| init                 | 0.000023 |  1.66 |     1 | 0.0000230000 |  
| statistics           | 0.000019 |  1.37 |     1 | 0.0000190000 |  
| freeing items        | 0.000016 |  1.16 |     1 | 0.0000160000 |  
| preparing            | 0.000014 |  1.01 |     1 | 0.0000140000 |  
| checking permissions | 0.000014 |  1.01 |     1 | 0.0000140000 |  
| optimizing           | 0.000012 |  0.87 |     1 | 0.0000120000 |  
| query end            | 0.000011 |  0.79 |     1 | 0.0000110000 |  
| end                  | 0.000010 |  0.72 |     1 | 0.0000100000 |  
| closing tables       | 0.000010 |  0.72 |     1 | 0.0000100000 |  
| executing            | 0.000006 |  0.43 |     1 | 0.0000060000 |  
+----------------------+----------+-------+-------+--------------+  
  
--开启profiling后,我们可以通过show profile等方式查看,其实质是这些开销信息被记录到information_schema.profiling表  
--如下面的查询,部分信息省略  
profiling  
root@localhost[information_schema]> select * from profiling limit 3,3\G;  
*************************** 1. row ***************************  
           QUERY_ID: 1  
                SEQ: 5  
              STATE: init  
           DURATION: 0.000020  
           CPU_USER: 0.000000  
         CPU_SYSTEM: 0.000000  
  CONTEXT_VOLUNTARY: 0  
CONTEXT_INVOLUNTARY: 0  
       BLOCK_OPS_IN: 0  
      BLOCK_OPS_OUT: 0  
      MESSAGES_SENT: 0  
  MESSAGES_RECEIVED: 0  
  PAGE_FAULTS_MAJOR: 0  
  PAGE_FAULTS_MINOR: 0  
              SWAPS: 0  
    SOURCE_FUNCTION: mysql_prepare_select  
        SOURCE_FILE: sql_select.cc  
        SOURCE_LINE: 1050  
  
--停止profile,可以设置profiling参数,或者在session退出之后,profiling会被自动关闭  
root@localhost[sakila]> set profiling=off;  
Query OK, 0 rows affected, 1 warning (0.00 sec)   

MySQL性能分析工具profile使用教程相关推荐

  1. Windows phone 7应用之代码性能分析工具——Profile.

    前端时间断断续续的写了几篇关于Windows phone 7本地数据库访问文章. 最近在Window phone 7 APPStore上注册Apphub账号看到很多很有意思的小应用, 也结识几个专职做 ...

  2. mysql 性能分析_十大MySQL性能分析工具汇总!专治MySQL性能瓶颈

    前言 MySQL 数据库最常见的两个瓶颈是CPU和I/O的瓶颈.CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据时候,磁盘I/O瓶颈发生在装入数据远大于内存容量的时候. MySQL数据库性能 ...

  3. MySQL性能分析工具的使用:慢查询日志、EXPLAN的使用、分析优化器执行计划:trace、MySQL监控分析视图-sys schema

    文章目录 1.数据库服务器的优化步骤 2.查看系统性能参数 2.1 语法 2.2 常用参数 3.统计SQL的查询成本:last_query_cost 4.定位执行慢的SQL:慢查询日志 4.1 慢查询 ...

  4. mysql性能分析工具profiling_Mysql系列(十)—— 性能分析工具profiling

    explain是从mysql怎样解析执行sql的角度分析sql优劣.profiling是从sql执行时资源使用情况的角度来分析sql. 分析SQL执行带来的开销是优化SQL的重要手段.在MySQL数据 ...

  5. mysql性能分析工具soar使用

    目录 soar介绍 安装 windows安装 工具使用 soar介绍 soar是小米开源的一个对 SQL 进行优化和改写的自动化工具. 跨平台支持(支持 Linux, Mac 环境,Windows 环 ...

  6. MySQL性能分析工具

    第一节 统计SQL执行成本 统计SQL的查询成本: last_query_cost 一条SQL查询语句在执行前需要确定查询执行计划,如果存在多种执行计划的话,MySQL会计算每个执行计划所需要的成本, ...

  7. php性能分析工具XHProf安装配置使用教程(linux精华版)

    XHProf是一个分层PHP性能分析工具.它报告函数级别的请求次数和各种指标,包括阻塞时间,CPU时间和内存使用情况.一个函数的开销,可细分成调用者和被调用者的开销,XHProf数据收集阶段,它记录调 ...

  8. 【宋红康 MySQL数据库 】【高级篇】【12】性能分析工具的使用

    持续学习&持续更新中- 学习态度:守破离 [宋红康 MySQL数据库 ][高级篇][12]性能分析工具的使用 数据库服务器的优化步骤 查看系统性能参数 统计SQL的查询成本:last_quer ...

  9. Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)

    前 言 ?? 作者简介:,长跑型选手,立志坚持写10年博客,专注于java后端 ?? 专栏简介:mysql进阶,主要讲解mysql数据库进阶知识,包括索引.数据库调优.分库分表等 ?? 文章简介:本文 ...

最新文章

  1. Linux静态/动态链接库的创建和使用
  2. axure8 事件改变样式_15. 教你零基础搭建小程序:小程序事件绑定(2)
  3. 神经网络中归一化的重要作用
  4. http请求响应的组成部分的介绍 用cherome查看请求响应内容 curl命令行的使用
  5. 875. 爱吃香蕉的珂珂(二分)
  6. redis本地及远程登录
  7. 让Android Studio支持系统签名(证书)
  8. php 字符转ansi,php 字符编码转换类,支持ANSI、Unicode、Unicode big endian、UTF-8、UTF-8+Bom 互相转换...
  9. Linux---字符设备驱动程序设计
  10. 音视频开发系列(49)视频编码标准发展史
  11. 迷你迅雷,IE下载加速补丁(转)
  12. Linux磁盘空间管理利器--ncdu(为你的 系统瘦身)
  13. TSO/GSO/LRO/GRO测试
  14. IIS网站中下载.bat文件
  15. kettle执行sql语句错误总结
  16. 交叉编译linux内核实例(最详细)总结
  17. 2021云计算技能竞赛真题
  18. SpringBoot整合druid数据源,quartz定式框架
  19. 书摘|什么是大数定律
  20. SpringBootc出现Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.clo

热门文章

  1. 如何提高百度云客户端的下载速度
  2. TestNG Annotations示例
  3. Java项目:养老院管理系统(java+Spring Boot + SpringMVC + MyBatis+HTML+CSS+JavaScrip+ Layui+maven+mysql)
  4. 301,404,网站地图
  5. 最中二的性能测试工具--vegeta
  6. 计算机四级考试题库 苹果,‎App Store 上的“计算机四级网络工程师考试题库”...
  7. 读书笔记——物联网导论第3版(1)
  8. 感谢无限互联iOS开发视频教程
  9. 精美创意美食卡通PPT-朴尔PPT
  10. CUIT循迹智能车竞赛