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 read read_rows, read_bytes, query_duration_ms, memory_usage from system.query_log. EXPLAIN indexes = 1 shows pruning; EXPLAIN PIPELINE shows parallelism; clickhouse-benchmark repeats 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 BY a prefix of the sorting key stops after the first rows instead of sorting everything.
  • Approximate aggregates: uniq, uniqCombined, quantileTDigest use far less memory than uniqExact, 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 *, FINAL on 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):

QueryRows readMemoryTime
ORDER BY country, event_type, user_id LIMIT 5, optimize_read_in_order=140,9602.6 MiB5 ms
same, optimize_read_in_order=05,000,0000.6 MiB9 ms
uniqExact(user_id)5,000,00072.7 MiB38 ms
uniq(user_id)5,000,00013.1 MiB25 ms
count() joined to 1,000-row subquery on the right1,310,7211.8 MiB7 ms
count() with the 5M-row table on the right (no automatic swap)5,000,001212 MiB349 ms
WHERE session LIKE 'D3F7%' with PREWHERE off5,000,000 rows, 128.7 MiB27 ms
same with PREWHERE1,695,744 rows, 29.3 MiB13 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_usage in the test server was unlimited (0); set limits in production profiles to protect the server.
  • Wide GROUP BY keys with many distinct values can exhaust memory; use GROUP BY ... WITH spill 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

  1. Which settings and tables do you use to measure a query’s rows and bytes read?
  2. When does optimize_read_in_order apply?
  3. Why is the right side of a hash join the one that costs memory?

References

  • ClickHouse documentation, Query optimization guide; EXPLAIN

ch-sorting-key-sparse-index · ch-skipping-indexes · ch-dictionaries · query-planning · pg-explain-analyze