ClickHouse

Query Memory Exhaustion Pattern

critical
Resource ContentionUpdated Jan 21, 2026

Queries exceeding memory limits cause 'Memory limit exceeded' errors, killing queries mid-execution. This often results from high-cardinality GROUP BY operations, large JOINs, or missing LIMIT clauses on exploratory queries.

How to detect:

Monitor query memory usage via system.query_log ProfileEvents['MemoryUsage'] and peak_memory_usage fields. Alert when queries approach max_memory_usage limit or when memory limit exceeded errors spike in system.errors table.

Recommended action:

Identify expensive queries via system.query_log ordering by memory_usage. Optimize high-cardinality GROUP BY using external aggregation (max_bytes_before_external_group_by). Use dictionaries instead of JOINs for dimension lookups. Add LIMIT clauses to exploratory queries. Increase max_memory_usage per-query or cluster-wide if legitimate.