mysql performance schema 实践

参考MySQL调优性能监控之performance schema,做了一些扩展

1

2、哪类SQL的平均响应时间最多
SUM_NO_INDEX_USED>0用来过滤那些没有使用的查询。

sql 复制代码
SELECT SCHEMA_NAME,DIGEST_TEXT,AVG_TIMER_WAIT,MAX_TIMER_WAIT,SUM_LOCK_TIME,SUM_ERRORS
,SUM_SELECT_FULL_JOIN,SUM_NO_INDEX_USED
FROM events_statements_summary_by_digest 
where SCHEMA_NAME='acc' 
-- and SUM_SELECT_FULL_JOIN>0
and SUM_NO_INDEX_USED>0
ORDER BY COUNT_STAR DESC

基本的sql语句如下:

sql 复制代码
--1、哪类的SQL执行最多?
SELECT DIGEST_TEXT,COUNT_STAR,FIRST_SEEN,LAST_SEEN FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--2、哪类SQL的平均响应时间最多?
SELECT DIGEST_TEXT,AVG_TIMER_WAIT FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--3、哪类SQL排序记录数最多?
SELECT DIGEST_TEXT,SUM_SORT_ROWS FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--4、哪类SQL扫描记录数最多?
SELECT DIGEST_TEXT,SUM_ROWS_EXAMINED FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--5、哪类SQL使用临时表最多?
SELECT DIGEST_TEXT,SUM_CREATED_TMP_TABLES,SUM_CREATED_TMP_DISK_TABLES FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--6、哪类SQL返回结果集最多?
SELECT DIGEST_TEXT,SUM_ROWS_SENT FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--7、哪个表物理IO最多?
SELECT file_name,event_name,SUM_NUMBER_OF_BYTES_READ,SUM_NUMBER_OF_BYTES_WRITE FROM file_summary_by_instance ORDER BY SUM_NUMBER_OF_BYTES_READ + SUM_NUMBER_OF_BYTES_WRITE DESC
--8、哪个表逻辑IO最多?
SELECT object_name,COUNT_READ,COUNT_WRITE,COUNT_FETCH,SUM_TIMER_WAIT FROM table_io_waits_summary_by_table ORDER BY sum_timer_wait DESC
--9、哪个索引访问最多?
SELECT OBJECT_NAME,INDEX_NAME,COUNT_FETCH,COUNT_INSERT,COUNT_UPDATE,COUNT_DELETE FROM table_io_waits_summary_by_index_usage ORDER BY SUM_TIMER_WAIT DESC
--10、哪个索引从来没有用过?
SELECT OBJECT_SCHEMA,OBJECT_NAME,INDEX_NAME FROM table_io_waits_summary_by_index_usage WHERE INDEX_NAME IS NOT NULL AND COUNT_STAR = 0 AND OBJECT_SCHEMA <> 'mysql' ORDER BY OBJECT_SCHEMA,OBJECT_NAME;
--11、哪个等待事件消耗时间最多?
SELECT EVENT_NAME,COUNT_STAR,SUM_TIMER_WAIT,AVG_TIMER_WAIT FROM events_waits_summary_global_by_event_name WHERE event_name != 'idle' ORDER BY SUM_TIMER_WAIT DESC
--12-1、剖析某条SQL的执行情况,包括statement信息,stege信息,wait信息
SELECT EVENT_ID,sql_text FROM events_statements_history WHERE sql_text LIKE '%count(*)%';
--12-2、查看每个阶段的时间消耗
SELECT event_id,EVENT_NAME,SOURCE,TIMER_END - TIMER_START FROM events_stages_history_long WHERE NESTING_EVENT_ID = 1553;
--12-3、查看每个阶段的锁等待情况
SELECT event_id,event_name,source,timer_wait,object_name,index_name,operation,nesting_event_id FROM events_waits_history_longWHERE nesting_event_id = 1553;

有了上面的基础开始优化调整
1 执行频繁的sql,更新不频繁的业务增加缓存

sql 复制代码
SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,FIRST_SEEN,LAST_SEEN 
FROM events_statements_summary_by_digest 
where schema_name='acc' and date_format(LAST_SEEN,'%Y-%m-%d')='2024-04-13'
ORDER BY COUNT_STAR DESC

加上缓存,这里借助了spring cache管理组件,基于l2cahe做了扩展

java 复制代码
 @Cacheable(value = "acc:voucherGroup", key = "'vg_'+#asId+'_'+#id")
    @Override
    public AccVoucherGroup selectByPrimaryKey(Integer asId, Integer id) {
        LambdaQueryWrapper<AccVoucherGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccVoucherGroup::getAsId,asId);
        queryWrapper.eq(AccVoucherGroup::getId,id);
        return getOne(queryWrapper);
    }

注意删除、更新的时候需要更新缓存,防止数据不正确

java 复制代码
@Caching(evict = {
            @CacheEvict(value = "acc:voucherGroup", key = "'vg_'+#asId+'_*")
    })
相关推荐
安迁岚几秒前
【SQL Server】华中农业大学空间数据库实验报告 实验三 数据操作
运维·服务器·数据库·sql·mysql
安迁岚2 分钟前
【SQL Server】华中农业大学空间数据库实验报告 实验九 触发器
数据库·sql·mysql·oracle·实验报告
Loganer3 分钟前
MongoDB分片集群搭建
数据库·mongodb
LKID体7 分钟前
Python操作neo4j库py2neo使用之创建和查询(二)
数据库·python·neo4j
刘大浪32 分钟前
后端数据增删改查基于Springboot+mybatis mysql 时间根据当时时间自动填充,数据库连接查询不一致,mysql数据库连接不好用
数据库·spring boot·mybatis
无敌岩雀39 分钟前
MySQL中的索引
数据库·mysql
a_安徒生1 小时前
linux安装TDengine
linux·数据库·tdengine
东阳马生架构1 小时前
MySQL原理简介—1.SQL的执行流程
mysql
程序员学习随笔1 小时前
PostgreSQL技术内幕19:逻辑备份工具pg_dump、pg_dumpall
数据库·postgresql
尘浮生2 小时前
Java项目实战II基于微信小程序的校运会管理系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea