Skip to content

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

ClickHouse 15 min read How-to mediumAdept

Super charging your ClickHouse queries

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.

Related posts

  • ClickHouse projections now behave like true secondary indexes

    ClickHouse mediumAdept

    ClickHouse lightweight projections, which store only a sorting key plus a _part_offset pointer into the base table, now act as true secondary indexes with granule-level pruning instead of only pruning whole parts. Several such projections can combine with each other and with the base table's primary index to narrow the granules a query reads. On the UK price paid dataset, a query filtering on date and town dropped from a 30.7M-row full scan in 0.077 s to about 16k rows in 0.010 s, and EXPLAIN shows both projection indexes pruning granules in turn. Two new settings gate when projection indexes are used, and 26.1 adds a compact `PROJECTION ... INDEX col TYPE basic` syntax.

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

  • ClickHouse gets lazier (and faster): Introducing lazy materialization

    ClickHouse mediumAdept

    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.

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

The quest for software engineering knowledge.

Look:
Layout: