Skip to content

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

ClickHouse 18 min read Deep dive mediumAdept

ClickHouse gets lazier (and faster): Introducing lazy materialization

ClickHouse 25.4 adds lazy materialization, which defers reading column data until the query plan actually needs it. For Top N queries with ORDER BY and LIMIT, large SELECT columns are read only for the few rows that survive sorting and limiting. Layered on top of columnar storage, sparse primary indexes and PREWHERE, it cut a query over a 150M-row Amazon reviews table from 219 seconds to 139 ms (1,576×) with no SQL changes. The walkthrough also measures each layer's contribution on cold cache, showing that disk throughput, not CPU, dominates the cost of reading large string columns.

Assumes: columnar storage basicsSQL ORDER BY and LIMITdatabase indexes

Related posts

  • ClickHouse Release 25.4

    ClickHouse mediumAdept

    ClickHouse 25.4 adds lazy materialization, which defers reading column data until it is needed. On a Top-N query over Amazon reviews it cut runtime from 219s to 0.139s, with 40× less I/O and 300× less memory. Other additions are MergeTree tables on read-only object storage that refresh parts, allowing one writer and unlimited readers for data sharing, and a CPU workload scheduler. The scheduler uses CPU resources and hierarchical workloads with max_concurrent_threads and weights. The release also brings experimental correlated subqueries in EXISTS, Apache Iceberg time travel via snapshot timestamps, a table-level default compression codec, persistent databases in clickhouse-local, and SSH access to the public playground.

  • 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.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.

  • Top 10 best practices tips for ClickHouse

    ClickHouse mediumAdept

    Ten practices for getting performance and storage efficiency out of ClickHouse, each measured on a 150M-row Amazon reviews dataset. Aligning ORDER BY with query filters (low-cardinality columns first) made one query 3x faster and cut scanned data 347x. Right-sized types, no Nullable and LowCardinality(String) doubled query speed and shrank storage. Partitioning is best avoided except for TTL expiry or FINAL on merge engines; monthly partitions slowed one aggregation 46x. The post also covers lightweight versus heavyweight skipping indexes, JSON type hints (38% less storage, 26% faster), and preferring Parquet/ORC for bulk ingestion.

  • Using Materialized Views in ClickHouse

    ClickHouse mediumAdept

    ClickHouse materialized views act as insert-time triggers that write the result of a SELECT into a target table, letting expensive aggregations be precomputed. On a 1B-row Wikistat dataset, a SummingMergeTree-backed view cuts a top-projects query from 15 seconds to 3 ms, and AggregatingMergeTree with *State()/*Merge() combinators handles min/max/avg the same way. The same mechanism supports compacting history (with source-table TTL), validating and filtering rows via a Null-engine source table, routing invalid data to separate tables, and transforming columns. Practical guidance: keep views per table to around ten or fewer, still GROUP BY at query time since merges are asynchronous, avoid FINAL on large tables, and only aggregate for storage when it reduces rows by roughly 10x.

The quest for software engineering knowledge.

Look:
Layout: