Hash tables in ClickHouse and C++ Zero-cost Abstractions
ClickHouse implements GROUP BY and JOIN with a family of open-addressing hash tables generated from a common zero-cost C++ framework, specialized by key type and workload. Key design choices are CRC-32C-based hashing instead of identity, cryptographic or FNV1a hashes, linear probing with power-of-two resizing and a 0.5 load factor, and a separately stored zero/null cell instead of user-supplied empty or tombstone keys. On real web-analytics data, ClickHouse's HashMap aggregated about 20M unique keys in 7.4s, versus 9.0s for Abseil, 10.1s for Google DenseMap and 44.8s for std::unordered_map, with cache misses explaining the gap. Benchmarks should use real data distributions and real scenarios, not random numbers.
Related posts
- Parallelizing ClickHouse aggregation merge for fixed hash map
ClickHouse hardMaster
ClickHouse 25.11 parallelizes the merge phase for GROUP BY on 8- and 16-bit keys, which use an array-backed FixedHashMap. Before this, these aggregations could not use the bucket-wise parallel merge available to two-level hash tables, so a UInt16 key query ran in 62.8s versus 8.5s for the same query cast to UInt64. The fix has merge threads work in place on disjoint key subsets instead of converting to a two-level table. Along the way: contiguous range segmentation balanced poorly, a shared non-thread-safe Arena caused memory corruption and needed one Arena per thread, and a fixed 3ms regression on trivial aggregates was solved by computing the min/max populated index before the parallel merge.
- 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 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.
- 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.