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+'_*")
    })
相关推荐
萌面小侠Plus31 分钟前
Android笔记(三十三):封装设备性能级别判断工具——低端机还是高端机
android·性能优化·kotlin·工具类·低端机
慢慢成长的码农31 分钟前
Android Profiler 内存分析
android
大风起兮云飞扬丶32 分钟前
Android——多线程、线程通信、handler机制
android
L725638 分钟前
Android的Handler
android
清风徐来辽38 分钟前
Android HandlerThread 基础
android
齐 飞1 小时前
MongoDB笔记01-概念与安装
前端·数据库·笔记·后端·mongodb
云空1 小时前
《Python 与 SQLite:强大的数据库组合》
数据库·python·sqlite
暮毅1 小时前
10.Node.js连接MongoDb
数据库·mongodb·node.js
wowocpp1 小时前
ubuntu 22.04 server 格式化 磁盘 为 ext4 并 自动挂载 LTS
服务器·数据库·ubuntu
成富1 小时前
文本转SQL(Text-to-SQL),场景介绍与 Spring AI 实现
数据库·人工智能·sql·spring·oracle