本文主要是介绍mysql performance schema 实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
参考MySQL调优性能监控之performance schema,做了一些扩展
1
2、哪类SQL的平均响应时间最多
SUM_NO_INDEX_USED>0
用来过滤那些没有使用的查询。
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语句如下:
--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,更新不频繁的业务增加缓存
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
做了扩展
@Cacheable(value = "acc:voucherGroup", key = "'vg_'+#asId+'_'+#id")@Overridepublic AccVoucherGroup selectByPrimaryKey(Integer asId, Integer id) {LambdaQueryWrapper<AccVoucherGroup> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(AccVoucherGroup::getAsId,asId);queryWrapper.eq(AccVoucherGroup::getId,id);return getOne(queryWrapper);}
注意删除、更新的时候需要更新缓存,防止数据不正确
@Caching(evict = {@CacheEvict(value = "acc:voucherGroup", key = "'vg_'+#asId+'_*")})
这篇关于mysql performance schema 实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!