ClickHouse projections now behave like true secondary indexes
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.
Related posts
- 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.
- 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.
- How ClickHouse makes Top-N queries faster with granule-level data skipping
ClickHouse mediumAdept
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.
- ClickHouse Release 25.6
ClickHouse mediumAdept
ClickHouse 25.6 (25 features, 32 performance optimizations, 105 bug fixes) can take a single consistent storage snapshot across all subqueries and CTEs in a SELECT, controlled by enable_shared_storage_snapshot_in_query. Snapshots rely on immutable data parts that a query references until it finishes. Multiple projections can now be combined to prune parts for one query, building on index-like projections that store only the sort key plus _part_offset to cut storage duplication, though pruning is part-level only for now. Other additions include reading Parquet's logical JSON type back as ClickHouse JSON, experimental Time/Time64 types, and new system.codecs and iceberg_history system tables.
- ClickHouse Newsletter May 2022: Explain Statement – Query Optimization
ClickHouse mediumAdept
ClickHouse's May 2022 newsletter centers on using EXPLAIN for query optimization. EXPLAIN SYNTAX reveals rewrites such as wildcard expansion and predicate pushdown into subqueries. EXPLAIN PLAN with indexes=1 shows granules read: on a 1-billion-row MergeTree table, sorting by the filtered column lets the primary key prune reads to 1 of 122,071 granules, cutting query time from 0.688s to 0.007s. The newsletter also covers the 22.4 release, which adds experimental transactions, Keeper load balancing, a RocksDB metadata cache that cut startup from 75 minutes to 20 seconds at 700k parts, and WITH FILL INTERPOLATE gap filling, plus upcoming events and revamped docs.