在上一篇《按 file 分组统计视图 | 全方位认识 sys 系统库》中,我们介绍了sys 系统库中按 file 分组统计的视图,本期的内容将为大家介绍内存事件和innodb buffer pool内存分配的统计视图。下面请跟随我们一起开始 sys 系统库的系统学习之旅吧。
PS:
按照schema分组的 InnoDB buffer pool统计信息,默认按照分配的buffer size大小降序排序--allocated字段。数据来源:information_schema.innodb_buffer_page
视图select语句文本如下:
# 不带x$前缀的视图select语句文本
SELECT IF(LOCATE('.', ibp.table_name) = 0, 'InnoDB System', REPLACE(SUBSTRING_INDEX(ibp.table_name, '.', 1), '`', '')) AS object_schema,
sys.format_bytes(SUM(IF(ibp.compressed_size = 0, 16384, compressed_size))) AS allocated,
sys.format_bytes(SUM(ibp.data_size)) AS data,
COUNT(ibp.page_number) AS pages,
COUNT(IF(ibp.is_hashed = 'YES', 1, NULL)) AS pages_hashed,
COUNT(IF(ibp.is_old = 'YES', 1, NULL)) AS pages_old,
ROUND(SUM(ibp.number_records)/COUNT(DISTINCT ibp.index_name)) AS rows_cached
FROM information_schema.innodb_buffer_page ibp
WHERE table_name IS NOT NULL
GROUP BY object_schema
ORDER BY SUM(IF(ibp.compressed_size = 0, 16384, compressed_size)) DESC;
# 带x$前缀的视图select语句文本
SELECT IF(LOCATE('.', ibp.table_name) = 0, 'InnoDB System', REPLACE(SUBSTRING_INDEX(ibp.table_name, '.', 1), '`', '')) AS object_schema,
SUM(IF(ibp.compressed_size = 0, 16384, compressed_size)) AS allocated,
SUM(ibp.data_size) AS data,
COUNT(ibp.page_number) AS pages,
COUNT(IF(ibp.is_hashed = 'YES', 1, NULL)) AS pages_hashed,
COUNT(IF(ibp.is_old = 'YES', 1, NULL)) AS pages_old,
ROUND(IFNULL(SUM(ibp.number_records)/NULLIF(COUNT(DISTINCT ibp.index_name), 0), 0)) AS rows_cached
FROM information_schema.innodb_buffer_page ibp
WHERE table_name IS NOT NULL
GROUP BY object_schema
ORDER BY SUM(IF(ibp.compressed_size = 0, 16384, compressed_size)) DESC;
下面我们看看使用该视图查询返回的结果。
# 不带x$前缀的视图
admin@localhost : sys 06:15:41> select * from innodb_buffer_stats_by_schema;
+---------------+------------+-----------+-------+--------------+-----------+-------------+
| object_schema | allocated | data | pages | pages_hashed | pages_old | rows_cached |
+---------------+------------+-----------+-------+--------------+-----------+-------------+
| InnoDB System | 23.73 MiB | 21.76 MiB | 1519 | 0 | 24 | 21474 |
| mysql | 240.00 KiB | 14.57 KiB | 15 | 0 | 15 | 179 |
| xiaoboluo | 128.00 KiB | 38.93 KiB | 8 | 0 | 5 | 982 |
| sys | 16.00 KiB | 354 bytes | 1 | 0 | 1 | 6 |
| 小萝卜 | 16.00 KiB | 135 bytes | 1 | 0 | 1 | 3 |
+---------------+------------+-----------+-------+--------------+-----------+-------------+
5 rows in set (0.43 sec)
# 带x$前缀的视图
admin@localhost : sys 06:15:54> select * from x$innodb_buffer_stats_by_schema;
+---------------+-----------+----------+-------+--------------+-----------+-------------+
| object_schema | allocated | data | pages | pages_hashed | pages_old | rows_cached |
+---------------+-----------+----------+-------+--------------+-----------+-------------+
| InnoDB System | 24887296 | 22809628 | 1519 | 0 | 24 | 21498 |
| mysql | 245760 | 14917 | 15 | 0 | 15 | 179 |
| xiaoboluo | 131072 | 39865 | 8 | 0 | 5 | 982 |
| sys | 16384 | 354 | 1 | 0 | 1 | 6 |
| 小萝卜 | 16384 | 135 | 1 | 0 | 1 | 3 |
+---------------+-----------+----------+-------+--------------+-----------+-------------+
5 rows in set (0.42 sec)
视图字段含义如下:
按照schema和表分组的 InnoDB buffer pool 统计信息,与sys.innodb_buffer_stats_by_schema视图类似,但是本视图是按照schema name和table name分组。数据来源:information_schema.innodb_buffer_page
视图select语句文本如下:
# 不带x$前缀的视图select语句文本
SELECT IF(LOCATE('.', ibp.table_name) = 0, 'InnoDB System', REPLACE(SUBSTRING_INDEX(ibp.table_name, '.', 1), '`', '')) AS object_schema,
REPLACE(SUBSTRING_INDEX(ibp.table_name, '.', -1), '`', '') AS object_name,
sys.format_bytes(SUM(IF(ibp.compressed_size = 0, 16384, compressed_size))) AS allocated,
sys.format_bytes(SUM(ibp.data_size)) AS data,
COUNT(ibp.page_number) AS pages,
COUNT(IF(ibp.is_hashed = 'YES', 1, NULL)) AS pages_hashed,
COUNT(IF(ibp.is_old = 'YES', 1, NULL)) AS pages_old,
ROUND(SUM(ibp.number_records)/COUNT(DISTINCT ibp.index_name)) AS rows_cached
FROM information_schema.innodb_buffer_page ibp
WHERE table_name IS NOT NULL
GROUP BY object_schema, object_name
ORDER BY SUM(IF(ibp.compressed_size = 0, 16384, compressed_size)) DESC;
# 带x$前缀的视图select语句文本
SELECT IF(LOCATE('.', ibp.table_name) = 0, 'InnoDB System', REPLACE(SUBSTRING_INDEX(ibp.table_name, '.', 1), '`', '')) AS object_schema,
REPLACE(SUBSTRING_INDEX(ibp.table_name, '.', -1), '`', '') AS object_name,
SUM(IF(ibp.compressed_size = 0, 16384, compressed_size)) AS allocated,
SUM(ibp.data_size) AS data,
COUNT(ibp.page_number) AS pages,
COUNT(IF(ibp.is_hashed = 'YES', 1, NULL)) AS pages_hashed,
COUNT(IF(ibp.is_old = 'YES', 1, NULL)) AS pages_old,
ROUND(IFNULL(SUM(ibp.number_records)/NULLIF(COUNT(DISTINCT ibp.index_name), 0), 0)) AS rows_cached
FROM information_schema.innodb_buffer_page ibp
WHERE table_name IS NOT NULL
GROUP BY object_schema, object_name
ORDER BY SUM(IF(ibp.compressed_size = 0, 16384, compressed_size)) DESC;
下面我们看看使用该视图查询返回的结果。
# 不带x$前缀的视图
root@localhost : sys 12:41:25> select * from innodb_buffer_stats_by_table limit 3;
+---------------+-------------+-----------+-----------+-------+--------------+-----------+-------------+
| object_schema | object_name | allocated | data | pages | pages_hashed | pages_old | rows_cached |
+---------------+-------------+-----------+-----------+-------+--------------+-----------+-------------+
| InnoDB System | SYS_TABLES | 11.58 MiB | 10.63 MiB | 741 | 0 | 3 | 36692 |
| luoxiaobo | t_luoxiaobo | 80.00 KiB | 29.21 KiB | 5 | 0 | 0 | 1658 |
| InnoDB System | SYS_COLUMNS | 48.00 KiB | 16.03 KiB | 3 | 0 | 3 | 239 |
+---------------+-------------+-----------+-----------+-------+--------------+-----------+-------------+
3 rows in set (0.12 sec)
# 带x$前缀的视图
root@localhost : sys 12:41:41> select * from x$innodb_buffer_stats_by_table limit 3;
+---------------+-------------+-----------+----------+-------+--------------+-----------+-------------+
| object_schema | object_name | allocated | data | pages | pages_hashed | pages_old | rows_cached |
+---------------+-------------+-----------+----------+-------+--------------+-----------+-------------+
| InnoDB System | SYS_TABLES | 12140544 | 11154757 | 741 | 0 | 3 | 36702 |
| luoxiaobo | t_luoxiaobo | 81920 | 29913 | 5 | 0 | 0 | 1658 |
| InnoDB System | SYS_COLUMNS | 49152 | 16412 | 3 | 0 | 3 | 239 |
+---------------+-------------+-----------+----------+-------+--------------+-----------+-------------+
3 rows in set (0.12 sec)
视图字段含义如下:
按照客户端主机名分组的内存使用统计信息,默认情况下按照当前内存使用量降序排序,数据来源:performance_schema.memory_summary_by_host_by_event_name
* 开启所有的memory类型的instruments:update setup_instruments set enabled='yes' where name like '%memory/%';
下面我们看看使用该视图查询返回的结果。
# 不带x$前缀的视图
admin@localhost : sys 10:01:37> select * from memory_by_host_by_current_bytes limit 3;
+-------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| host | current_count_used | current_allocated | current_avg_alloc | current_max_alloc | total_allocated |
+-------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| 10.10.20.14 | 58256 | 35.83 MiB | 645 bytes | 14.20 MiB | 2.36 GiB |
| localhost | 32 | 903.11 KiB | 28.22 KiB | 819.00 KiB | 7.69 MiB |
| background | 5 | 176 bytes | 35 bytes | 160 bytes | 352.57 KiB |
+-------------+--------------------+-------------------+-------------------+-------------------+-----------------+
3 rows in set (0.01 sec)
# 带x$前缀的视图
admin@localhost : sys 10:02:19> select * from x$memory_by_host_by_current_bytes limit 3;
+-------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| host | current_count_used | current_allocated | current_avg_alloc | current_max_alloc | total_allocated |
+-------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| 10.10.20.14 | 58256 | 37569266 | 644.8995 | 14885584 | 2538394110 |
| localhost | 18 | 891658 | 49536.5556 | 838656 | 9821551 |
| background | 5 | 176 | 35.2000 | 160 | 361068 |
+-------------+--------------------+-------------------+-------------------+-------------------+-----------------+
3 rows in set (0.00 sec)
视图字段含义如下:
按照thread ID分组的内存使用统计信息(只统计前台线程),默认情况下按照当前内存使用量进行降序排序,数据来源:performance_schema.memory_summary_by_thread_by_event_name、performance_schema.threads
下面我们看看使用该视图查询返回的结果。
# 不带x$前缀的视图
admin@localhost : sys 10:04:07> select * from memory_by_thread_by_current_bytes limit 3;
+-----------+--------------------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| thread_id | user | current_count_used | current_allocated | current_avg_alloc | current_max_alloc | total_allocated |
+-----------+--------------------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| 45 | admin@localhost | 34 | 4.91 MiB | 147.98 KiB | 4.00 MiB | 29.36 MiB |
| 41 | innodb/dict_stats_thread | 5 | 176 bytes | 35 bytes | 160 bytes | 346.31 KiB |
| 47 | admin@localhost | 3 | 112 bytes | 37 bytes | 80 bytes | 8.17 KiB |
+-----------+--------------------------+--------------------+-------------------+-------------------+-------------------+-----------------+
3 rows in set (0.13 sec)
# 带x$前缀的视图
admin@localhost : sys 10:04:58> select * from x$memory_by_thread_by_current_bytes limit 3;
+-----------+--------------------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| thread_id | user | current_count_used | current_allocated | current_avg_alloc | current_max_alloc | total_allocated |
+-----------+--------------------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| 45 | admin@localhost | 19 | 5102538 | 268554.6316 | 4194304 | 44995979 |
| 41 | innodb/dict_stats_thread | 5 | 176 | 35.2000 | 160 | 354620 |
| 47 | admin@localhost | 3 | 112 | 37.3333 | 80 | 8368 |
+-----------+--------------------------+--------------------+-------------------+-------------------+-------------------+-----------------+
3 rows in set (0.12 sec)
视图字段含义如下:
按照用户分组的内存使用统计信息,默认按照当前内存使用量进行降序排序,数据来源:performance_schema.memory_summary_by_user_by_event_name
下面我们看看使用该视图查询返回的结果。
# 不带x$前缀的视图
admin@localhost : sys 10:05:02> select * from memory_by_user_by_current_bytes limit 3;
+------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| user | current_count_used | current_allocated | current_avg_alloc | current_max_alloc | total_allocated |
+------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| admin | 58291 | 36.71 MiB | 660 bytes | 14.20 MiB | 2.41 GiB |
| background | 5 | 176 bytes | 35 bytes | 160 bytes | 358.17 KiB |
| qfsys | 0 | 0 bytes | 0 bytes | 0 bytes | 0 bytes |
+------------+--------------------+-------------------+-------------------+-------------------+-----------------+
3 rows in set (0.01 sec)
# 带x$前缀的视图
admin@localhost : sys 10:05:21> select * from x$memory_by_user_by_current_bytes limit 3;
+------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| user | current_count_used | current_allocated | current_avg_alloc | current_max_alloc | total_allocated |
+------------+--------------------+-------------------+-------------------+-------------------+-----------------+
| admin | 58278 | 38460932 | 659.9563 | 14885584 | 2586890836 |
| background | 5 | 176 | 35.2000 | 160 | 366828 |
| qfsys | 0 | 0 | 0.0000 | 0 | 0 |
+------------+--------------------+-------------------+-------------------+-------------------+-----------------+
3 rows in set (0.01 sec)
视图字段含义如下:
按照内存分配类型(事件类型)分组的内存使用统计信息,默认情况下按照当前内存使用量进行降序排序,数据来源:performance_schema.memory_summary_global_by_event_name
下面我们看看使用该视图查询返回的结果。
# 不带x$前缀的视图
admin@localhost : sys 10:05:24> select * from memory_global_by_current_bytes limit 3;
+-----------------------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
| event_name | current_count | current_alloc | current_avg_alloc | high_count | high_alloc | high_avg_alloc |
+-----------------------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
| memory/innodb/lock0lock | 9166 | 14.20 MiB | 1.59 KiB | 9166 | 14.20 MiB | 1.59 KiB |
| memory/performance_schema/events_statements_history_long | 1 | 13.66 MiB | 13.66 MiB | 1 | 13.66 MiB | 13.66 MiB |
| memory/performance_schema/events_statements_history_long.tokens | 1 | 9.77 MiB | 9.77 MiB | 1 | 9.77 MiB | 9.77 MiB |
+-----------------------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
3 rows in set (0.01 sec)
# 带x$前缀的视图
admin@localhost : sys 10:07:19> select * from x$memory_global_by_current_bytes limit 3;
+-----------------------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
| event_name | current_count | current_alloc | current_avg_alloc | high_count | high_alloc | high_avg_alloc |
+-----------------------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
| memory/innodb/lock0lock | 9166 | 14885584 | 1624.0000 | 9166 | 14885584 | 1624.0000 |
| memory/performance_schema/events_statements_history_long | 1 | 14320000 | 14320000.0000 | 1 | 14320000 | 14320000.0000 |
| memory/performance_schema/events_statements_history_long.tokens | 1 | 10240000 | 10240000.0000 | 1 | 10240000 | 10240000.0000 |
+-----------------------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
3 rows in set (0.00 sec)
视图字段含义如下:
当前总内存使用量统计(注意:只包含自memory类型的instruments启用以来被监控到的内存事件,在启用之前的无法监控,so..如果你不是在server启动之前就在配置文件中配置启动memory类型的instruments,那么此值可能并不可靠,当然如果你的server运行时间足够长,那么该值也具有一定参考价值),数据来源:performance_schema.memory_summary_global_by_event_name
下面我们看看使用该视图查询返回的结果。
# 不带x$前缀的视图
admin@localhost : sys 10:07:22> select * from memory_global_total limit 3;
+-----------------+
| total_allocated |
+-----------------+
| 168.91 MiB |
+-----------------+
1 row in set (0.01 sec)
# 带x$前缀的视图
admin@localhost : sys 10:08:24> select * from x$memory_global_total limit 3;
+-----------------+
| total_allocated |
+-----------------+
| 177099388 |
+-----------------+
1 row in set (0.00 sec)
视图字段含义如下:
本期内容就介绍到这里,本期内容参考链接如下:
https://dev.mysql.com/doc/refman/5.7/en/sys-memory-global-total.html
https://dev.mysql.com/doc/refman/5.7/en/sys-innodb-buffer-stats-by-table.html
https://dev.mysql.com/doc/refman/5.7/en/sys-memory-by-host-by-current-bytes.html
https://dev.mysql.com/doc/refman/5.7/en/sys-memory-by-thread-by-current-bytes.html
https://dev.mysql.com/doc/refman/5.7/en/sys-memory-by-user-by-current-bytes.html
https://dev.mysql.com/doc/refman/5.7/en/sys-memory-global-by-current-bytes.html
https://dev.mysql.com/doc/refman/5.7/en/sys-innodb-buffer-stats-by-schema.html
罗小波·沃趣科技高级数据库技术专家
IT从业多年,历任运维工程师,高级运维工程师,运维经理,数据库工程师,曾参与版本发布系统,轻量级监控系统,运维管理平台,数据库管理平台的设计与编写,熟悉MySQL的体系结构时,InnoDB存储引擎,喜好专研开源技术,追求完美。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有