概述

众所周知,MySQL数据库中的performance_schema的事件统计表中的统计数据计算的是累计值,如果想要计算某段时间的TOP SQL是不行的,这里考虑用函数定期取值存进中间表定期将累计值的结果存入A表,按sql_id分组查A表,筛选最后执行时间,然后直接最大值减最小值来实现需求了。
以下针对MySQL数据库8.0版本。

1、创建中间表

-- 报错记录error_log
CREATE TABLE sys.error_log (
ERROR varchar(128) NOT NULL,
AFFECT_ROWS bigint(20) unsigned NOT NULL,
del_time datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='报错记录表'; -- 中间表ews_event(取PERFORMANCE_SCHEMA.events_waits_summary_global_by_event_name:按照账户、主机、用户或线程统计的等待事件统计表)

CREATE TABLE sys.ews_event(

EVENT_NAME varchar(128) NOT NULL,

COUNT_STAR bigint(20) unsigned NOT NULL,

SUM_TIMER_WAIT bigint(20) unsigned NOT NULL,

MIN_TIMER_WAIT bigint(20) unsigned NOT NULL,

AVG_TIMER_WAIT bigint(20) unsigned NOT NULL,

MAX_TIMER_WAIT bigint(20) unsigned NOT NULL,

insert_date datetime NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='按照账户、主机、用户或线程统计的等待事件统计表'; -- 中间表ews_sql(取performance_schema.events_statements_summary_by_digest:按照事件的语句进行聚合,抓取每条标准化语句有关的延迟、错误和查询量信息)

CREATE TABLE sys.ews_sql (

SCHEMA_NAME varchar(64) DEFAULT NULL,

DIGEST varchar(32) DEFAULT NULL,

DIGEST_TEXT longtext,

COUNT_STAR bigint(20) unsigned NOT NULL,

SUM_TIMER_WAIT bigint(20) unsigned NOT NULL,

MIN_TIMER_WAIT bigint(20) unsigned NOT NULL,

AVG_TIMER_WAIT bigint(20) unsigned NOT NULL,

MAX_TIMER_WAIT bigint(20) unsigned NOT NULL,

SUM_LOCK_TIME bigint(20) unsigned NOT NULL,

SUM_ERRORS bigint(20) unsigned NOT NULL,

SUM_WARNINGS bigint(20) unsigned NOT NULL,

SUM_ROWS_AFFECTED bigint(20) unsigned NOT NULL,

SUM_ROWS_SENT bigint(20) unsigned NOT NULL,

SUM_ROWS_EXAMINED bigint(20) unsigned NOT NULL,

SUM_CREATED_TMP_DISK_TABLES bigint(20) unsigned NOT NULL,

SUM_CREATED_TMP_TABLES bigint(20) unsigned NOT NULL,

SUM_SELECT_FULL_JOIN bigint(20) unsigned NOT NULL,

SUM_SELECT_FULL_RANGE_JOIN bigint(20) unsigned NOT NULL,

SUM_SELECT_RANGE bigint(20) unsigned NOT NULL,

SUM_SELECT_RANGE_CHECK bigint(20) unsigned NOT NULL,

SUM_SELECT_SCAN bigint(20) unsigned NOT NULL,

SUM_SORT_MERGE_PASSES bigint(20) unsigned NOT NULL,

SUM_SORT_RANGE bigint(20) unsigned NOT NULL,

SUM_SORT_ROWS bigint(20) unsigned NOT NULL,

SUM_SORT_SCAN bigint(20) unsigned NOT NULL,

SUM_NO_INDEX_USED bigint(20) unsigned NOT NULL,

SUM_NO_GOOD_INDEX_USED bigint(20) unsigned NOT NULL,

FIRST_SEEN datetime NOT NULL DEFAULT NOW(),

LAST_SEEN datetime NOT NULL DEFAULT NOW()

) ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='按照事件的语句进行聚合,抓取每条标准化语句有关的延迟、错误和查询量信息'; -- 中间表ews_table_iowaits(取performance_schema.table_io_waits_summary_by_table:按照每个表进行统计的表I/O等待事件)

CREATE TABLE sys.ews_table_iowaits (

OBJECT_TYPE varchar(64) DEFAULT NULL,

OBJECT_SCHEMA varchar(64) DEFAULT NULL,

OBJECT_NAME varchar(64) DEFAULT NULL,

COUNT_STAR bigint(20) unsigned NOT NULL,

SUM_TIMER_WAIT bigint(20) unsigned NOT NULL,

MIN_TIMER_WAIT bigint(20) unsigned NOT NULL,

AVG_TIMER_WAIT bigint(20) unsigned NOT NULL,

MAX_TIMER_WAIT bigint(20) unsigned NOT NULL,

COUNT_READ bigint(20) unsigned NOT NULL,

SUM_TIMER_READ bigint(20) unsigned NOT NULL,

MIN_TIMER_READ bigint(20) unsigned NOT NULL,

AVG_TIMER_READ bigint(20) unsigned NOT NULL,

MAX_TIMER_READ bigint(20) unsigned NOT NULL,

COUNT_WRITE bigint(20) unsigned NOT NULL,

SUM_TIMER_WRITE bigint(20) unsigned NOT NULL,

MIN_TIMER_WRITE bigint(20) unsigned NOT NULL,

AVG_TIMER_WRITE bigint(20) unsigned NOT NULL,

MAX_TIMER_WRITE bigint(20) unsigned NOT NULL,

COUNT_FETCH bigint(20) unsigned NOT NULL,

SUM_TIMER_FETCH bigint(20) unsigned NOT NULL,

MIN_TIMER_FETCH bigint(20) unsigned NOT NULL,

AVG_TIMER_FETCH bigint(20) unsigned NOT NULL,

MAX_TIMER_FETCH bigint(20) unsigned NOT NULL,

COUNT_INSERT bigint(20) unsigned NOT NULL,

SUM_TIMER_INSERT bigint(20) unsigned NOT NULL,

MIN_TIMER_INSERT bigint(20) unsigned NOT NULL,

AVG_TIMER_INSERT bigint(20) unsigned NOT NULL,

MAX_TIMER_INSERT bigint(20) unsigned NOT NULL,

COUNT_UPDATE bigint(20) unsigned NOT NULL,

SUM_TIMER_UPDATE bigint(20) unsigned NOT NULL,

MIN_TIMER_UPDATE bigint(20) unsigned NOT NULL,

AVG_TIMER_UPDATE bigint(20) unsigned NOT NULL,

MAX_TIMER_UPDATE bigint(20) unsigned NOT NULL,

COUNT_DELETE bigint(20) unsigned NOT NULL,

SUM_TIMER_DELETE bigint(20) unsigned NOT NULL,

MIN_TIMER_DELETE bigint(20) unsigned NOT NULL,

AVG_TIMER_DELETE bigint(20) unsigned NOT NULL,

MAX_TIMER_DELETE bigint(20) unsigned NOT NULL,

INSERT_DATE datetime NOT NULL

) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='按照每个表进行统计的表I/O等待事件'; -- 中间表ews_index_iowaits(取performance_schema.table_io_waits_summary_by_index_usage:按照每个索引进行统计的表I/O等待事件)

CREATE TABLE sys.ews_index_iowaits (

OBJECT_TYPE varchar(64) DEFAULT NULL,

OBJECT_SCHEMA varchar(64) DEFAULT NULL,

OBJECT_NAME varchar(64) DEFAULT NULL,

INDEX_NAME varchar(64) DEFAULT NULL,

COUNT_STAR bigint(20) unsigned NOT NULL,

SUM_TIMER_WAIT bigint(20) unsigned NOT NULL,

MIN_TIMER_WAIT bigint(20) unsigned NOT NULL,

AVG_TIMER_WAIT bigint(20) unsigned NOT NULL,

MAX_TIMER_WAIT bigint(20) unsigned NOT NULL,

COUNT_READ bigint(20) unsigned NOT NULL,

SUM_TIMER_READ bigint(20) unsigned NOT NULL,

MIN_TIMER_READ bigint(20) unsigned NOT NULL,

AVG_TIMER_READ bigint(20) unsigned NOT NULL,

MAX_TIMER_READ bigint(20) unsigned NOT NULL,

COUNT_WRITE bigint(20) unsigned NOT NULL,

SUM_TIMER_WRITE bigint(20) unsigned NOT NULL,

MIN_TIMER_WRITE bigint(20) unsigned NOT NULL,

AVG_TIMER_WRITE bigint(20) unsigned NOT NULL,

MAX_TIMER_WRITE bigint(20) unsigned NOT NULL,

COUNT_FETCH bigint(20) unsigned NOT NULL,

SUM_TIMER_FETCH bigint(20) unsigned NOT NULL,

MIN_TIMER_FETCH bigint(20) unsigned NOT NULL,

AVG_TIMER_FETCH bigint(20) unsigned NOT NULL,

MAX_TIMER_FETCH bigint(20) unsigned NOT NULL,

COUNT_INSERT bigint(20) unsigned NOT NULL,

SUM_TIMER_INSERT bigint(20) unsigned NOT NULL,

MIN_TIMER_INSERT bigint(20) unsigned NOT NULL,

AVG_TIMER_INSERT bigint(20) unsigned NOT NULL,

MAX_TIMER_INSERT bigint(20) unsigned NOT NULL,

COUNT_UPDATE bigint(20) unsigned NOT NULL,

SUM_TIMER_UPDATE bigint(20) unsigned NOT NULL,

MIN_TIMER_UPDATE bigint(20) unsigned NOT NULL,

AVG_TIMER_UPDATE bigint(20) unsigned NOT NULL,

MAX_TIMER_UPDATE bigint(20) unsigned NOT NULL,

COUNT_DELETE bigint(20) unsigned NOT NULL,

SUM_TIMER_DELETE bigint(20) unsigned NOT NULL,

MIN_TIMER_DELETE bigint(20) unsigned NOT NULL,

AVG_TIMER_DELETE bigint(20) unsigned NOT NULL,

MAX_TIMER_DELETE bigint(20) unsigned NOT NULL,

INSERT_DATE datetime NOT NULL

) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='按照每个索引进行统计的表I/O等待事件'; -- 中间表sys.ews_table_lockwaits(取performance_schema.table_lock_waits_summary_by_table:按照每个表进行统计的表锁等待事件)

CREATE TABLE sys.ews_table_lockwaits (

OBJECT_TYPE varchar(64) DEFAULT NULL,

OBJECT_SCHEMA varchar(64) DEFAULT NULL,

OBJECT_NAME varchar(64) DEFAULT NULL,

COUNT_STAR bigint(20) unsigned NOT NULL,

SUM_TIMER_WAIT bigint(20) unsigned NOT NULL,

MIN_TIMER_WAIT bigint(20) unsigned NOT NULL,

AVG_TIMER_WAIT bigint(20) unsigned NOT NULL,

MAX_TIMER_WAIT bigint(20) unsigned NOT NULL,

COUNT_READ bigint(20) unsigned NOT NULL,

SUM_TIMER_READ bigint(20) unsigned NOT NULL,

MIN_TIMER_READ bigint(20) unsigned NOT NULL,

AVG_TIMER_READ bigint(20) unsigned NOT NULL,

MAX_TIMER_READ bigint(20) unsigned NOT NULL,

COUNT_WRITE bigint(20) unsigned NOT NULL,

SUM_TIMER_WRITE bigint(20) unsigned NOT NULL,

MIN_TIMER_WRITE bigint(20) unsigned NOT NULL,

AVG_TIMER_WRITE bigint(20) unsigned NOT NULL,

MAX_TIMER_WRITE bigint(20) unsigned NOT NULL,

COUNT_READ_NORMAL bigint(20) unsigned NOT NULL,

SUM_TIMER_READ_NORMAL bigint(20) unsigned NOT NULL,

MIN_TIMER_READ_NORMAL bigint(20) unsigned NOT NULL,

AVG_TIMER_READ_NORMAL bigint(20) unsigned NOT NULL,

MAX_TIMER_READ_NORMAL bigint(20) unsigned NOT NULL,

COUNT_READ_WITH_SHARED_LOCKS bigint(20) unsigned NOT NULL,

SUM_TIMER_READ_WITH_SHARED_LOCKS bigint(20) unsigned NOT NULL,

MIN_TIMER_READ_WITH_SHARED_LOCKS bigint(20) unsigned NOT NULL,

AVG_TIMER_READ_WITH_SHARED_LOCKS bigint(20) unsigned NOT NULL,

MAX_TIMER_READ_WITH_SHARED_LOCKS bigint(20) unsigned NOT NULL,

COUNT_READ_NO_INSERT bigint(20) unsigned NOT NULL,

SUM_TIMER_READ_NO_INSERT bigint(20) unsigned NOT NULL,

MIN_TIMER_READ_NO_INSERT bigint(20) unsigned NOT NULL,

AVG_TIMER_READ_NO_INSERT bigint(20) unsigned NOT NULL,

MAX_TIMER_READ_NO_INSERT bigint(20) unsigned NOT NULL,

COUNT_READ_EXTERNAL bigint(20) unsigned NOT NULL,

SUM_TIMER_READ_EXTERNAL bigint(20) unsigned NOT NULL,

MIN_TIMER_READ_EXTERNAL bigint(20) unsigned NOT NULL,

AVG_TIMER_READ_EXTERNAL bigint(20) unsigned NOT NULL,

MAX_TIMER_READ_EXTERNAL bigint(20) unsigned NOT NULL,

COUNT_WRITE_ALLOW_WRITE bigint(20) unsigned NOT NULL,

SUM_TIMER_WRITE_ALLOW_WRITE bigint(20) unsigned NOT NULL,

MIN_TIMER_WRITE_ALLOW_WRITE bigint(20) unsigned NOT NULL,

AVG_TIMER_WRITE_ALLOW_WRITE bigint(20) unsigned NOT NULL,

MAX_TIMER_WRITE_ALLOW_WRITE bigint(20) unsigned NOT NULL,

COUNT_WRITE_CONCURRENT_INSERT bigint(20) unsigned NOT NULL,

SUM_TIMER_WRITE_CONCURRENT_INSERT bigint(20) unsigned NOT NULL,

MIN_TIMER_WRITE_CONCURRENT_INSERT bigint(20) unsigned NOT NULL,

AVG_TIMER_WRITE_CONCURRENT_INSERT bigint(20) unsigned NOT NULL,

MAX_TIMER_WRITE_CONCURRENT_INSERT bigint(20) unsigned NOT NULL,

COUNT_WRITE_NORMAL bigint(20) unsigned NOT NULL,

SUM_TIMER_WRITE_NORMAL bigint(20) unsigned NOT NULL,

MIN_TIMER_WRITE_NORMAL bigint(20) unsigned NOT NULL,

AVG_TIMER_WRITE_NORMAL bigint(20) unsigned NOT NULL,

MAX_TIMER_WRITE_NORMAL bigint(20) unsigned NOT NULL,

COUNT_WRITE_EXTERNAL bigint(20) unsigned NOT NULL,

SUM_TIMER_WRITE_EXTERNAL bigint(20) unsigned NOT NULL,

MIN_TIMER_WRITE_EXTERNAL bigint(20) unsigned NOT NULL,

AVG_TIMER_WRITE_EXTERNAL bigint(20) unsigned NOT NULL,

MAX_TIMER_WRITE_EXTERNAL bigint(20) unsigned NOT NULL,

INSERT_DATE datetime NOT NULL

) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='按照每个表进行统计的表锁等待事件'; -- 中间表sys.ews_file_io(取performance_schema.file_summary_by_instance:按照每个文件实例(对应具体的磁盘文件)进行统计的文件I/0等待事件)

CREATE TABLE sys.ews_file_io (

FILE_NAME varchar(512) NOT NULL,

EVENT_NAME varchar(128) NOT NULL,

COUNT_STAR bigint(20) unsigned NOT NULL,

SUM_NUMBER_OF_BYTES_READ bigint(20) NOT NULL,

SUM_NUMBER_OF_BYTES_WRITE bigint(20) NOT NULL,

INSERT_DATE datetime NOT NULL

) ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='按照每个文件实例(对应具体的磁盘文件)进行统计的文件I/0等待事件';

2、自定义存储过程

use sys;
drop procedure if exists ews; DELIMITER //

CREATE DEFINER = root @'%' PROCEDURE ews( ) BEGIN

DECLARE

affect_rows INT;

DECLARE

del_day INT;-- 长日志存留期

DECLARE

v_commit INT DEFAULT 2;-- 定义事务用,1为正常,-10为失败

DECLARE

msg text;-- 记录错误信息

-- 异常的时候msg捕获报错信息

DECLARE

CONTINUE HANDLER FOR SQLEXCEPTION BEGIN

get diagnostics CONDITION 1 msg = message_text;
    SET v_commit <span class="token operator">=</span> - 10<span class="token punctuation">;</span>

END<span class="token punctuation">;</span>

SET @del_day <span class="token operator">=</span> 8<span class="token punctuation">;</span>
START TRANSACTION<span class="token punctuation">;</span>-- 设置事务 -- Top 10 Event Summary
INSERT INTO sys.ews_event SELECT
ews.EVENT_NAME,
ews.COUNT_STAR,
round<span class="token punctuation">(</span> ews.SUM_TIMER_WAIT / 1000000 <span class="token punctuation">)</span> SUM_TIMER_WAIT,
round<span class="token punctuation">(</span> ews.AVG_TIMER_WAIT / 1000000 <span class="token punctuation">)</span> AVG_TIMER_WAIT,
round<span class="token punctuation">(</span> ews.MIN_TIMER_WAIT / 1000000 <span class="token punctuation">)</span> MIN_TIMER_WAIT,
round<span class="token punctuation">(</span> ews.MAX_TIMER_WAIT / 1000000 <span class="token punctuation">)</span> MAX_TIMER_WAIT,
now<span class="token punctuation">(</span> <span class="token punctuation">)</span>
FROM
PERFORMANCE_SCHEMA.events_waits_summary_global_by_event_name ews
WHERE
ews.count_star <span class="token operator">&gt;</span> 0
AND ews.event_name <span class="token operator">!=</span> <span class="token string">'idle'</span><span class="token punctuation">;</span> -- TOP 10 SQL
INSERT INTO sys.ews_sql <span class="token keyword">select</span> ews.SCHEMA_NAME,
ews.DIGEST,
ews.DIGEST_TEXT,
ews.COUNT_STAR,
round<span class="token punctuation">(</span> ews.SUM_TIMER_WAIT / 1000000 <span class="token punctuation">)</span> SUM_TIMER_WAIT,
round<span class="token punctuation">(</span> ews.MIN_TIMER_WAIT / 1000000 <span class="token punctuation">)</span> MIN_TIMER_WAIT,
round<span class="token punctuation">(</span> ews.AVG_TIMER_WAIT / 1000000 <span class="token punctuation">)</span> AVG_TIMER_WAIT,
round<span class="token punctuation">(</span> ews.MAX_TIMER_WAIT / 1000000 <span class="token punctuation">)</span> MAX_TIMER_WAIT,
round<span class="token punctuation">(</span> ews.SUM_LOCK_TIME / 1000000 <span class="token punctuation">)</span> SUM_LOCK_TIME,
ews.SUM_ERRORS,
ews.SUM_WARNINGS,
ews.SUM_ROWS_AFFECTED,
ews.SUM_ROWS_SENT,
ews.SUM_ROWS_EXAMINED,
ews.SUM_CREATED_TMP_DISK_TABLES,
ews.SUM_CREATED_TMP_TABLES,
ews.SUM_SELECT_FULL_JOIN,
ews.SUM_SELECT_FULL_RANGE_JOIN,
ews.SUM_SELECT_RANGE,
ews.SUM_SELECT_RANGE_CHECK,
ews.SUM_SELECT_SCAN,
ews.SUM_SORT_MERGE_PASSES,
ews.SUM_SORT_RANGE,
ews.SUM_SORT_ROWS,
ews.SUM_SORT_SCAN,
ews.SUM_NO_INDEX_USED,
ews.SUM_NO_GOOD_INDEX_USED,
ews.FIRST_SEEN,
ews.LAST_SEEN
FROM
PERFORMANCE_SCHEMA.events_statements_summary_by_digest ews
WHERE
ews.SCHEMA_NAME NOT IN <span class="token punctuation">(</span> <span class="token string">'perf_stat'</span>, <span class="token string">'mysql'</span>, <span class="token string">'sys'</span>, <span class="token string">'performance_schema'</span>, <span class="token string">'information_schema'</span> <span class="token punctuation">)</span>
AND ews.count_star <span class="token operator">&gt;</span> 0
AND ews.digest_text NOT LIKE <span class="token string">'SHOW%'</span>
AND ews.digest_text NOT LIKE <span class="token string">'USE%'</span>
AND ews.digest_text NOT LIKE <span class="token string">'SET%'</span>
AND ews.digest_text NOT LIKE <span class="token string">'SELECT @%'</span>
AND ews.digest_text NOT LIKE <span class="token string">'EXPLAIN%'</span>
AND ews.digest_text NOT LIKE <span class="token string">'DROP PROCEDURE%'</span>
AND ews.digest_text NOT LIKE <span class="token string">'CREATE PROCEDURE%'</span><span class="token punctuation">;</span> -- Top 10 Table By LOGICAL IO Wait
INSERT INTO sys.ews_table_iowaits <span class="token keyword">select</span>
ews.OBJECT_TYPE,
ews.OBJECT_SCHEMA,
ews.OBJECT_NAME,
ews.COUNT_STAR,
round<span class="token punctuation">(</span>ews.SUM_TIMER_WAIT/1000000<span class="token punctuation">)</span> SUM_TIMER_WAIT,
round<span class="token punctuation">(</span>ews.MIN_TIMER_WAIT/1000000<span class="token punctuation">)</span> MIN_TIMER_WAIT,
round<span class="token punctuation">(</span>ews.AVG_TIMER_WAIT/1000000<span class="token punctuation">)</span> AVG_TIMER_WAIT,
round<span class="token punctuation">(</span>ews.MAX_TIMER_WAIT/1000000<span class="token punctuation">)</span> MAX_TIMER_WAIT,
ews.COUNT_READ,
round<span class="token punctuation">(</span>ews.SUM_TIMER_READ/1000000<span class="token punctuation">)</span> SUM_TIMER_READ,
round<span class="token punctuation">(</span>ews.MIN_TIMER_READ/1000000<span class="token punctuation">)</span> MIN_TIMER_READ,
round<span class="token punctuation">(</span>ews.AVG_TIMER_READ/1000000<span class="token punctuation">)</span> AVG_TIMER_READ,
round<span class="token punctuation">(</span>ews.MAX_TIMER_READ/1000000<span class="token punctuation">)</span> MAX_TIMER_READ,
ews.COUNT_WRITE,
round<span class="token punctuation">(</span>ews.SUM_TIMER_WRITE/1000000<span class="token punctuation">)</span> SUM_TIMER_WRITE,
round<span class="token punctuation">(</span>ews.MIN_TIMER_WRITE/1000000<span class="token punctuation">)</span> MIN_TIMER_WRITE,
round<span class="token punctuation">(</span>ews.AVG_TIMER_WRITE/1000000<span class="token punctuation">)</span> AVG_TIMER_WRITE,
round<span class="token punctuation">(</span>ews.MAX_TIMER_WRITE/1000000<span class="token punctuation">)</span> MAX_TIMER_WRITE,
ews.COUNT_FETCH,
round<span class="token punctuation">(</span>ews.SUM_TIMER_FETCH/1000000<span class="token punctuation">)</span> SUM_TIMER_FETCH,
round<span class="token punctuation">(</span>ews.MIN_TIMER_FETCH/1000000<span class="token punctuation">)</span> MIN_TIMER_FETCH,
round<span class="token punctuation">(</span>ews.AVG_TIMER_FETCH/1000000<span class="token punctuation">)</span> AVG_TIMER_FETCH,
round<span class="token punctuation">(</span>ews.MAX_TIMER_FETCH/1000000<span class="token punctuation">)</span> MAX_TIMER_FETCH,
ews.COUNT_INSERT,
round<span class="token punctuation">(</span>ews.SUM_TIMER_INSERT/1000000<span class="token punctuation">)</span> SUM_TIMER_INSERT,
round<span class="token punctuation">(</span>ews.MIN_TIMER_INSERT/1000000<span class="token punctuation">)</span> MIN_TIMER_INSERT,
round<span class="token punctuation">(</span>ews.AVG_TIMER_INSERT/1000000<span class="token punctuation">)</span> AVG_TIMER_INSERT,
round<span class="token punctuation">(</span>ews.MAX_TIMER_INSERT/1000000<span class="token punctuation">)</span> MAX_TIMER_INSERT,
ews.COUNT_UPDATE,
round<span class="token punctuation">(</span>ews.SUM_TIMER_UPDATE/1000000<span class="token punctuation">)</span> SUM_TIMER_UPDATE,
round<span class="token punctuation">(</span>ews.MIN_TIMER_UPDATE/1000000<span class="token punctuation">)</span> MIN_TIMER_UPDATE,
round<span class="token punctuation">(</span>ews.AVG_TIMER_UPDATE/1000000<span class="token punctuation">)</span> AVG_TIMER_UPDATE,
round<span class="token punctuation">(</span>ews.MAX_TIMER_UPDATE/1000000<span class="token punctuation">)</span> MAX_TIMER_UPDATE,
ews.COUNT_DELETE,
round<span class="token punctuation">(</span>ews.SUM_TIMER_DELETE/1000000<span class="token punctuation">)</span> SUM_TIMER_DELETE,
round<span class="token punctuation">(</span>ews.MIN_TIMER_DELETE/1000000<span class="token punctuation">)</span> MIN_TIMER_DELETE,
round<span class="token punctuation">(</span>ews.AVG_TIMER_DELETE/1000000<span class="token punctuation">)</span> AVG_TIMER_DELETE,
round<span class="token punctuation">(</span>ews.MAX_TIMER_DELETE/1000000<span class="token punctuation">)</span> MAX_TIMER_DELETE,
now<span class="token punctuation">(</span> <span class="token punctuation">)</span>
from performance_schema.table_io_waits_summary_by_table ews
where ews.OBJECT_SCHEMA not <span class="token keyword">in</span> <span class="token punctuation">(</span><span class="token string">'perf_stat'</span>,<span class="token string">'mysql'</span>,<span class="token string">'sys'</span>,<span class="token string">'performance_schema'</span>,<span class="token string">'information_schema'</span><span class="token punctuation">)</span>
and ews.count_star <span class="token operator">&gt;</span> 0<span class="token punctuation">;</span> -- Top 10 Index By LOGICAL IO Wait
INSERT INTO SYS.ews_index_iowaits <span class="token keyword">select</span>
ews.OBJECT_TYPE,
ews.OBJECT_SCHEMA,
ews.OBJECT_NAME,
ews.INDEX_NAME,
ews.COUNT_STAR,
round<span class="token punctuation">(</span>ews.SUM_TIMER_WAIT/1000000<span class="token punctuation">)</span> SUM_TIMER_WAIT,
round<span class="token punctuation">(</span>ews.MIN_TIMER_WAIT/1000000<span class="token punctuation">)</span> MIN_TIMER_WAIT,
round<span class="token punctuation">(</span>ews.AVG_TIMER_WAIT/1000000<span class="token punctuation">)</span> AVG_TIMER_WAIT,
round<span class="token punctuation">(</span>ews.MAX_TIMER_WAIT/1000000<span class="token punctuation">)</span> MAX_TIMER_WAIT,
ews.COUNT_READ,
round<span class="token punctuation">(</span>ews.SUM_TIMER_READ/1000000<span class="token punctuation">)</span> SUM_TIMER_READ,
round<span class="token punctuation">(</span>ews.MIN_TIMER_READ/1000000<span class="token punctuation">)</span> MIN_TIMER_READ,
round<span class="token punctuation">(</span>ews.AVG_TIMER_READ/1000000<span class="token punctuation">)</span> AVG_TIMER_READ,
round<span class="token punctuation">(</span>ews.MAX_TIMER_READ/1000000<span class="token punctuation">)</span> MAX_TIMER_READ,
ews.COUNT_WRITE,
round<span class="token punctuation">(</span>ews.SUM_TIMER_WRITE/1000000<span class="token punctuation">)</span> SUM_TIMER_WRITE,
round<span class="token punctuation">(</span>ews.MIN_TIMER_WRITE/1000000<span class="token punctuation">)</span> MIN_TIMER_WRITE,
round<span class="token punctuation">(</span>ews.AVG_TIMER_WRITE/1000000<span class="token punctuation">)</span> AVG_TIMER_WRITE,
round<span class="token punctuation">(</span>ews.MAX_TIMER_WRITE/1000000<span class="token punctuation">)</span> MAX_TIMER_WRITE,
ews.COUNT_FETCH,
round<span class="token punctuation">(</span>ews.SUM_TIMER_FETCH/1000000<span class="token punctuation">)</span> SUM_TIMER_FETCH,
round<span class="token punctuation">(</span>ews.MIN_TIMER_FETCH/1000000<span class="token punctuation">)</span> MIN_TIMER_FETCH,
round<span class="token punctuation">(</span>ews.AVG_TIMER_FETCH/1000000<span class="token punctuation">)</span> AVG_TIMER_FETCH,
round<span class="token punctuation">(</span>ews.MAX_TIMER_FETCH/1000000<span class="token punctuation">)</span> MAX_TIMER_FETCH,
ews.COUNT_INSERT,
round<span class="token punctuation">(</span>ews.SUM_TIMER_INSERT/1000000<span class="token punctuation">)</span> SUM_TIMER_INSERT,
round<span class="token punctuation">(</span>ews.MIN_TIMER_INSERT/1000000<span class="token punctuation">)</span> MIN_TIMER_INSERT,
round<span class="token punctuation">(</span>ews.AVG_TIMER_INSERT/1000000<span class="token punctuation">)</span> AVG_TIMER_INSERT,
round<span class="token punctuation">(</span>ews.MAX_TIMER_INSERT/1000000<span class="token punctuation">)</span> MAX_TIMER_INSERT,
ews.COUNT_UPDATE,
round<span class="token punctuation">(</span>ews.SUM_TIMER_UPDATE/1000000<span class="token punctuation">)</span> SUM_TIMER_UPDATE,
round<span class="token punctuation">(</span>ews.MIN_TIMER_UPDATE/1000000<span class="token punctuation">)</span> MIN_TIMER_UPDATE,
round<span class="token punctuation">(</span>ews.AVG_TIMER_UPDATE/1000000<span class="token punctuation">)</span> AVG_TIMER_UPDATE,
round<span class="token punctuation">(</span>ews.MAX_TIMER_UPDATE/1000000<span class="token punctuation">)</span> MAX_TIMER_UPDATE,
ews.COUNT_DELETE,
round<span class="token punctuation">(</span>ews.SUM_TIMER_DELETE/1000000<span class="token punctuation">)</span> SUM_TIMER_DELETE,
round<span class="token punctuation">(</span>ews.MIN_TIMER_DELETE/1000000<span class="token punctuation">)</span> MIN_TIMER_DELETE,
round<span class="token punctuation">(</span>ews.AVG_TIMER_DELETE/1000000<span class="token punctuation">)</span> AVG_TIMER_DELETE,
round<span class="token punctuation">(</span>ews.MAX_TIMER_DELETE/1000000<span class="token punctuation">)</span> MAX_TIMER_DELETE,
now<span class="token punctuation">(</span> <span class="token punctuation">)</span>
from performance_schema.table_io_waits_summary_by_index_usage ews
where ews.OBJECT_SCHEMA not <span class="token keyword">in</span> <span class="token punctuation">(</span><span class="token string">'perf_stat'</span>,<span class="token string">'mysql'</span>,<span class="token string">'sys'</span>,<span class="token string">'performance_schema'</span>,<span class="token string">'information_schema'</span><span class="token punctuation">)</span>
and ews.count_star <span class="token operator">&gt;</span> 0<span class="token punctuation">;</span> -- Top 10 Table By LOCKS WAITS
INSERT INTO sys.ews_table_lockwaits <span class="token keyword">select</span>
ews.OBJECT_TYPE,
ews.OBJECT_SCHEMA,
ews.OBJECT_NAME,
ews.COUNT_STAR,
round<span class="token punctuation">(</span>ews.SUM_TIMER_WAIT/1000000<span class="token punctuation">)</span> SUM_TIMER_WAIT,
round<span class="token punctuation">(</span>ews.MIN_TIMER_WAIT/1000000<span class="token punctuation">)</span> MIN_TIMER_WAIT,
round<span class="token punctuation">(</span>ews.AVG_TIMER_WAIT/1000000<span class="token punctuation">)</span> AVG_TIMER_WAIT,
round<span class="token punctuation">(</span>ews.MAX_TIMER_WAIT/1000000<span class="token punctuation">)</span> MAX_TIMER_WAIT,
ews.COUNT_READ,
round<span class="token punctuation">(</span>ews.SUM_TIMER_READ/1000000<span class="token punctuation">)</span> SUM_TIMER_READ,
round<span class="token punctuation">(</span>ews.MIN_TIMER_READ/1000000<span class="token punctuation">)</span> MIN_TIMER_READ,
round<span class="token punctuation">(</span>ews.AVG_TIMER_READ/1000000<span class="token punctuation">)</span> AVG_TIMER_READ,
round<span class="token punctuation">(</span>ews.MAX_TIMER_READ/1000000<span class="token punctuation">)</span> MAX_TIMER_READ,
ews.COUNT_WRITE,
round<span class="token punctuation">(</span>ews.SUM_TIMER_WRITE/1000000<span class="token punctuation">)</span> SUM_TIMER_WRITE,
round<span class="token punctuation">(</span>ews.MIN_TIMER_WRITE/1000000<span class="token punctuation">)</span> MIN_TIMER_WRITE,
round<span class="token punctuation">(</span>ews.AVG_TIMER_WRITE/1000000<span class="token punctuation">)</span> AVG_TIMER_WRITE,
round<span class="token punctuation">(</span>ews.MAX_TIMER_WRITE/1000000<span class="token punctuation">)</span> MAX_TIMER_WRITE,
ews.COUNT_READ_NORMAL,
round<span class="token punctuation">(</span>ews.SUM_TIMER_READ_NORMAL/1000000<span class="token punctuation">)</span> SUM_TIMER_READ_NORMAL,
round<span class="token punctuation">(</span>ews.MIN_TIMER_READ_NORMAL/1000000<span class="token punctuation">)</span> MIN_TIMER_READ_NORMAL,
round<span class="token punctuation">(</span>ews.AVG_TIMER_READ_NORMAL/1000000<span class="token punctuation">)</span> AVG_TIMER_READ_NORMAL,
round<span class="token punctuation">(</span>ews.MAX_TIMER_READ_NORMAL/1000000<span class="token punctuation">)</span> MAX_TIMER_READ_NORMAL,
ews.COUNT_READ_WITH_SHARED_LOCKS,
round<span class="token punctuation">(</span>ews.SUM_TIMER_READ_WITH_SHARED_LOCKS/1000000<span class="token punctuation">)</span> SUM_TIMER_READ_WITH_SHARED_LOCKS,
round<span class="token punctuation">(</span>ews.MIN_TIMER_READ_WITH_SHARED_LOCKS/1000000<span class="token punctuation">)</span> MIN_TIMER_READ_WITH_SHARED_LOCKS,
round<span class="token punctuation">(</span>ews.AVG_TIMER_READ_WITH_SHARED_LOCKS/1000000<span class="token punctuation">)</span> AVG_TIMER_READ_WITH_SHARED_LOCKS,
round<span class="token punctuation">(</span>ews.MAX_TIMER_READ_WITH_SHARED_LOCKS/1000000<span class="token punctuation">)</span> MAX_TIMER_READ_WITH_SHARED_LOCKS,
ews.COUNT_READ_NO_INSERT,
round<span class="token punctuation">(</span>ews.SUM_TIMER_READ_NO_INSERT/1000000<span class="token punctuation">)</span> SUM_TIMER_READ_NO_INSERT,
round<span class="token punctuation">(</span>ews.MIN_TIMER_READ_NO_INSERT/1000000<span class="token punctuation">)</span> MIN_TIMER_READ_NO_INSERT,
round<span class="token punctuation">(</span>ews.AVG_TIMER_READ_NO_INSERT/1000000<span class="token punctuation">)</span> AVG_TIMER_READ_NO_INSERT,
round<span class="token punctuation">(</span>ews.MAX_TIMER_READ_NO_INSERT/1000000<span class="token punctuation">)</span> MAX_TIMER_READ_NO_INSERT,
ews.COUNT_READ_EXTERNAL,
round<span class="token punctuation">(</span>ews.SUM_TIMER_READ_EXTERNAL/1000000<span class="token punctuation">)</span> SUM_TIMER_READ_EXTERNAL,
round<span class="token punctuation">(</span>ews.MIN_TIMER_READ_EXTERNAL/1000000<span class="token punctuation">)</span> MIN_TIMER_READ_EXTERNAL,
round<span class="token punctuation">(</span>ews.AVG_TIMER_READ_EXTERNAL/1000000<span class="token punctuation">)</span> AVG_TIMER_READ_EXTERNAL,
round<span class="token punctuation">(</span>ews.MAX_TIMER_READ_EXTERNAL/1000000<span class="token punctuation">)</span> MAX_TIMER_READ_EXTERNAL,
ews.COUNT_WRITE_ALLOW_WRITE,
round<span class="token punctuation">(</span>ews.SUM_TIMER_WRITE_ALLOW_WRITE/1000000<span class="token punctuation">)</span> SUM_TIMER_WRITE_ALLOW_WRITE,
round<span class="token punctuation">(</span>ews.MIN_TIMER_WRITE_ALLOW_WRITE/1000000<span class="token punctuation">)</span> MIN_TIMER_WRITE_ALLOW_WRITE,
round<span class="token punctuation">(</span>ews.AVG_TIMER_WRITE_ALLOW_WRITE/1000000<span class="token punctuation">)</span> AVG_TIMER_WRITE_ALLOW_WRITE,
round<span class="token punctuation">(</span>ews.MAX_TIMER_WRITE_ALLOW_WRITE/1000000<span class="token punctuation">)</span> MAX_TIMER_WRITE_ALLOW_WRITE,
ews.COUNT_WRITE_CONCURRENT_INSERT,
round<span class="token punctuation">(</span>ews.SUM_TIMER_WRITE_CONCURRENT_INSERT/1000000<span class="token punctuation">)</span> SUM_TIMER_WRITE_CONCURRENT_INSERT,
round<span class="token punctuation">(</span>ews.MIN_TIMER_WRITE_CONCURRENT_INSERT/1000000<span class="token punctuation">)</span> MIN_TIMER_WRITE_CONCURRENT_INSERT,
round<span class="token punctuation">(</span>ews.AVG_TIMER_WRITE_CONCURRENT_INSERT/1000000<span class="token punctuation">)</span> AVG_TIMER_WRITE_CONCURRENT_INSERT,
round<span class="token punctuation">(</span>ews.MAX_TIMER_WRITE_CONCURRENT_INSERT/1000000<span class="token punctuation">)</span> MAX_TIMER_WRITE_CONCURRENT_INSERT,
ews.COUNT_WRITE_NORMAL,
round<span class="token punctuation">(</span>ews.SUM_TIMER_WRITE_NORMAL/1000000<span class="token punctuation">)</span> SUM_TIMER_WRITE_NORMAL,
round<span class="token punctuation">(</span>ews.MIN_TIMER_WRITE_NORMAL/1000000<span class="token punctuation">)</span> MIN_TIMER_WRITE_NORMAL,
round<span class="token punctuation">(</span>ews.AVG_TIMER_WRITE_NORMAL/1000000<span class="token punctuation">)</span> AVG_TIMER_WRITE_NORMAL,
round<span class="token punctuation">(</span>ews.MAX_TIMER_WRITE_NORMAL/1000000<span class="token punctuation">)</span> MAX_TIMER_WRITE_NORMAL,
ews.COUNT_WRITE_EXTERNAL,
round<span class="token punctuation">(</span>ews.SUM_TIMER_WRITE_EXTERNAL/1000000<span class="token punctuation">)</span> SUM_TIMER_WRITE_EXTERNAL,
round<span class="token punctuation">(</span>ews.MIN_TIMER_WRITE_EXTERNAL/1000000<span class="token punctuation">)</span> MIN_TIMER_WRITE_EXTERNAL,
round<span class="token punctuation">(</span>ews.AVG_TIMER_WRITE_EXTERNAL/1000000<span class="token punctuation">)</span> AVG_TIMER_WRITE_EXTERNAL,
round<span class="token punctuation">(</span>ews.MAX_TIMER_WRITE_EXTERNAL/1000000<span class="token punctuation">)</span> MAX_TIMER_WRITE_EXTERNAL,
now<span class="token punctuation">(</span> <span class="token punctuation">)</span>
from performance_schema.table_lock_waits_summary_by_table ews
where ews.OBJECT_SCHEMA not <span class="token keyword">in</span> <span class="token punctuation">(</span><span class="token string">'perf_stat'</span>,<span class="token string">'mysql'</span>,<span class="token string">'sys'</span>,<span class="token string">'performance_schema'</span>,<span class="token string">'information_schema'</span><span class="token punctuation">)</span>
and ews.count_star <span class="token operator">&gt;</span> 0<span class="token punctuation">;</span> -- TOP 10 Table By PHYSICAL IO Wait
INSERT INTO sys.ews_file_io SELECT
file_name,
event_name,
COUNT_STAR,
SUM_NUMBER_OF_BYTES_READ,
SUM_NUMBER_OF_BYTES_WRITE,
NOW<span class="token punctuation">(</span> <span class="token punctuation">)</span>
FROM performance_schema.file_summary_by_instance<span class="token punctuation">;</span> COMMIT<span class="token punctuation">;</span>-- 异常回滚且记录日志 START TRANSACTION<span class="token punctuation">;</span>-- 设置删除事务
delete from sys.ews_event where insert_date <span class="token operator">&lt;</span> DATE_SUB<span class="token punctuation">(</span>CURDATE<span class="token punctuation">(</span><span class="token punctuation">)</span>,INTERVAL @del_day DAY<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">select</span> ROW_COUNT<span class="token punctuation">(</span><span class="token punctuation">)</span> into @affect_rows<span class="token punctuation">;</span>
insert into sys.error_log values<span class="token punctuation">(</span><span class="token string">'删除sys.ews_event成功'</span>,@affect_rows,now<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span> delete from sys.ews_sql where last_seen <span class="token operator">&lt;</span> DATE_SUB<span class="token punctuation">(</span>CURDATE<span class="token punctuation">(</span><span class="token punctuation">)</span>,INTERVAL @del_day DAY<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">select</span> ROW_COUNT<span class="token punctuation">(</span><span class="token punctuation">)</span> into @affect_rows<span class="token punctuation">;</span>
insert into sys.error_log values<span class="token punctuation">(</span><span class="token string">'删除sys.ews_sql成功'</span>,@affect_rows,now<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span> delete from sys.ews_table_iowaits where insert_date <span class="token operator">&lt;</span> DATE_SUB<span class="token punctuation">(</span>CURDATE<span class="token punctuation">(</span><span class="token punctuation">)</span>,INTERVAL @del_day DAY<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">select</span> ROW_COUNT<span class="token punctuation">(</span><span class="token punctuation">)</span> into @affect_rows<span class="token punctuation">;</span>
insert into sys.error_log values<span class="token punctuation">(</span><span class="token string">'删除sys.ews_table_iowaits成功'</span>,@affect_rows,now<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span> delete from sys.ews_index_iowaits where insert_date <span class="token operator">&lt;</span> DATE_SUB<span class="token punctuation">(</span>CURDATE<span class="token punctuation">(</span><span class="token punctuation">)</span>,INTERVAL @del_day DAY<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">select</span> ROW_COUNT<span class="token punctuation">(</span><span class="token punctuation">)</span> into @affect_rows<span class="token punctuation">;</span>
insert into sys.error_log values<span class="token punctuation">(</span><span class="token string">'删除sys.ews_index_iowaits成功'</span>,@affect_rows,now<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span> delete from sys.ews_table_lockwaits where insert_date <span class="token operator">&lt;</span> DATE_SUB<span class="token punctuation">(</span>CURDATE<span class="token punctuation">(</span><span class="token punctuation">)</span>,INTERVAL @del_day DAY<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">select</span> ROW_COUNT<span class="token punctuation">(</span><span class="token punctuation">)</span> into @affect_rows<span class="token punctuation">;</span>
insert into sys.error_log values<span class="token punctuation">(</span><span class="token string">'删除sys.ews_table_lockwaits成功'</span>,@affect_rows,now<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span> delete from sys.ews_file_io where insert_date <span class="token operator">&lt;</span> DATE_SUB<span class="token punctuation">(</span>CURDATE<span class="token punctuation">(</span><span class="token punctuation">)</span>,INTERVAL @del_day DAY<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">select</span> ROW_COUNT<span class="token punctuation">(</span><span class="token punctuation">)</span> into @affect_rows<span class="token punctuation">;</span>
insert into sys.error_log values<span class="token punctuation">(</span><span class="token string">'删除sys.ews_file_io成功'</span>,@affect_rows,now<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span> COMMIT<span class="token punctuation">;</span>-- 异常回滚且记录日志 IF
v_commit <span class="token operator">=</span> - 10 THEN
ROLLBACK<span class="token punctuation">;</span>
INSERT INTO sys.error_log VALUES <span class="token punctuation">(</span> msg, 0, now<span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
END IF<span class="token punctuation">;</span>

END //

DELIMITER;

3、定时任务

-- 定时执行存储过程
USE SYS;
DROP EVENT IF EXISTS ews;
CREATE EVENT ews ON SCHEDULE EVERY 10 MINUTE DO
CALL ews ( );

4、创建索引

考虑到后面查询基本是按日期做筛选来查的,所以建这个索引。

create index idx_date1 on ews_event(insert_date);
create index idx_date2 on ews_sql(last_seen);
create index idx_date3 on ews_table_iowaits(insert_date);
create index idx_date4 on ews_index_iowaits(insert_date);
create index idx_date5 on ews_table_lockwaits(insert_date);
create index idx_date6 on ews_file_io(insert_date);

5、数据查询

5.1、最近30分钟排名前十的等待事件

SELECT
event_name "等待事件",
max( count_star ) - min( count_star)+1 "等待次数",
round( ( max( sum_timer_wait ) - min( sum_timer_wait ) ) / 1000 ) "总时长(s)↓",
concat(
round(
(max( sum_timer_wait ) - min( sum_timer_wait ))/(
SELECT sum(ev) FROM
( SELECT (max(sum_timer_wait) - min( sum_timer_wait ) ) ev FROM sys.ews_event
WHERE insert_date >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE GROUP BY event_name ) c
) * 100,
2),
'%') "总时长占比",
max( avg_timer_wait ) - min( avg_timer_wait ) "平均等待时间(ms)"
FROM sys.ews_event
WHERE insert_date >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE
GROUP BY event_name
ORDER BY max( sum_timer_wait ) - min( sum_timer_wait ) DESC LIMIT 10;

5.2、最近30分钟总耗时排名前十的sql

SELECT
DIGEST "sql_id",
SCHEMA_NAME "数据库",
max( COUNT_STAR ) - min( COUNT_STAR )+1 "执行次数",
round( ( max( sum_timer_wait ) - min( sum_timer_wait ) ) / 1000 ) "总时长(s)↓",
concat(
round(
(max( sum_timer_wait ) - min( sum_timer_wait ))/(
SELECT sum(ev) FROM
( SELECT (max(sum_timer_wait) - min( sum_timer_wait ) ) ev FROM sys.ews_sql
WHERE last_seen >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE GROUP BY DIGEST) c
) * 100,
2),
'%') "总时长占比",
max( avg_timer_wait ) - min( avg_timer_wait ) "平均时长(ms)",
max( SUM_ROWS_EXAMINED ) - min( SUM_ROWS_EXAMINED ) "总检查行数",
max( LAST_SEEN ) "最后执行时间",
DIGEST_TEXT "sql语句"
FROM sys.ews_sql
WHERE last_seen >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE
GROUP BY DIGEST
ORDER BY max( sum_timer_wait ) - min( sum_timer_wait ) DESC LIMIT 10;

5.3、最近30分钟执行次数排名前十的sql

SELECT
SCHEMA_NAME "数据库",
DIGEST "sql_id",
max( COUNT_STAR ) - min( COUNT_STAR ) "执行次数↓",
concat(
round(
(max( COUNT_STAR ) - min( COUNT_STAR ))/(
SELECT sum(ev) FROM
( SELECT (max(COUNT_STAR) - min( COUNT_STAR ) ) ev FROM sys.ews_sql
WHERE last_seen >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE GROUP BY DIGEST) c
) * 100,
2),
'%') "执行次数占比",
max( sum_timer_wait ) - min( sum_timer_wait ) "总时长(ms)",
max( avg_timer_wait ) - min( avg_timer_wait ) "平均时长(ms)",
max( SUM_ROWS_EXAMINED ) - min( SUM_ROWS_EXAMINED ) "总检查行数",
max( LAST_SEEN ) "最后执行时间",
DIGEST_TEXT "sql语句"
FROM sys.ews_sql
WHERE last_seen >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE
GROUP BY DIGEST
ORDER BY max( COUNT_STAR ) - min( COUNT_STAR ) DESC LIMIT 10;

5.4、最近30分钟平均耗时最长的前十sql语句

SELECT
DIGEST "sql_id",
SCHEMA_NAME "数据库",
max( COUNT_STAR ) - min( COUNT_STAR ) "执行次数",
round( ( max( sum_timer_wait ) - min( sum_timer_wait ) ) / 1000 ) "总时长(s)",
max( avg_timer_wait ) - min( avg_timer_wait ) "平均时长(ms)↓",
concat(
round(
(max( avg_timer_wait ) - min( avg_timer_wait ))/(
SELECT sum(ev) FROM
( SELECT (max(avg_timer_wait) - min( avg_timer_wait ) ) ev FROM sys.ews_sql
WHERE last_seen >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE GROUP BY DIGEST) c
) * 100,
2),
'%') "平均时长占比",
max( SUM_ROWS_EXAMINED ) - min( SUM_ROWS_EXAMINED ) "总检查行数",
max( LAST_SEEN ) "最后执行时间",
DIGEST_TEXT "sql语句"
FROM sys.ews_sql
WHERE last_seen >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE
GROUP BY DIGEST
ORDER BY max( avg_timer_wait ) - min( avg_timer_wait ) DESC LIMIT 10;

5.5、最近30分钟逻辑IO总延时排名前十的表

SELECT
object_type "类型",
object_schema "数据库",
object_name "对象名",
max( count_star ) - min( count_star ) "执行总次数",
max( sum_timer_wait ) - min( sum_timer_wait ) "总延时(ms)↓",
concat(
round(
(max( sum_timer_wait ) - min( sum_timer_wait ))/(
SELECT sum(ev) FROM
( SELECT (max(sum_timer_wait) - min( sum_timer_wait ) ) ev FROM sys.ews_table_iowaits
WHERE insert_date >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE GROUP BY object_schema, object_name,object_type ) c
) * 100,
2),
'%') "总延时占比",
max( avg_timer_wait ) - min( avg_timer_wait ) "平均延时(ms)",
max( count_read ) - min( count_read ) "读次数",
max( sum_timer_read ) - min( sum_timer_read ) "读延时(ms)",
max( avg_timer_read ) - min( avg_timer_read ) "平均读延时(ms)",
max( count_write ) - min( count_write ) "写次数",
max( sum_timer_write ) - min( sum_timer_write ) "写延时(ms)",
max( avg_timer_write ) - min( avg_timer_write ) "平均写延时(ms)"
FROM sys.ews_table_iowaits
WHERE insert_date >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE
GROUP BY object_schema, object_name, object_type
ORDER BY max( sum_timer_wait ) - min( sum_timer_wait ) DESC LIMIT 10;

5.6、最近30分钟逻辑IO总延时排名前十的索引

SELECT
object_type "类型",
object_schema "数据库",
object_name "对象名称",
index_name "索引名称",
max( count_star ) - min( count_star ) "执行总次数",
max( sum_timer_wait ) - min( sum_timer_wait ) "总延时(ms)↓",
concat(
round(
(max( sum_timer_wait ) - min( sum_timer_wait ))/(
SELECT sum(ev) FROM
( SELECT (max(sum_timer_wait) - min( sum_timer_wait ) ) ev FROM sys.ews_index_iowaits
WHERE insert_date >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE and index_name is not null GROUP BY object_schema, object_name,index_name, object_type ) c
) * 100,
2),
'%') "总延时占比",
max( avg_timer_wait ) - min( avg_timer_wait ) "平均延时(ms)",
max( count_read ) - min( count_read ) "读次数",
max( sum_timer_read ) - min( sum_timer_read ) "读延时(ms)",
max( avg_timer_read ) - min( avg_timer_read ) "平均读延时(ms)",
max( count_write ) - min( count_write ) "写次数",
max( sum_timer_write ) - min( sum_timer_write ) "写延时(ms)",
max( avg_timer_write ) - min( avg_timer_write ) "平均写延时(ms)"
FROM sys.ews_index_iowaits
WHERE insert_date >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE and index_name is not null
GROUP BY object_schema, object_name, index_name, object_type
ORDER BY max( sum_timer_wait ) - min( sum_timer_wait ) DESC limit 10;

5.7、最近30分钟表锁耗时排名前十的表

SELECT
object_type "类型",
object_schema "数据库",
object_name "对象名称",
max( count_star ) - min( count_star ) "执行总次数",
max( sum_timer_wait ) - min( sum_timer_wait ) "总延时(ms)↓",
concat(
round(
(max( sum_timer_wait ) - min( sum_timer_wait ))/(
SELECT sum(ev) FROM
( SELECT (max(sum_timer_wait) - min( sum_timer_wait ) ) ev FROM sys.ews_table_lockwaits
WHERE insert_date >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE GROUP BY object_schema,object_name,object_type ) c
) * 100,
2),
'%') "总延时占比",
max( avg_timer_wait ) - min( avg_timer_wait ) "平均延时(ms)",
max( count_read ) - min( count_read ) "读次数",
max( sum_timer_read ) - min( sum_timer_read ) "读延时(ms)",
max( avg_timer_read ) - min( avg_timer_read ) "平均读延时(ms)",
max( count_write ) - min( count_write ) "写次数",
max( sum_timer_write ) - min( sum_timer_write ) "写延时(ms)",
max( avg_timer_write ) - min( avg_timer_write ) "平均写延时(ms)"
FROM sys.ews_table_lockwaits
WHERE insert_date >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE
GROUP BY object_schema, object_name, object_type
ORDER BY max( sum_timer_wait ) - min( sum_timer_wait ) DESC LIMIT 10;

5.8、最近30分钟物理IO排名前十的表

SELECT
FILE_NAME "文件名",
EVENT_NAME "等待事件",
max( COUNT_STAR ) - min( COUNT_STAR ) "文件I/0操作数量",
round( ( max( sum_number_of_bytes_read ) - min( sum_number_of_bytes_read ) ) / 1024 ) "文件读I/0数据量(KB)",
round( ( max( sum_number_of_bytes_write ) - min( sum_number_of_bytes_write ) ) / 1024 ) "文件写I/0数据量(KB)",
concat(
round(
(max( SUM_NUMBER_OF_BYTES_READ + SUM_NUMBER_OF_BYTES_WRITE ) - min( SUM_NUMBER_OF_BYTES_READ + SUM_NUMBER_OF_BYTES_WRITE ))/(
SELECT sum(ev) FROM
( SELECT (max( SUM_NUMBER_OF_BYTES_READ + SUM_NUMBER_OF_BYTES_WRITE ) - min( SUM_NUMBER_OF_BYTES_READ + SUM_NUMBER_OF_BYTES_WRITE )) ev FROM sys.ews_file_io
WHERE insert_date >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE GROUP BY FILE_NAME,EVENT_NAME)c
) * 100,
2),
'%') "文件I/0占比↓"
FROM sys.ews_file_io
WHERE insert_date >= CURRENT_TIMESTAMP - INTERVAL 30 MINUTE
GROUP BY FILE_NAME, EVENT_NAME
ORDER BY max( SUM_NUMBER_OF_BYTES_READ + SUM_NUMBER_OF_BYTES_WRITE ) - min( SUM_NUMBER_OF_BYTES_READ + SUM_NUMBER_OF_BYTES_WRITE ) DESC LIMIT 10;

5.9、未使用的索引

通过table_io_waits_summary_by_index_usage表可以获得系统运行到现在,哪些索引从来没有被用过。由于索引也会占用大量的空间,可以利用这个统计信息,结合一定的时间策略将无用的索引删除。

SELECT
OBJECT_SCHEMA "数据库",
OBJECT_NAME "对象名称",
INDEX_NAME "索引名称"
FROM
performance_schema.table_io_waits_summary_by_index_usage ews
WHERE
INDEX_NAME IS NOT NULL
AND COUNT_STAR = 0
AND OBJECT_SCHEMA not in ('perf_stat','mysql','sys','performance_schema','information_schema');

文章知识点与官方知识档案匹配,可进一步学习相关知识

[转帖]基于MySQL8.0存储过程实现myawr平台的top sql功能的更多相关文章

  1. 基于mysql-8.0.16-winx64的主从搭建

    1.主服务器的my.ini文件内容:[mysqld]# 主库和从库需要不一致server-id=1log-bin=mysql-bin# 同步的数据库binlog-do-db=master-slave# ...

  2. mysql8.0 Server 在Windows平台中的安装、初始化和远程访问设置

    mysql8.0 server安装 1.下载mysql 8.0 可以到mysql官网下载 https://dev.mysql.com/downloads/mysql 或者如下地址 mysql-8.0. ...

  3. 基于BPM的低代码开发平台应具备什么功能

    一个BPM平台应该具备什么样的功能    用户在选型BPM软件的时候往往不知道该关注哪些功能,什么样的BPM软件能满足国内企业应用需求,笔者从多年BPM研发和实施经验提炼了中国特色BPM应该具备的功能 ...

  4. MySQL8.0 存储引擎(InnoDB )buffer pool的实现原理

      数据库为了高效读取和存储物理数据,通常都会采用缓存的方式来弥补磁盘IO与CPU运算速度差.InnoDB 作为一个具有高可靠性和高性能的通用存储引擎也不例外,Buffer Pool就是其用来在内存中 ...

  5. 升级MySQL8.0的历险记

    最近忙于Fighting的项目,所以笔耕有些松懈,实为不该. 刚好遇到需要从MySQL5.7.33升级到MySQL8.0.x的需求,于是记录一下整个升级过程,踩坑而过. 背景梗概:本地docker容器 ...

  6. 基于Prometheus和Grafana的监控平台 - 运维告警

    通过前面几篇文章我们搭建好了监控环境并且监控了服务器.数据库.应用,运维人员可以实时了解当前被监控对象的运行情况,但是他们不可能时时坐在电脑边上盯着DashBoard,这就需要一个告警功能,当服务器或 ...

  7. docker中基于centos镜像部署lnmp环境 php7.3 mysql8.0 最新版

    Docker是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源. Docker可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的Linux机器上 ...

  8. [转帖]mysql8.0忘记密码如何操作?

    mysql8.0忘记密码如何操作? https://www.cnblogs.com/gspsuccess/p/11245314.html mark 一下 上次竟然不知道怎么弄. 很不幸,刚安装了MYS ...

  9. MySQL8.0新特性实验1

    Server层,选项持久化 mysql> show variables like '%max_connections%';+------------------------+-------+| ...

  10. Mysql8.0新特性【详细版本】

    1.  账户与安全 用户创建与授权 之前:创建用户并授权 1 grant all privileges on *.* to 'myuser'@'%' identified by '3edc#EDC'; ...

随机推荐

  1. JavaFx之WebView(二十五)

    JavaFx之WebView(二十五) jfx的web引擎已经几百年没更新,早就放弃了,写写demo还是不错.jdk8u202还能跑vue 3.0项目 import javafx.applicatio ...

  2. JavaScript异步编程3——Promise的链式使用

    目录 概述 详论 1️⃣回调地狱 2️⃣Promise实现 参考 概述 在上一篇文章<JavaScript异步编程2--结合XMLHttpRequest使用Promise>中,简要介绍了A ...

  3. 君子不玩物丧志,亦常以借物调心,网站集成二次元网页小组件(widget)石蒜模拟器,聊以赏玩

    传世经典<菜根谭>中有言曰:"徜徉于山林泉石之间,而尘心渐息:夷犹于诗书图画之内,而俗气潜消.故君子虽不玩物丧志,亦常借物调心."意思是,徜徉在林泉山石之间,能够摒弃杂 ...

  4. 云小课|MRS基础原理之Oozie任务调度

    阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说).深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云.更多精彩内容请单击此处. 摘要:Oozie是一个基 ...

  5. 云图说:云数据库 RDS for MySQL一键开通读写分离,轻松应对业务高峰期

    摘要:华为云数据库 RDS for MySQL提供一键开通读写分离功能,只需要一个连接地址,让您在业务高峰期不再迷茫,不再慌乱,so easy 的应对业务. 本文分享自华为云社区<云图说 | 第 ...

  6. 让数据库无惧灾难,华为云GaussDB同城双集群高可用方案正式发布!

    摘要:在华为全联接2021期间,华为云GaussDB(for openGauss)正式推出重大内核新特性--同城双集群高可用方案,提供金融级高可用服务,支持RPO=0 .RTO<60s的同城双集 ...

  7. iOS描述文件(.mobileprovision)一键申请

    转载:IOS描述文件制作教程 iOS描述文件(.mobileprovision)一键申请 在主界面上点击描述文件按钮. ​ 编辑切换为居中 添加图片注释,不超过 140 字(可选)     新建ios ...

  8. 从“概念”到“应用”,字节跳动基于 DataLeap 的 DataOps 实践

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 近日,火山引擎数智平台 VeDI Meetup「超话数据」在深圳举办,来自火山引擎的产品专家分享了字节跳动基于 D ...

  9. A/B 实验避坑指南:为什么不建议开 AABB 实验

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 本文将针对日常开设 A/B 实验过程中一个不太合理的使用方法--AABB 实验进行详细的解释,告诉大家为什么不建议 ...

  10. ThreadPoolExecutor 介绍

    线程池能够带来3个好处: 降低资源消耗:通过重复利用已创建的线程降低线程创建和销毁造成的消耗:提高响应速度:当任务到达时,任务可以不需要等到线程创建就能立即执行:提高线程的可管理性:线程是稀缺资源,如 ...