分析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.
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并获取真实的执行计划以及其开销信息
--查看当前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 | +---------+------+--------------------------------------------------------------------------------------------------------------+