作者介绍:简历上没有一个精通的运维工程师,下面的思维导图也是预计更新的内容和当前进度(不定时更新)。

数据库是一个系统(应用)最重要的资产之一,所以我们的数据库将从以下几个数据库来进行介绍。
MySQL(本章节)
PostgreSQL
Redis
Etcd
上个小节我们介绍了MYSQL的系统库表(information_schema和performance_schema),本小节我们来介绍另外一个库表(sys),他的部分信息是基于以上2个库。
mysql> show tables;
+-----------------------------------------------+
| Tables_in_sys |
+-----------------------------------------------+
| host_summary |
| host_summary_by_file_io |
| host_summary_by_file_io_type |
| host_summary_by_stages |
| host_summary_by_statement_latency |
| host_summary_by_statement_type |
| innodb_buffer_stats_by_schema |
| innodb_buffer_stats_by_table |
| innodb_lock_waits |
| io_by_thread_by_latency |
| io_global_by_file_by_bytes |
| io_global_by_file_by_latency |
| io_global_by_wait_by_bytes |
| io_global_by_wait_by_latency |
| latest_file_io |
| memory_by_host_by_current_bytes |
| memory_by_thread_by_current_bytes |
| memory_by_user_by_current_bytes |
| memory_global_by_current_bytes |
| memory_global_total |
| metrics |
| processlist |
| ps_check_lost_instrumentation |
| schema_auto_increment_columns |
| schema_index_statistics |
| schema_object_overview |
| schema_redundant_indexes |
| schema_table_lock_waits |
| schema_table_statistics |
| schema_table_statistics_with_buffer |
| schema_tables_with_full_table_scans |
| schema_unused_indexes |
| session |
| session_ssl_status |
| statement_analysis |
| statements_with_errors_or_warnings |
| statements_with_full_table_scans |
| statements_with_runtimes_in_95th_percentile |
| statements_with_sorting |
| statements_with_temp_tables |
| sys_config |
| user_summary |
| user_summary_by_file_io |
| user_summary_by_file_io_type |
| user_summary_by_stages |
| user_summary_by_statement_latency |
| user_summary_by_statement_type |
| version |
| wait_classes_global_by_avg_latency |
| wait_classes_global_by_latency |
| waits_by_host_by_latency |
| waits_by_user_by_latency |
| waits_global_by_latency |
| x$host_summary |
| x$host_summary_by_file_io |
| x$host_summary_by_file_io_type |
| x$host_summary_by_stages |
| x$host_summary_by_statement_latency |
| x$host_summary_by_statement_type |
| x$innodb_buffer_stats_by_schema |
| x$innodb_buffer_stats_by_table |
| x$innodb_lock_waits |
| x$io_by_thread_by_latency |
| x$io_global_by_file_by_bytes |
| x$io_global_by_file_by_latency |
| x$io_global_by_wait_by_bytes |
| x$io_global_by_wait_by_latency |
| x$latest_file_io |
| x$memory_by_host_by_current_bytes |
| x$memory_by_thread_by_current_bytes |
| x$memory_by_user_by_current_bytes |
| x$memory_global_by_current_bytes |
| x$memory_global_total |
| x$processlist |
| x$ps_digest_95th_percentile_by_avg_us |
| x$ps_digest_avg_latency_distribution |
| x$ps_schema_table_statistics_io |
| x$schema_flattened_keys |
| x$schema_index_statistics |
| x$schema_table_lock_waits |
| x$schema_table_statistics |
| x$schema_table_statistics_with_buffer |
| x$schema_tables_with_full_table_scans |
| x$session |
| x$statement_analysis |
| x$statements_with_errors_or_warnings |
| x$statements_with_full_table_scans |
| x$statements_with_runtimes_in_95th_percentile |
| x$statements_with_sorting |
| x$statements_with_temp_tables |
| x$user_summary |
| x$user_summary_by_file_io |
| x$user_summary_by_file_io_type |
| x$user_summary_by_stages |
| x$user_summary_by_statement_latency |
| x$user_summary_by_statement_type |
| x$wait_classes_global_by_avg_latency |
| x$wait_classes_global_by_latency |
| x$waits_by_host_by_latency |
| x$waits_by_user_by_latency |
| x$waits_global_by_latency |
+-----------------------------------------------+
101 rows in set (0.00 sec)
1. 性能概览视图
host_summary - 按主机分组的性能概览(连接数、语句执行、文件I/O等)
user_summary - 按用户分组的性能概览(连接数、语句执行、文件I/O等)
processlist - 增强的进程列表,包含更多性能信息
session - 当前会话的详细性能统计
metrics - 关键性能指标的简明视图
version - sys库的版本信息
注:这些视图提供数据库整体性能的快速概览。
2. 语句性能分析
statement_analysis - SQL语句的详细性能分析(执行次数、延迟、扫描行数等)
statements_with_errors_or_warnings - 产生错误或警告的语句统计
statements_with_full_table_scans - 执行全表扫描的SQL语句
statements_with_runtimes_in_95th_percentile - 执行时间在95%分位以上的慢查询
statements_with_sorting - 包含排序操作的语句统计
statements_with_temp_tables - 使用临时表的语句统计
注:用于分析SQL语句执行效率和优化点。
3. 架构和索引分析
schema_auto_increment_columns - 所有表的自增列当前状态
schema_index_statistics - 索引使用频率和性能统计
schema_redundant_indexes - 识别可以删除的冗余索引
schema_table_statistics - 表级别的读写统计
schema_table_statistics_with_buffer - 包含缓冲池信息的表统计
schema_tables_with_full_table_scans - 识别频繁全表扫描的表
schema_unused_indexes - 识别从未使用过的索引
schema_table_lock_waits - 当前表级锁等待情况
注:用于数据库架构优化和索引管理。
4. InnoDB相关分析
innodb_buffer_stats_by_schema - 按数据库统计的InnoDB缓冲池使用情况
innodb_buffer_stats_by_table - 按表统计的InnoDB缓冲池使用情况
innodb_lock_waits - 当前InnoDB锁等待关系链
注:专门针对InnoDB存储引擎的性能分析。
5. I/O性能分析
io_global_by_file_by_bytes - 全局文件I/O吞吐量统计
io_global_by_file_by_latency - 全局文件I/O延迟统计
io_global_by_wait_by_bytes - 按等待事件分组的I/O吞吐量
io_global_by_wait_by_latency - 按等待事件分组的I/O延迟
latest_file_io - 最近发生的文件I/O操作记录
注:用于磁盘I/O性能分析和瓶颈定位。
6. 内存使用分析
memory_global_by_current_bytes - 全局内存使用分布(按事件类型)
memory_global_total - 服务器总内存使用量
memory_by_host_by_current_bytes - 按主机分组的内存分配情况
memory_by_user_by_current_bytes - 按用户分组的内存分配情况
memory_by_thread_by_current_bytes - 按线程分组的内存分配情况
注:用于内存使用情况监控和优化。
7. 等待事件分析
wait_classes_global_by_avg_latency - 等待类别平均延迟统计
wait_classes_global_by_latency - 等待类别总延迟统计
waits_global_by_latency - 全局等待事件统计
waits_by_host_by_latency - 按主机分组的等待事件统计
waits_by_user_by_latency - 按用户分组的等待事件统计
注:用于分析数据库资源等待情况。
8. 详细分组统计
host_summary_by_file_io - 按主机分组的文件I/O统计
host_summary_by_statement_latency - 按主机分组的语句延迟统计
user_summary_by_file_io - 按用户分组的文件I/O统计
user_summary_by_statement_latency - 按用户分组的语句延迟统计
注:提供更细粒度的分组性能统计。
9. 原始数据视图(x$系列)
x$host_summary - host_summary的原始数据版本(未格式化)
x$statement_analysis - statement_analysis的原始数据版本(未格式化)
x$innodb_buffer_stats_by_table - innodb_buffer_stats_by_table的原始数据版本
注:所有以x$开头的表都是对应视图的原始数据版本,适合程序化处理。
10. 系统工具和配置
sys_config - sys库的配置参数表
ps_check_lost_instrumentation - 检查performance_schema中丢失的监控点
session_ssl_status - 会话的SSL连接状态信息
注:系统维护和配置相关工具。
-- 查看最耗时的SQL
SELECT * FROM statement_analysis ORDER BY avg_latency DESC LIMIT 10;
-- 查看冗余索引
SELECT * FROM schema_redundant_indexes;
-- 查看内存使用情况
SELECT * FROM memory_global_by_current_bytes LIMIT 10;
-- 查看锁等待
SELECT * FROM innodb_lock_waits;
-- 查看全表扫描的语句
SELECT * FROM statements_with_full_table_scans;
-- 查看未使用的索引
SELECT * FROM schema_unused_indexes;sys 库是MySQL性能诊断和优化的利器,通过友好的视图将复杂的性能数据转化为易于理解的信息。