Choosing the Right Join Algorithm
ClickHouse offers six join algorithms (direct, hash, parallel hash, grace hash, full sorting merge, partial merge), and the right choice depends on speed, memory and supported join types. Benchmarks on 100M- and 1B-row IMDB tables show direct and parallel hash joins are fastest but memory-hungry. Full sorting merge becomes competitive or faster with far less memory when the physical row order matches the join key, and partial merge is the slowest but leanest. Grace hash trades memory for speed via its bucket count. The post provides decision trees and recommends benchmarking with realistic data, since data volume, types and key distribution can flip the rankings.
Related posts
- ClickHouse Joins Under the Hood - Hash Join, Parallel Hash Join, Grace Hash Join
ClickHouse mediumAdept
ClickHouse implements three in-memory hash-table-based join algorithms within its parallel query pipeline. The default hash join builds a single hash table from the right-hand table in one thread, so with a 100M-row table on the right it ran 5x slower and used 8.95 GiB versus 716 MiB with the small table on the right. Parallel hash join splits right-side rows into max_threads buckets and fills that many hash tables concurrently, roughly halving runtime at more than double the memory. Grace hash join spills buckets to disk to stay within memory limits without requiring sorting, and the post uses EXPLAIN pipeline graphs and query_log statistics to show each algorithm's behavior.
- ClickHouse Joins Under the Hood - Full Sorting Merge Join, Partial Merge Join - MergingSortedTransform
ClickHouse hardMaster
ClickHouse offers two external-sort-based join algorithms that are not memory bound. The full sorting merge join is a classical sort-merge join built into the query pipeline. It can pre-filter each table by the other's join keys and skip sorting entirely when a table's physical row order matches the join key. In those cases it can rival hash joins while using far less memory. The partial merge join minimizes memory for large joins by externally sorting the right table with on-disk index structures and sorting the left table block by block. Query pipeline graphs, trace logs, query_log metrics and settings such as max_bytes_before_external_sort and max_rows_in_set_to_optimize_join show how spilling and the optimizations affect runtime and memory.
- ClickHouse Joins Under the Hood - Direct Join
ClickHouse mediumAdept
ClickHouse's direct join is its fastest join algorithm. It applies when the right-hand table sits on storage that supports low-latency key-value lookups: the Join engine, EmbeddedRocksDB or a Dictionary. The left table is streamed in parallel and each row is joined by looking it up directly in the in-memory structure, so there is no hash-table build phase (a FilledJoin step instead of FillRightFirst). Only LEFT ANY JOIN is supported, because dictionaries deduplicate on the key. A flat dictionary layout gives O(1) array-offset lookups at higher memory cost, and is 2-5x faster than a hashed layout. The worked examples use dictGet, EXPLAIN PLAN and timing comparisons against hash and parallel hash joins on a 100M-row table.
- ClickHouse Release 22.12
ClickHouse mediumAdept
ClickHouse 22.12 adds a grace hash join algorithm that partitions both join sides into hash buckets spilled to disk, dynamically increasing the bucket count when the in-memory hash table exceeds max_bytes_in_join, so large joins run with bounded memory. On a self-join of the hits dataset it beat partial_merge on speed while using more memory, and stayed slower than hash and parallel_hash. Tuning grace_hash_join_initial_buckets avoids re-bucketing. The release also adds regex-based password complexity rules, BSON input for mongodump files, GROUP BY ALL, and a Prometheus metrics endpoint for ClickHouse Keeper.
- How ClickHouse became fast at joins
ClickHouse hardMaster
ClickHouse became 26× faster on the join-heavy TPC-H SF100 workload between 22.4 and 26.4. In the first year (4.4×) parallel hash join became the default, build and probe phases were optimized and two-table reordering was added. In the second year (another 6× with default settings) four changes landed: decorrelation of correlated subqueries, lazy column replication, runtime filters and statistics-based join reordering. Lazy replication represents repeated join outputs as dictionary indexes instead of copying wide strings (1.9× faster in a self-join benchmark). Runtime bloom/min-max filters built from build-side keys are applied as PREWHERE on the probe side (2.1× faster, peak memory cut from 1.24 GiB to 185 MiB).