Database performance

Budgeting Memory for ClickHouse IN

Keep large ClickHouse IN subqueries within a memory budget using disk spilling, careful thresholds and workload tests that include storage and concurrency.
By MR Solutions
5 min readPublished
Amber tiles stacked in a transparent container, with turquoise tiles travelling along a curved channel onto a storage platter.

When a report outgrows its memory budget

A report can return a single number while needing a large working set to calculate it. If an IN subquery collects years of customer, account or address keys, the operational question is whether that report can finish within the resources available alongside other work.

ClickHouse 26.10 introduces disk spilling for sets built by IN subqueries. That gives teams another option before increasing memory allowances. Start by reducing unnecessary work, then test spilling against a defined memory budget, response-time target and temporary-storage allowance.

Find the part that needs memory

For IN with a subquery, ClickHouse builds a set of the returned values and tests the outer rows for membership. A small final result does not imply a small set. In ClickHouse’s release example, a query counting 2024 sales at previously sold addresses builds its set from 29.3 million earlier sales.

Before changing settings, check that the subquery selects only the intended membership keys and applies all filters allowed by the business question. Narrowing a date range or replacing an address tuple with an identifier requires a correctness decision: it can change which records match. Treat a rewrite as a separate experiment and compare its results with the original.

Separate the spill trigger from the limit

The key setting is max_bytes_before_external_set. In the 26.10 release example, a value of '128Mi' starts spilling while max_memory_usage = '512Mi' retains the query’s overall memory allowance. These are example settings for a controlled test, not recommended production defaults.

The spill threshold is not a memory ceiling. The release example reaches 203.11 MiB at peak despite its 128 MiB threshold, because the query also needs memory to read and process data. Leave headroom below the overall limit and measure the complete query.

The current documentation also describes max_bytes_ratio_before_external_set: if both thresholds apply, the smaller is used. Its ratio is calculated from available server or user memory at execution start, not from max_memory_usage. Check which settings exist on your deployed build; current documentation can describe behaviour beyond the 26.10 release example.

What the published example establishes

ClickHouse’s release post, dated 6 November 2026, reports a peak of 1.96 GiB with spilling disabled and a 4 GiB query limit. Lowering the limit to 512 MiB without spilling produces MEMORY_LIMIT_EXCEEDED during CreatingSetsTransform. With a 128 MiB spill threshold and the same 512 MiB limit, the query completes at 203.11 MiB peak.

Both successful runs return 558,458 sales. The example counts sales at matching addresses, not distinct properties or permanent property identifiers. Its profile events report one spilled set, four written parts and 470,342,352 compressed bytes.

These are vendor-reported observations on one example, not MR Solutions measurements or a capacity forecast. The post’s spilling run is also quicker than its in-memory run, but that comparison does not establish a general speed advantage. Measure latency and storage demand on your own workload.

Test the budget under realistic demand

Agree acceptance criteria before testing: correct results, a memory ceiling, an acceptable response time and enough temporary-disk capacity for the expected simultaneous workload. Use a staging environment with representative data and storage.

  • Record the exact ClickHouse version, query, effective settings, data snapshot, storage type and thread count.
  • Establish a successful baseline with adequate memory. Compare result contents, or counts where appropriate, before accepting a change.
  • Keep other settings fixed while testing several spill thresholds below the query memory limit. Record failures as well as successful runs.
  • Measure elapsed time, peak memory, temporary-disk occupancy and storage I/O. Capture SetsSpilledToDisk, ExternalSetWritePart and ExternalSetCompressedBytes where available; compressed bytes written do not by themselves establish peak disk occupancy.
  • Repeat runs and then test representative concurrency, including other activity sharing the storage. Compare local and network-backed storage separately if both are deployment options.

Choose the trade-off and keep a recovery path

Use spilling when the representative tests meet both memory and service targets with adequate storage headroom. Prefer a query change when it removes unnecessary work while preserving the intended result. Consider more memory when the tested storage cost or latency of spilling is unacceptable, within a budget for the wider workload.

Review temporary-data controls, including max_temporary_data_on_disk_size_for_query and max_temporary_data_on_disk_size_for_user, with the platform owner. Confirm their behaviour on the deployed version and test exhaustion explicitly before relying on them.

Roll out to a limited workload first, watch failures and disk pressure, and retain the previous query settings for recovery. If concurrency breaks the agreed limits, pause expansion and revisit scheduling, query design or capacity. A successful isolated query is only the first acceptance test.

Frequently asked questions

Which setting enables IN subquery disk spilling?

ClickHouse 26.10’s release example enables it with a non-zero max_bytes_before_external_set. Check support and effective settings on your deployed build. Current documentation also describes a ratio threshold, so changing the absolute threshold alone may not fully determine spilling behaviour.

Does a 128 MiB threshold cap query memory at 128 MiB?

No. It triggers spilling and actual memory can exceed it. ClickHouse’s example reaches 203.11 MiB with a 128 MiB spill threshold. Keep the trigger below max_memory_usage and measure the whole query to establish enough headroom.

Will disk spilling make the report faster?

Do not assume it will. The release post reports a faster spilling run for its example, but this does not predict performance on other workloads. Test repeated runs on representative storage and at expected concurrency before setting a response-time target.

Is max_bytes_in_set the same as the spill threshold?

No. The documentation describes max_bytes_in_set as a size restriction on the set created by an IN subquery. max_bytes_before_external_set is a trigger for spilling. Review existing set restrictions as well as the query memory budget when investigating failures.

← Back to the article library

Start with a conversation

Put your ideas into practice.

Tell us about your business and what you want to improve. We’ll help you find a sensible place to start.

Discuss Your Project