Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 9 min read Deep dive mediumAdept

How ClickHouse makes Top-N queries faster with granule-level data skipping

ClickHouse can now use minmax data-skipping indexes to prune whole granules for ORDER BY … LIMIT N queries before reading any rows. Without predicates, it preselects only the granules with the smallest min (or largest max) values. With predicates, it keeps a dynamic N-th-value threshold during streaming execution and skips granules whose min/max cannot improve the result. On the 100M-row hits dataset, enabling use_skip_indexes_for_top_k and use_top_k_dynamic_filtering made Top-N queries 5–10× faster and cut data read by 10–100×, and a 50B-row production table answered in under 0.2 s. The pruning composes with streaming Top-N, read-in-order and lazy reading, and it pays off most on large tables, cold caches and object storage.

Assumes: ClickHouse granules and MergeTreedata skipping indexesORDER BY LIMIT query execution

Related posts

  • Index-based pruning in ClickHouse

    ClickHouse mediumAdept

    ClickHouse offers three index-based ways to skip data: the sparse primary index (one entry per 8,192-row granule), lightweight projections that store only a sort key plus a _part_offset pointer into the base table, and skip indexes such as minmax that keep per-granule min/max values. On a 243-million-row UK property sales table, a filter on the leading primary-key column read 1.69 million rows in 15 ms. Filtering only on the second key column fell back to generic exclusion search, scanned 138 million rows and ran 50x slower. Lightweight projections handle filters on non-key columns without duplicating full rows, while minmax indexes cost even less storage but only prune well when the column correlates with the primary key.

  • ClickHouse Release 25.12

    ClickHouse hardMaster

    ClickHouse 25.12 ships 26 features, 31 performance optimizations and 129 bug fixes, led by three query-engine speedups. Top-N queries (ORDER BY … LIMIT N) now compare the running threshold against granule-level min/max skip-index metadata to skip granules before reading, cutting data read by one to two orders of magnitude and speeding queries 5–10×. Lazy materialization moved from row-by-row column fetches to a batched join-style step (JoinLazyColumnsStep), making a LIMIT 100,000 query about 75× faster (38.7s to 0.51s), and its default limit was raised from 10 to 10,000. An experimental DPsize dynamic-programming join reordering algorithm for INNER JOINs explores more join orders than the greedy approach and produces a better plan on a TPC-H eight-table query.

  • Streaming secondary indices: incremental, demand-driven index evaluation

    ClickHouse mediumAdept

    ClickHouse 25.9 introduces streaming secondary indices, which check skip-index entries (minmax, set, bloom filter, vector, text) incrementally as each table granule is about to be read, instead of scanning the whole index before query execution. Interleaving index checks with data reads removes the startup delay and lets queries with LIMIT stop both index scanning and granule reads as soon as the result is complete. On a one-billion-row table with a 2.2 GiB bloom filter index, a LIMIT 1 needle lookup dropped from ~10.2s to ~2.5s and peak memory halved. The feature is controlled by the use_skip_indexes_on_data_read setting.

  • Super charging your ClickHouse queries

    ClickHouse mediumAdept

    ClickHouse query speed comes mainly from reading less data: a sparse primary index stores one entry per 8192-row granule and lets binary search skip blocks that can't match the WHERE clause. On the 27.6M-row UK property dataset, re-sorting a copy of the table by (town, price) cuts a full scan to 81.92 thousand rows, and optimize_read_in_order removes the in-memory sort and stops early under LIMIT. Projections do the same job automatically, as hidden tables kept in sync with the main table that can use a different sort order or pre-computed aggregates (backed by AggregatingMergeTree). ClickHouse picks the cheapest projection at query time. Raising query-engine parallelism is the third tuning knob.

  • ClickHouse Release 25.9

    ClickHouse mediumAdept

    ClickHouse 25.9 adds automatic global join reordering, which uses a greedy algorithm plus row-count estimates or manually created column statistics to pick build/probe order across many tables. On a six-table TPC-H query at scale factor 100 it cut runtime from over an hour to 2.7 seconds (~1,450×) with ~25× less memory. Secondary indices are now checked granule by granule while data is read, so LIMIT queries stop early; one LIMIT 1 query ran over 4× faster. Other changes include an experimental streaming-friendly text index replacing the FST design, Iceberg write and metadata improvements, arrayExcept, argument-less boolean settings, and S3 storage class selection.

The quest for software engineering knowledge.

Look:
Layout: