ClickHouse Joins Under the Hood - Direct Join
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.
Related posts
- Using Dictionaries to Accelerate Queries
ClickHouse mediumAdept
ClickHouse dictionaries, in-memory key-value structures, can replace JOINs against small lookup tables. Using the NOAA weather dataset, a normalized INNER JOIN against a stations table forces a full scan of 1.08 billion rows because ClickHouse runs joins before WHERE filtering. Swapping in dictGet lookups alongside a primary-key-friendly subquery brings that query back to millisecond latency. The post walks through creating a dictionary, choosing a layout (flat, hashed, hashed_array, complex_key variants, sparse), setting LIFETIME and invalidate_query for refreshes, and loading data, recommending hashed_array or complex_key_hashed_array for most cases.
- 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.
- Join me if you can: ClickHouse vs. Databricks vs. Snowflake - Part 2
ClickHouse mediumAdept
Replacing fact-to-dimension joins with in-memory dictionaries in ClickHouse Cloud sped up a 17-query join benchmark over 1.4 billion rows by 1.9–3.5× in total runtime, up to 6.6× on individual queries, and cut cost by up to 60%. The Locations table becomes a complex_key_hashed dictionary for String-key lookups. The Products table becomes a range_hashed dictionary that resolves time-versioned rows with a hash map plus binary search over date ranges, provided the source has no overlapping ranges per key. Queries change only by swapping JOINs for dictGet calls, and the gains grew as the cluster scaled from 2 to 8 nodes.
- 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).