10.12.3.2 MySQL 内存使用情况监控
以下示例演示如何使用Performance Schema和sys schema监控MySQL内存使用情况。
大多数Performance Schema内存instrumentation默认处于禁用状态。可以通过更新Performance Schemasetup_instruments
表的ENABLED
列来启用instrumentation。内存instrumentation名称以memory/code_area
/instrument_name的形式,where code_area
是值如sql
或innodb
,and instrument_name
是instrument detail。
-
要查看可用的MySQL内存instrumentation,可以查询Performance Schema
setup_instruments
表。以下查询返回所有代码区域的数百个内存instrumentation。mysql> SELECT * FROM performance_schema.setup_instruments WHERE NAME LIKE '%memory%';
可以通过指定代码区域来限制结果。例如,可以将结果限制到
InnoDB
内存instrumentation bằng cách指定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在启动时启用内存仪器确保了在启动时发生的内存分配被计数。
在重新启动服务器后,Performance Schema
setup_instruments
表的ENABLED
列应该报告内存仪器为YES
。Performance Schemasetup_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节,“Memory Summary Tables”。
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 Schema》。