以下示例演示了如何使用 Performance Schema 和 sys 模式 监控 MySQL 内存使用情况。
默认情况下,大多数 Performance Schema 内存检测功能处于禁用状态。可以通过更新 Performance Schema setup_instruments
表的 ENABLED
列来启用检测功能。内存检测功能的名称格式为 memory/
,其中 code_area
/instrument_name
code_area
是一个值,例如 sql
或 innodb
,instrument_name
是检测功能的详细信息。
要查看可用的 MySQL 内存检测功能,请查询 Performance Schema
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
以及其他代码区域。要启用内存检测功能,请在 MySQL 配置文件中添加一个
performance-schema-instrument
规则。例如,要启用所有内存检测功能,请将以下规则添加到您的配置文件中,并重新启动服务器performance-schema-instrument='memory/%=COUNTED'
注意在启动时启用内存检测功能可以确保在启动时发生的内存分配被统计。
重新启动服务器后,Performance Schema
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 | ...
查询内存检测功能数据。在此示例中,内存检测功能数据是在 Performance Schema
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 模式。