ClickHouse Newsletter May 2022: Explain Statement – Query Optimization
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.
Related posts
- 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 Release 26.4
ClickHouse mediumAdept
ClickHouse 26.4 ships 39 features, 45 performance optimizations and 238 bug fixes, led by SQL-compatibility additions: VALUES as a table expression with column aliases, PostgreSQL-style EXTRACT units, SET TIME ZONE, NATURAL JOIN and compound INTERVAL literals. LIKE '%token%' queries can now scan the text index dictionary instead of the full column, which ran about 3x faster with far less memory on the HackerNews dataset. COUNT DISTINCT (uniqExact) merges all per-thread two-level hash tables in a single pass and stops spawning redundant threads, giving 3-15x speedups on a 288-core machine but nothing on a 12-core laptop. EXPLAIN pretty output is more readable, and a new JSONAllValues function lets a text index cover every leaf value of a JSON column.
- A simple guide to ClickHouse query optimization: part 1
ClickHouse mediumAdept
Slow ClickHouse queries can be diagnosed with system.query_log (duration, rows read, memory, CPU, cache metrics) and the EXPLAIN statement, illustrated on the NYC Taxi dataset in the ClickHouse Playground. A table created with an inferred schema (all Nullable columns, empty ORDER BY) forces every example query into a full scan of 329 million rows taking over a second and roughly 400–550 MiB of memory. Turning off the filesystem cache makes timings reproducible. The first part of the series then moves to basic optimizations and choosing primary keys so ClickHouse can skip data instead of scanning it.
- 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 26.6
ClickHouse mediumAdept
ClickHouse 26.6 ships 56 features, 79 performance optimizations and 366 bug fixes, marking ten years of ClickHouse as open source. Headline additions include session-scoped hypothetical skip indexes evaluated with EXPLAIN WHATIF, which estimate skip ratios before an index is built (92% vs 77% of granules skipped at granularity 1 vs 128 in a London query on ~487M rows). Refreshable materialized views can now cascade via REFRESH DEPENDS ON without separate timers, avoiding drift between stages. Other changes include ALTER ... ADD ENUM VALUES, in-CLI help backed by system.documentation, clickhouse-local listening as a server, about 3x faster analysis of deeply nested queries, and experimental continuous STREAM queries with cursors.