MySql监控分析视图-sys schema

时间:2022-07-22
本文章向大家介绍MySql监控分析视图-sys schema,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

关于MySQL的性能监控和问题诊断,我们一般都从performance_schema中去获取想要的数据,在MySQL5.7.7版本中新增sys schema,它将performance_schema和information_schema中的数据以更容易理解的方式总结归纳为”视图”,其目的就是为了降低查询performance_schema的复杂度,让DBA能够快速的定位问题。今天我一起来看看这些库中都有哪些监控表和视图,掌握了这些,在我们开发和运维的过程中就起到了事半功倍的效果。

1. Sys schema视图摘要

1. 主机相关:以host_summary开头,主要汇总了IO延迟的信息。

2. Innodb相关:以innodb开头,汇总了innodb buffer信息和事务等待innodb锁的信息。

3. I/o相关:以io开头,汇总了等待I/O、I/O使用量情况。

4. 内存使用情况:以memory开头,从主机、线程、事件等角度展示内存的使用情况

5. 连接与会话信息:processlist和session相关视图,总结了会话相关信息。

6. 表相关:以schema_table开头的视图,展示了表的统计信息。

7. 索引信息:统计了索引的使用情况,包含冗余索引和未使用的索引情况。

8. 语句相关:以statement开头,包含执行全表扫描、使用临时表、排序等的语句信息。

9. 用户相关:以user开头的视图,统计了用户使用的文件I/O、执行语句统计信息。

10. 等待事件相关信息:以wait开头,展示等待事件的延迟情况。

2. Sys schema视图使用场景

索引情况

1. 查询冗余索引
select * from schema_redundant_indexes;
2. 查询未使用过得索引
select * from schema_unused_indexes;
3. 查询索引的使用情况
select index_name,rows_selected,rows_inserted,rows_updated,rows_deleted from schema_index_statistics where table_schema='dbname' ;

表相关

1. 查询表的访问量
select table_schema,table_name,sum(io_read_requests+io_write_requests) as io from schema_table_statistics group by table_schema,table_name order by io desc;
2. 查询占用bufferpool较多的表
select object_schema,object_name,allocated,data from innodb_buffer_stats_by_table order by allocated limit 10
3. 查看表的全表扫描情况
select * from statements_with_full_table_scans where db='dbname';

语句相关

1. 监控SQL执行的频率
select db,exec_count,query from statement_analysis order by exec_count desc
2. 监控使用了排序的SQL
select db,exec_count,first_seen,last_seen,query from statements_with_sorting limit 1;
3. 监控使用了临时表或者磁盘临时表的SQL
select db,exec_count,tmp_tables,tmp_disk_tables,query from statement_analysis where tmp_tables>0 or tmp_disk_tables >0 order by (tmp_tables+tmp_disk_tables) desc;

IO相关

1. 查看消耗磁盘IO的文件
select file,avg_read,avg_write,avg_read+avg_write as avg_io from io_global_by_file_by_bytes order by avg_read  limit 10;

Innodb 相关

1. 行锁阻塞情况
select * from innodb_lock_waits;

3. 风险提示

通过sys库去查询时,MySQL会消耗大量资源去收集相关信息,严重的可能会导致业务请求被阻塞,从而引起故障。建议生产上不要频繁的去查询sys或者performance_schema、information_schema来完成监控、巡检等工作。