statement_performance_analyzer()过程
创建服务器上运行的语句的报告。这些视图是基于总体和/或增量活动来计算的。
此过程通过操纵sql_log_bin
系统变量的会话值来在执行过程中禁用二进制日志记录。那是一个受限的操作,因此该过程需要足以设置受限会话变量的特权。请参见“系统变量特权”。
参量
in_action ENUM('snapshot','overall','delta','create_tmp','create_table','save','cleanup')
:要采取的行动。这些值是允许的:snapshot
:存储快照。默认设置是为“性能模式”events_statements_summary_by_digest
表的当前内容制作快照。通过设置in_table
,可以覆盖此设置以复制指定表的内容。快照存储在sys
架构tmp_digests
临时表中。overall
:根据所指定表的内容来生成分析in_table
。对于整体分析,in_table
可以NOW()
使用一个新鲜的快照。这将覆盖现有快照。使用NULL
了in_table
使用现有的快照。如果in_table
是,NULL
并且不存在快照,则会创建一个新的快照。该in_views
参数和statement_performance_analyzer.limit
配置选项会影响这个过程的操作。delta
:生成增量分析。增量是在由指定的参考表in_table
和必须存在的快照之间计算的。此操作使用sys
架构tmp_digests_delta
临时表。该in_views
参数和statement_performance_analyzer.limit
配置选项会影响这个过程的操作。create_table
:创建一个常规表,该表适合存储快照供以后使用(例如,用于计算增量)。create_tmp
:创建一个临时表,该表适合于存储快照以供以后使用(例如,用于计算增量)。save
:将快照保存在由指定的表中in_table
。该表必须存在并且具有正确的结构。如果不存在快照,则会创建一个新的快照。cleanup
:删除用于快照和增量的临时表。
in_table VARCHAR(129)
:表参数用于该in_action
参数指定的某些操作。使用格式db_name.tbl_name
或tbl_name
不使用任何反引号(`
)标识符引号引起来。.
数据库和表名不支持句点()。in_table
每个in_action
值的值含义在各个in_action
值说明中都有详细说明。in_views SET('with_runtimes_in_95th_percentile','analysis','with_errors_or_warnings','with_full_table_scans','with_sorting','with_temp_tables','custom')
:要包含的视图。此参数是一个SET
值,因此它可以包含多个视图名称,以逗号分隔。默认设置为包括除之外的所有视图custom
。允许使用以下值:with_runtimes_in_95th_percentile
:使用statements_with_runtimes_in_95th_percentile
视图。analysis
:使用statement_analysis
视图。with_errors_or_warnings
:使用statements_with_errors_or_warnings
视图。with_full_table_scans
:使用statements_with_full_table_scans
视图。with_sorting
:使用statements_with_sorting
视图。with_temp_tables
:使用statements_with_temp_tables
视图。custom
:使用自定义视图。必须使用statement_performance_analyzer.view
配置选项指定该视图以命名查询或现有视图。
配置选项
statement_performance_analyzer()
可以使用以下配置选项或其相应的用户定义变量来修改该操作(请参见“ sys_config表”):
debug
,@sys.debug
如果此选项为
ON
,则产生调试输出。默认值为OFF
。statement_performance_analyzer.limit
,@sys.statement_performance_analyzer.limit
对于没有内置限制的视图,要返回的最大行数。默认值为100。
statement_performance_analyzer.view
,@sys.statement_performance_analyzer.view
要使用的定制查询或视图。如果选项值包含空格,则将其解释为查询。否则,它必须是查询“性能模式”
events_statements_summary_by_digest
表的现有视图的名称。LIMIT
如果statement_performance_analyzer.limit
配置选项大于0 ,则查询或视图定义中不能存在任何子句。如果指定视图,请使用与in_table
参数相同的格式。默认值为NULL
(未定义自定义视图)。
例
要创建带有自上次截断以来的第95个百分点中的查询events_statements_summary_by_digest
且具有一分钟的增量周期的报告,请执行以下操作:
- 创建一个临时表来存储初始快照。
- 创建初始快照。
- 将初始快照保存在临时表中。
- 等一分钟。
- 创建一个新的快照。
- 根据新快照执行分析。
- 基于初始快照和新快照之间的增量执行分析。
mysql>CALL sys.statement_performance_analyzer('create_tmp', 'mydb.tmp_digests_ini', NULL); Query OK, 0 rows affected (0.08 sec) mysql>CALL sys.statement_performance_analyzer('snapshot', NULL, NULL); Query OK, 0 rows affected (0.02 sec) mysql>CALL sys.statement_performance_analyzer('save', 'mydb.tmp_digests_ini', NULL); Query OK, 0 rows affected (0.00 sec) mysql>DO SLEEP(60); Query OK, 0 rows affected (1 min 0.00 sec) mysql>CALL sys.statement_performance_analyzer('snapshot', NULL, NULL); Query OK, 0 rows affected (0.02 sec) mysql>CALL sys.statement_performance_analyzer('overall', NULL, 'with_runtimes_in_95th_percentile'); +----------------------------------------- + | Next Output | +----------------------------------------- + | Queries with Runtime in 95th Percentile | +----------------------------------------- + 1 row in set (0.05 sec) ... mysql>CALL sys.statement_performance_analyzer('delta', 'mydb.tmp_digests_ini', 'with_runtimes_in_95th_percentile'); +----------------------------------------- + | Next Output | +----------------------------------------- + | Queries with Runtime in 95th Percentile | +----------------------------------------- + 1 row in set (0.03 sec) ...
使用全表扫描创建第95个百分点查询和前10个查询的总体报告:
mysql>CALL sys.statement_performance_analyzer('snapshot', NULL, NULL);Query OK, 0rows affected (0.01 sec) mysql>SET @sys.statement_performance_analyzer.limit = 10;Query OK, 0rows affected (0.00 sec) mysql>CALL sys.statement_performance_analyzer('overall', NULL, 'with_runtimes_in_95th_percentile,with_full_table_scans'); +----------------------------------------- + | Next Output | +----------------------------------------- + | Queries with Runtime in 95th Percentile | +----------------------------------------- + 1 row in set (0.01 sec) ... +------------------------------------- + | Next Output | +------------------------------------- + | Top 10 Queries with Full Table Scan | +------------------------------------- + 1 row in set (0.09 sec) ...
使用定制视图显示按总执行时间排序的前10个查询,并在Linux中使用watch命令每分钟刷新一次视图:
mysql>CREATE ORREPLACE VIEW mydb.my_statementsAS SELECT sys.format_statement(DIGEST_TEXT)AS query ,SCHEMA_NAME AS db, COUNT_STARAS exec_count, sys.format_time(SUM_TIMER_WAIT)AS total_latency, sys.format_time(AVG_TIMER_WAIT)AS avg_latency, ROUND(IFNULL(SUM_ROWS_SENT / NULLIF(COUNT_STAR, 0), 0))AS rows_sent_avg, ROUND(IFNULL(SUM_ROWS_EXAMINED / NULLIF(COUNT_STAR, 0), 0))AS rows_examined_avg, ROUND(IFNULL(SUM_ROWS_AFFECTED / NULLIF(COUNT_STAR, 0), 0))AS rows_affected_avg, DIGESTAS digestFROM performance_schema.events_statements_summary_by_digestORDER BY SUM_TIMER_WAITDESC ; Query OK, 0 rows affected (0.10 sec) mysql>CALL sys.statement_performance_analyzer('create_table', 'mydb.digests_prev', NULL); Query OK, 0 rows affected (0.10 sec) shell> watch -n 60 "mysql sys --table -e \" > SET @sys.statement_performance_analyzer.view = 'mydb.my_statements'; > SET @sys.statement_performance_analyzer.limit = 10; > CALL statement_performance_analyzer('snapshot', NULL, NULL); > CALL statement_performance_analyzer('delta', 'mydb.digests_prev', 'custom'); > CALL statement_performance_analyzer('save', 'mydb.digests_prev', NULL); > \""Every 60.0s: mysql sys --table -e " ... Mon Dec 22 10:58:51 2014 +---------------------------------- + | Next Output | +---------------------------------- + | Top 10 Queries Using Custom View | +---------------------------------- + +------------------- +------- +------------ +--------------- +------------- +--------------- +------------------- +------------------- +---------------------------------- + | query | db | exec_count | total_latency | avg_latency | rows_sent_avg | rows_examined_avg | rows_affected_avg | digest | +------------------- +------- +------------ +--------------- +------------- +--------------- +------------------- +------------------- +---------------------------------- + ...