// ClickHouse error reference

241: MEMORY_LIMIT_EXCEEDED (code 241)

The query would exceed the memory limit set by max_memory_usage or max_server_memory_usage.

오류 코드
241
이름
MEMORY_LIMIT_EXCEEDED
카테고리
Resource limits
오류 메시지
Memory limit (for query) exceeded: would use {bytes}

의미

MEMORY_LIMIT_EXCEEDED (241) fires when a query would push past max_memory_usage per query or the server total limit. The message shows the bytes it would have used. GROUP BY over wide data, ORDER BY without limits, and huge joins are the usual offenders.

일반적인 원인

  • A large GROUP BY or DISTINCT that does not fit the per-query limit.
  • ORDER BY over a big result without max_bytes_before_external_sort configured.
  • Many heavy queries at once exhausting the server total, not one query alone.

Find the cause

`system.query_log` column `memory_usage` per finished query, `system.metrics` and `system.processes` (or `SHOW PROCESSLIST`) to see live memory per query, `system.events` for `QueryMemoryLimitExceeded` count.

해결 방법

  1. Let GROUP BY spill to disk instead of aborting.

    SET max_bytes_before_external_group_by = 20000000000; SET max_memory_usage = 40000000000;
  2. Sort externally the same way.

    SET max_bytes_before_external_sort = 20000000000, max_bytes_ratio_before_external_sort = 0;
  3. Cap the result with LIMIT so ORDER BY keeps only the top rows in memory.

    SELECT * FROM big ORDER BY ts DESC LIMIT 100;
  4. Raise the per-query limit for a heavy session without touching the server default.

    SET max_memory_usage = 50000000000;

Other ways to handle it

SETTINGS max_memory_usage = ... per query is safer than editing user profiles. max_execution_time complements memory limits for runaway queries.

안전하게 재현하세요

groupArray materializes every value; the final array is about 80 MB for 10 million numbers and the query peaks near 257 MiB, so the 10 MB limit fires deterministically; a count() grouped by number % 1000 holds only a few thousand tiny states and never gets close.

SELECT groupArray(number) FROM numbers(10000000) SETTINGS max_memory_usage = 10000000; raises code 241.

질문

Should I just raise max_memory_usage?

Only if the server has the RAM. Prefer spilling to disk and LIMIT-first patterns; raising the limit moves the failure to the OS OOM killer.

Where do I see which query used the most memory?

SELECT query_id, memory_usage, query FROM system.query_log ORDER BY memory_usage DESC.

관련 오류

More resource limits errors

코드와 메시지 텍스트의 출처는 다음과 같습니다 ClickHouse documentation (system.errors). 전체 목록은 ClickHouse error list 또는 모든 데이터베이스 오류 검색.

// 오류는 줄이고, 수정은 빠르게

Work on ClickHouse in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for ClickHouse and the other databases you use.

이 페이지의 텍스트와 데이터: CC BY 4.0, 출처: SQLly (sqlly.app). 코드와 SQL 스니펫: MIT. 라이선스 세부 정보 인용된 오류 메시지 텍스트의 권리는 해당 데이터베이스 공급업체에 있습니다.