以下示例演示如何使用 性能架构 和 sys 架构 监控 MySQL 内存使用情况。
大多数性能架构内存仪表默认情况下是禁用的。仪表可以通过更新性能架构 setup_instruments
表的 ENABLED
列来启用。内存仪表的名称以 memory/
形式命名,其中 code_area
/instrument_name
code_area
是一个值,例如 sql
或 innodb
,instrument_name
是仪表详细信息。
-
要查看可用的 MySQL 内存仪表,请查询性能架构
setup_instruments
表。以下查询将返回所有代码区域的数百个内存仪表。mysql> SELECT * FROM performance_schema.setup_instruments WHERE NAME LIKE '%memory%';
您可以通过指定代码区域来缩小结果。例如,您可以将结果限制为
InnoDB
内存仪表,方法是指定innodb
作为代码区域。mysql> SELECT * FROM performance_schema.setup_instruments WHERE NAME LIKE '%memory/innodb%'; +-------------------------------------------+---------+-------+ | NAME | ENABLED | TIMED | +-------------------------------------------+---------+-------+ | memory/innodb/adaptive hash index | NO | NO | | memory/innodb/buf_buf_pool | NO | NO | | memory/innodb/dict_stats_bg_recalc_pool_t | NO | NO | | memory/innodb/dict_stats_index_map_t | NO | NO | | memory/innodb/dict_stats_n_diff_on_level | NO | NO | | memory/innodb/other | NO | NO | | memory/innodb/row_log_buf | NO | NO | | memory/innodb/row_merge_sort | NO | NO | | memory/innodb/std | NO | NO | | memory/innodb/trx_sys_t::rw_trx_ids | NO | NO | ...
根据您的 MySQL 安装,代码区域可能包括
performance_schema
、sql
、client
、innodb
、myisam
、csv
、memory
、blackhole
、archive
、partition
等。 -
要启用内存仪表,请将
performance-schema-instrument
规则添加到您的 MySQL 配置文件中。例如,要启用所有内存仪表,请将以下规则添加到配置文件中,然后重新启动服务器:performance-schema-instrument='memory/%=COUNTED'
Note在启动时启用内存仪表,以确保在启动时的内存分配被计数。
重新启动服务器后,性能架构
setup_instruments
表的ENABLED
列应该报告您启用的内存仪表为YES
。性能架构setup_instruments
表的TIMED
列将被忽略,因为内存操作不计时。mysql> SELECT * FROM performance_schema.setup_instruments WHERE NAME LIKE '%memory/innodb%'; +-------------------------------------------+---------+-------+ | NAME | ENABLED | TIMED | +-------------------------------------------+---------+-------+ | memory/innodb/adaptive hash index | NO | NO | | memory/innodb/buf_buf_pool | NO | NO | | memory/innodb/dict_stats_bg_recalc_pool_t | NO | NO | | memory/innodb/dict_stats_index_map_t | NO | NO | | memory/innodb/dict_stats_n_diff_on_level | NO | NO | | memory/innodb/other | NO | NO | | memory/innodb/row_log_buf | NO | NO | | memory/innodb/row_merge_sort | NO | NO | | memory/innodb/std | NO | NO | | memory/innodb/trx_sys_t::rw_trx_ids | NO | NO | ...
-
查询内存仪表数据。在这个示例中,内存仪表数据被查询在性能架构
memory_summary_global_by_event_name
表中,该表按EVENT_NAME
汇总数据。EVENT_NAME
是仪表的名称。以下查询返回
InnoDB
缓冲池的内存数据。有关列描述,请参阅 第 29.12.20.10 节,“内存摘要表”。mysql> SELECT * FROM performance_schema.memory_summary_global_by_event_name WHERE EVENT_NAME LIKE 'memory/innodb/buf_buf_pool'\G EVENT_NAME: memory/innodb/buf_buf_pool COUNT_ALLOC: 1 COUNT_FREE: 0 SUM_NUMBER_OF_BYTES_ALLOC: 137428992 SUM_NUMBER_OF_BYTES_FREE: 0 LOW_COUNT_USED: 0 CURRENT_COUNT_USED: 1 HIGH_COUNT_USED: 1 LOW_NUMBER_OF_BYTES_USED: 0 CURRENT_NUMBER_OF_BYTES_USED: 137428992 HIGH_NUMBER_OF_BYTES_USED: 137428992
同样的基础数据可以使用
sys
架构memory_global_by_current_bytes
表查询,该表显示服务器全局当前内存使用情况,按分配类型划分。mysql> SELECT * FROM sys.memory_global_by_current_bytes WHERE event_name LIKE 'memory/innodb/buf_buf_pool'\G *************************** 1. row *************************** event_name: memory/innodb/buf_buf_pool current_count: 1 current_alloc: 131.06 MiB current_avg_alloc: 131.06 MiB high_count: 1 high_alloc: 131.06 MiB high_avg_alloc: 131.06 MiB
这个
sys
架构查询聚合当前分配的内存 (current_alloc
) 按代码区域:mysql> SELECT SUBSTRING_INDEX(event_name,'/',2) AS code_area, FORMAT_BYTES(SUM(current_alloc)) AS current_alloc FROM sys.x$memory_global_by_current_bytes GROUP BY SUBSTRING_INDEX(event_name,'/',2) ORDER BY SUM(current_alloc) DESC; +---------------------------+---------------+ | code_area | current_alloc | +---------------------------+---------------+ | memory/innodb | 843.24 MiB | | memory/performance_schema | 81.29 MiB | | memory/mysys | 8.20 MiB | | memory/sql | 2.47 MiB | | memory/memory | 174.01 KiB | | memory/myisam | 46.53 KiB | | memory/blackhole | 512 bytes | | memory/federated | 512 bytes | | memory/csv | 512 bytes | | memory/vio | 496 bytes | +---------------------------+---------------+
有关
sys
架构的更多信息,请参阅 第 30 章,《MySQL sys 架构》。