以下示例演示如何使用 Performance Schema 和 sys schema 监控 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
schemamemory_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
schema 查询按代码区域汇总当前分配的内存 (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
schema 的更多信息,请参阅 第 30 章,MySQL sys Schema。