A simple guide to ClickHouse query optimization: part 1
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.
Related posts
- 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.
- 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.
- 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.
- Introducing the ClickHouse Query Cache
ClickHouse mediumAdept
ClickHouse v23.1 adds an experimental query cache that stores results of expensive SELECT queries for a configurable TTL (60 s by default), cutting a 9-second aggregation over 3.1 billion GitHub events to 0.04 s on repeat runs. A walkthrough shows a common pitfall: results larger than the 1 MiB default max_entry_size are silently skipped, which trace logs and system.query_cache reveal. Settings control read/write-only use, minimum query duration or run counts before caching, caching of non-deterministic functions, and sharing entries between users. The cache is deliberately transactionally inconsistent: inserts and merges don't invalidate entries, which avoids MySQL-style scalability problems. Entries are keyed by the query AST rather than its text.
- How we made ClickStack 5x faster for ClickHouse observability
ClickHouse hardMaster
ClickStack's OpenTelemetry logs schema, inherited from the ClickHouse exporter for the OTel Collector, was reworked through a benchmark-driven process. The changes combined time-bucketed primary key redesign, text indexes, query rewrites, materialized views and newer ClickHouse features, cutting representative observability query latency by more than 5×. Every schema iteration was validated with the ClickCannon tool and ephemeral ClickHouse Cloud instances at 640 MiB/s ingest on three 24-core replicas. Each change was judged against ingestion throughput, CPU, memory, part counts and the full query workload, so a gain on one query pattern could not hide regressions elsewhere.