跳到主要内容
跳到主要内容

调试内存问题

在遇到内存问题或内存泄漏时,了解哪些查询和资源占用了大量内存是非常有帮助的。以下是一些查询,可以帮助您通过查找哪些查询、数据库和表可以进行优化来调试内存问题:

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

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,
    formatReadableSize(sum(index_granularity_bytes_in_memory)) AS index_granularity_bytes_in_memory,
    formatReadableSize(sum(index_granularity_bytes_in_memory_allocated)) AS index_granularity_bytes_in_memory_allocated
FROM system.parts;