Skip to content

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

ClickHouse 32 min read Deep dive hardMaster

ClickHouse Joins Under the Hood - Full Sorting Merge Join, Partial Merge Join - MergingSortedTransform

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.

Assumes: SQL joinssort-merge join basicsClickHouse query pipeline

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.

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

  • HIFI’s migration from BigQuery to ClickHouse

    ClickHouse mediumAdept

    HIFI moved music royalty analytics from BigQuery to ClickHouse because BigQuery's per-query pricing discouraged exploratory use and slot reservations didn't fit a startup's shifting workloads. Dashboard loads for accounts with 25M+ rows dropped from up to 30 seconds (with timeouts) to a few seconds. The migration surfaced ClickHouse-specific join behaviour: bulk updates work better via a Join engine table and joinGet, and LEFT joins from PostgreSQL to large ClickHouse tables can exhaust memory because the hash join loads the right side into memory, which pre-aggregating the right side with GROUP BY fixes (under 1 second). A community Node.js client also silently converted NULLs to empty strings on insert, which they patched.

  • 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 Cloud: Fast, Updatable Lookups with the Join Table Engine

    ClickHouse mediumAdept

    ClickHouse's Join table engine keeps a precomputed in-memory hash map for a fixed join strictness, type and key. With ANY LEFT strictness, inserts behave as upserts, which suits Type 1 slowly changing dimensions as an updatable alternative to Dictionaries, queried via Direct Join or joinGet. In open source the engine isn't distributed and persists each insert as an uncompacted .bin file, which degrades over time. ClickHouse Cloud transparently backs Join tables with a SharedJoin over a MergeTree (ALL) or ReplacingMergeTree (ANY) table, refreshing memory with a FINAL query on insert and startup, so deduplication, compaction and clustering are handled automatically.

The quest for software engineering knowledge.

Look:
Layout: