概述

众所周知,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. SQL优化案例(1):隐式转换

    MySQL是当下最流行的关系型数据库之一,互联网高速发展的今天,MySQL数据库在电商.金融等诸多行业的生产系统中被广泛使用. 在实际的开发运维过程中,想必大家也常常会碰到慢SQL的困扰.一条性能不好 ...

  2. 终于卷完了!MySQL 打怪升级进阶成神之路(2023 最新版)!

    从第一篇文章开始,我们逐步详细介绍了 MySQL 数据库的基础知识,如:数据类型.存储引擎.性能优化(软.硬及sql语句),MySQL 数据库的高可用架构的部分,如:主从同步.读写分离的原理与实践.跨 ...

  3. Kubernetes架构及安装

    K8s架构 k8s内部是有几个组件的,分别是controller manager,api-server,scheduler,kubelet以及etcd,kube-proxy还有k8s客户端kubect ...

  4. 前端系列:ES6-ES12新语法

    目录 ECMAScript系列:简介 ECMAScript系列:ES6新特性 let 关键字 const 关键字 变量的解构赋值 模板字符串 简化对象写法 箭头函数 参数默认值 rest 参数 spr ...

  5. three.js中场景模糊、纹理失真的问题

    目录 1. 概述 2. 方案 2.1. 开启反走样 2.2. 开启HiDPI设置 3. 结果 4. 参考 1. 概述 在three.js场景中,有时会遇到场景模糊,纹理失真的现象,似乎three.js ...

  6. Rasa中的tracker_store和event_broker

      Rasa 中的 tracker_store 相对主流为 Redis,event_broker 相对主流为 RabbitMQ.后续为了研究学习直接将 tracker_store 和 event_br ...

  7. 网络ping不通,试试这8招

    摘要:网络ping不通,该怎么办?本文教你8个大招,轻松找到问题根源. 本文分享自华为云社区<网络ping不通,该怎么办?>,作者:wljslmz. 如下图,PC(192.168.10.1 ...

  8. CG行业云渲染服务的演进之路

    摘要:影视动画.特效制作等行业渲染需求量增多,4K/6K以及各高分辨率会陆续成为主流,本地算力与存储资源已无法满足现有任务量.而随着大环境的演变,CG行业发展已进入发展快车道.来自赞奇科技的CEO金伟 ...

  9. Hadoop中mapreduce作业日志是如何生成的

    摘要:本篇博客介绍了hadoop中mapreduce类型的作业日志是如何生成的.主要介绍日志生成的几个关键过程,不涉及过多细节性的内容. 本文分享自华为云社区<hadoop中mapreduce作 ...

  10. 用火山引擎DataTester,这家企业开始了“数据驱动增长”

    年末购物季已至,近些年来,预售抵扣.平台满减.品类专享券.大额补贴--动辄四五种计算方法叠加的大促活动,让不少消费者"懵"感十足.同一样商品,到底谁家卖的最便宜?比价平台应声发展而 ...