MySQL内存分析常用语句
一、 OS层
首先需要确认是否是MySQL占用的内存
top -p $(pidof mysqld) -n 1
或者
cat /proc/$(pidof mysqld)/status
二、 DB层
1. 全局情况
各种buffer pool大小,是否有明显不合理的设置
SHOW GLOBAL VARIABLES LIKE '%buffer%szie%';
MySQL总占用内存
SELECT * FROM sys.memory_global_total;
2. 内存占用详情
按内存模块
SELECT
EVENT_NAME,
CURRENT_NUMBER_OF_BYTES_USED AS memory_bytes,
CURRENT_NUMBER_OF_BYTES_USED / 1024 / 1024 AS memory_mb
FROM
performance_schema.memory_summary_global_by_event_name
WHERE
CURRENT_NUMBER_OF_BYTES_USED > 0
ORDER BY
CURRENT_NUMBER_OF_BYTES_USED DESC
LIMIT 10;
各模块内存使用变化(结果是累计值,可以根据差值观察变化情况)
多次查询,求差值
SELECT
EVENT_NAME,
SUM(SUM_NUMBER_OF_BYTES_ALLOC) / 1024 / 1024 AS total_memory_mb
FROM
performance_schema.memory_summary_global_by_event_name
GROUP BY
EVENT_NAME
ORDER BY
SUM_NUMBER_OF_BYTES_ALLOC DESC
LIMIT 10;
按线程查看内存占用
SELECT
m.EVENT_NAME,
m.COUNT_ALLOC,
m.CURRENT_NUMBER_OF_BYTES_USED AS mem_sum,
(m.CURRENT_NUMBER_OF_BYTES_USED / 1024 / 1024.0) AS mem_sum_mb,
t.NAME,
t.TYPE,
t.PROCESSLIST_ID,
LEFT(t.PROCESSLIST_INFO, 10)
FROM
performance_schema.memory_summary_by_thread_by_event_name m
JOIN performance_schema.threads t
USING (THREAD_ID)
WHERE
t.PROCESSLIST_ID != CONNECTION_ID()
ORDER BY
m.CURRENT_NUMBER_OF_BYTES_USED desc
LIMIT 10;
sys库中的视图基于 performance_schema 提供了更易读和易用的性能数据汇总
SELECT * FROM sys.memory_by_thread_by_current_bytes LIMIT 10;
内存分布详情
SELECT * FROM sys.memory_global_by_current_bytes order by current_alloc desc LIMIT 10;
按用户排序
SELECT * FROM sys.memory_by_user_by_current_bytes;
参考
GreatSQL 运行时内存太高,超过90%怎么办(重发,附解决办法)-CSDN博客
MySQL内存使用率高且不释放问题排查与总结_Mysql_脚本之家
MySQL 8.0不再担心被垃圾SQL搞爆内存 - GreatSQL - 博客园
MySQL内存为什么不断增高,怎么让它释放 - 墨天轮
MySQL进程内存一直在涨,怎么办? - 墨天轮