跳到主要内容

调试内存问题

当遇到内存问题或内存泄漏时,了解哪些查询和资源正在消耗大量的内存是很有帮助的。以下是一些可以帮助调试和找出哪些查询、数据库和表可以优化的查询。

按峰值内存使用情况列出当前运行的进程

SELECT
initial_query_id,
query,
elapsed,
formatReadableSize(memory_usage),
formatReadableSize(peak_memory_usage),
FROM system.processes
ORDER BY peak_memory_usage DESC
LIMIT 100;

列出内存使用情况的指标

SELECT
metric, description, formatReadableSize(value) size
FROM
system.asynchronous_metrics
WHERE
metric like '%Cach%'
or metric like '%Mem%'
order by
value desc;

按当前内存使用情况列出表

SELECT
database,
name,
formatReadableSize(total_bytes)
FROM system.tables
WHERE engine IN ('Memory','Set','Join');

输出合并使用的总内存

SELECT formatReadableSize(sum(memory_usage)) FROM system.merges;

输出当前运行的进程使用的总内存

SELECT formatReadableSize(sum(memory_usage)) FROM system.processes;

输出字典使用的总内存

SELECT formatReadableSize(sum(bytes_allocated)) FROM system.dictionaries;

输出主键使用的总内存

SELECT
sumIf(data_uncompressed_bytes, part_type = 'InMemory') as memory_parts,
formatReadableSize(sum(primary_key_bytes_in_memory)) AS primary_key_bytes_in_memory,
formatReadableSize(sum(primary_key_bytes_in_memory_allocated)) AS primary_key_bytes_in_memory_allocated
FROM system.parts;