Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 22 min read Deep dive mediumAdept

ClickHouse Joins Under the Hood - Hash Join, Parallel Hash Join, Grace Hash Join

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.

Assumes: SQL joinshash tablesClickHouse basics

Related posts

  • Choosing the Right Join Algorithm

    ClickHouse mediumAdept

    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.

  • 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).

  • ClickHouse Release 25.1

    ClickHouse mediumAdept

    ClickHouse 25.1 speeds up the default parallel hash join by building N two-level hash tables whose 256 buckets are filled by threads in a non-overlapping way, then merging them in constant time into one shared table. The probe phase can then read that table concurrently without splitting input blocks, and it uses a single hash function where the old design used two. Benchmarks on a 32-vCPU m6i.8xlarge show roughly 37% faster joins on a synthetic query and 32% faster on a TPC-H SF100 lineitem/orders join. The release also adds the add_minmax_index_for_numeric_columns setting, which creates MinMax skip indexes on every numeric column, and it improves Merge tables and adds auto-increment support.

  • 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.

  • ClickHouse Release 24.11

    ClickHouse mediumAdept

    ClickHouse 24.11 makes parallel hash join the default join algorithm, building several hash tables concurrently for speed at the cost of more memory, and adds zero-copy block scattering between threads. ORDER BY WITH FILL gains a STALENESS modifier that stops generating filler rows once the gap from the previous real row exceeds a threshold. The HTTP interface now surfaces errors that happen mid-stream as a non-zero client exit code, and a new mark_cache_prewarm_ratio setting plus a SYSTEM PREWARM MARK CACHE command let the server load index marks into memory eagerly. An experimental BFloat16 type speeds up vector search: a full-scan nearest-neighbor query over 28M 384-dimensional vectors took 0.061s versus 0.146s with Float32.

The quest for software engineering knowledge.

Look:
Layout: