Overview
ClickHouse performance comes from reading less data and using all cores. Optimisation is mostly: let the primary key prune, read few columns, avoid large joins and exact distinct counts, and measure with system.query_log. General theory: query-planning.
Core concepts
- Measure first: add
SETTINGS log_comment = 'name', run,SYSTEM FLUSH LOGS, then readread_rows,read_bytes,query_duration_ms,memory_usagefromsystem.query_log.EXPLAIN indexes = 1shows pruning;EXPLAIN PIPELINEshows parallelism;clickhouse-benchmarkrepeats queries. - PREWHERE: reads only the filter columns first, then the remaining columns for surviving granules. On by default (
optimize_move_to_prewhere = 1); you can force it. - Read in order (
optimize_read_in_order):ORDER BYa prefix of the sorting key stops after the first rows instead of sorting everything. - Approximate aggregates:
uniq,uniqCombined,quantileTDigestuse far less memory thanuniqExact,quantileExact. - Joins: right side goes to memory; put the smaller table on the right; choose
join_algorithm(parallel_hash,grace_hash,full_sorting_merge) when memory is tight (ch-dictionaries). - Avoid:
SELECT *,FINALon huge tables without key filters, many tiny queries returning large results, joins on huge tables. - Resource limits:
max_memory_usage,max_execution_time,max_threads, spill settings (max_bytes_before_external_group_by) via profiles.
Worked example
Measured on events_a (5M rows) and events_s (5M rows):
| Query | Rows read | Memory | Time |
|---|---|---|---|
ORDER BY country, event_type, user_id LIMIT 5, optimize_read_in_order=1 | 40,960 | 2.6 MiB | 5 ms |
same, optimize_read_in_order=0 | 5,000,000 | 0.6 MiB | 9 ms |
uniqExact(user_id) | 5,000,000 | 72.7 MiB | 38 ms |
uniq(user_id) | 5,000,000 | 13.1 MiB | 25 ms |
count() joined to 1,000-row subquery on the right | 1,310,721 | 1.8 MiB | 7 ms |
count() with the 5M-row table on the right (no automatic swap) | 5,000,001 | 212 MiB | 349 ms |
WHERE session LIKE 'D3F7%' with PREWHERE off | 5,000,000 rows, 128.7 MiB | 27 ms | |
| same with PREWHERE | 1,695,744 rows, 29.3 MiB | 13 ms |
Takeaways: reading in key order can turn a sort into a short scan; approximate distinct counts use about 5x less memory; join order changes memory by two orders of magnitude; PREWHERE cut bytes read by about 4x for a selective string filter.
Pitfalls and production notes
- Compare plans and timings on realistic data volumes and cold/warm cache; microsecond differences on tiny tables mean nothing.
- A projection or index can silently change later benchmarks on the same table (ch-projections).
- Default
max_memory_usagein the test server was unlimited (0); set limits in production profiles to protect the server. - Wide
GROUP BYkeys with many distinct values can exhaust memory; useGROUP BY ... WITHspill settings or pre-aggregation (ch-materialized-views). - Keep statistics-style tuning out of habit: ClickHouse has no cost-based plan cache to tune, so structure (key, types) matters most (ch-sorting-key-sparse-index).
Practice questions
- Which settings and tables do you use to measure a query’s rows and bytes read?
- When does
optimize_read_in_orderapply? - Why is the right side of a hash join the one that costs memory?
References
- ClickHouse documentation, Query optimization guide; EXPLAIN
Related
ch-sorting-key-sparse-index · ch-skipping-indexes · ch-dictionaries · query-planning · pg-explain-analyze