Skip to content

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

ClickHouse 6 min read Deep dive hardMaster

Parallelizing ClickHouse aggregation merge for fixed hash map

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.

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 Release 25.11

    ClickHouse mediumAdept

    ClickHouse 25.11 ships 24 features, 27 performance optimizations and 97 bug fixes. Headline additions are automatic TLS certificate provisioning via ACME (Let's Encrypt, ZeroSSL), with certificates shared across the cluster through ClickHouse Keeper, and parallel merging for GROUP BY on 8/16-bit keys. That merge works because FixedHashMap is a directly indexed array, so merge threads can each take disjoint, interleaved key positions without locks, giving about a 40% speedup on a uniqExact benchmark. Lightweight _part_offset projections now prune at granule level and act as secondary indexes, cutting a two-filter query by 90%. DISTINCT queries can also read from smaller pre-aggregated projections when those contain the distinct key.

  • Hash tables in ClickHouse and C++ Zero-cost Abstractions

    ClickHouse hardMaster

    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.

  • 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 we scaled raw GROUP BY to 100 B+ rows in under a second

    ClickHouse mediumAdept

    ClickHouse Cloud's parallel replicas feature spreads a single GROUP BY query across every core of every node in a service, with no pre-aggregation or data reshuffling. In the benchmark, 100 billion rows were aggregated in 414 ms at about 1.45 TB/s, compared with 16.6 s on a single 89-core node. The speedup rests on ClickHouse's execution model. Each pipeline stream scans a non-overlapping set of part ranges and builds mergeable partial aggregation states in one of 30+ specialized hash table variants, so any core or node can process any subset of rows and the partial results merge correctly. The post also covers memory behaviour by aggregate function, the reproducible benchmark setup, and how runtime scales vertically (more cores per node) and horizontally (more nodes).

The quest for software engineering knowledge.

Look:
Layout: