mysql性能分析工具profile使用教程
来源:易贤网 阅读:901 次 日期:2014-12-10 11:56:36
温馨提示:易贤网小编为您整理了“mysql性能分析工具profile使用教程”,方便广大网友查阅!

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

1、有关profile的描述

代码如下:

--当前版本

]> show variables like 'version';

+---------------+---------------------------------------+

| variable_name | value |

+---------------+---------------------------------------+

| version | 5.6.17-enterprise-commercial-advanced |

+---------------+---------------------------------------+

--查看profiling系统变量

]> 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的帮助

]> 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.

--上面描述从5.6.7开始该命令将会被移除,用performance schema instead代替

--在oracle数据库中,是通过autotrace来剖析单条sql并获取真实的执行计划以及其开销信息

2、开启porfiling

代码如下:

--启用session级别的profiling

]> set profiling=1;

query ok, 0 rows affected, 1 warning (0.00 sec)

--验证修改后的结果

]> show variables like '%profil%';

+------------------------+-------+

| variable_name | value |

+------------------------+-------+

| have_profiling | yes |

| profiling | on |

| profiling_history_size | 15 |

+------------------------+-------+

--发布sql查询

]> select count(*) from customer;

+----------+

| count(*) |

+----------+

| 599 |

+----------+

--查看当前session所有已产生的profile

]> 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,之前一个,现在一个

]> 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产生的相应开销

]> 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

]> 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 |

+----------+------------+--------------------------------+

--获取指定查询的开销

]> 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部分的开销

]> 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部分的开销

]> 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 |

+----------------------+----------+

--同时查看不同资源开销

]> 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开销,且按最大耗用时间倒序排列

]> set @query_id=2;

]> 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

]> 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会被自动关闭

]> set profiling=off;

query ok, 0 rows affected, 1 warning (0.00 sec)

更多信息请查看IT技术专栏

更多信息请查看数据库
易贤网手机网站地址:mysql性能分析工具profile使用教程
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标