Repository navigation
Conversation
A non-nullable `keys256` key set is packed row by row (`packFixed`): the 32-byte slot is zero-filled, each column is written with a 1/2/4/8/16-byte store in clause order, and then the hash and the hash table read the slot back as 8-byte words. A word holding a key narrower than 8 bytes spans several in-flight stores and cannot be store-forwarded, so each row stalls. Sets of up to 16 bytes are already batch-packed per block (`usePreparedKeys`), and nullable sets have been since ClickHouse#126098, so this was the last per-row fixed-key path outside LowCardinality. `useOrderedPreparedKeys` now also accepts non-nullable `keys256` sets whose widths are all in {1, 2, 4, 8, 16} and that contain a key narrower than 8 bytes. They are packed once per block with `packFixedBatchOrdered` in clause order, which is byte for byte the per-row `packFixed` layout, so the hashes, the two-level buckets, the unpacked output columns (`packedKeysOrder` stays empty) and the `Join` engine read-back are unchanged. Sets of only 8- and 16-byte keys keep the per-row path: their words forward, and a longest-first batch of `UInt64` x3 measured 1.024x with 10M unique keys. The short sub-range fallback packs `keys256` in clause order too. `SubRangeStateOf` now gives every non-LowCardinality fixed-key state a sub-range variant (no fixed key is wider than `UInt256`), so aggregation and DISTINCT in order pack only their run, as ClickHouse#120259 does for keys128; without it the new in-order perf queries take 9 s instead of 0.03 s. Measured: release without ThinLTO, Xeon 6975P-C, pinned to the physical cores of one idle NUMA node, 16 interleaved pairs, geometric-mean ratio fix/master with 95% CI: UInt32, UInt64, UInt64, 91K groups (numbers_mt 1e8) 1 thread 0.515 [0.472, 0.563] 8 threads 0.426 [0.406, 0.447] 32 threads 0.651 [0.636, 0.665] UInt64 x3, same rows and groups (path unchanged) 1 thread 0.994 [0.987, 1.001] 8 threads 1.019 [0.972, 1.069] group_by_fixed_keys.xml `a, e, f` (1e7 groups, two-level) 1 thread 0.645 [0.627, 0.663] 8 threads 0.991 [0.944, 1.042] 32 threads 0.980 [0.934, 1.028] nine UInt16 keys, 804K groups 1: 0.395 8: 0.555 hits (10M rows), 8 threads: UserID, CounterID, RegionID, EventDate 0.707; CounterID, RegionID, EventDate, SearchEngineID, OS, UserAgent, TraficSourceID 0.657; nine Int16/Date keys 0.743; UserID, CounterID (keys128, unchanged) 1.020 DISTINCT a, e, f 1: 0.397 8: 0.480; IN (a, e, f), 8: 0.478 JOIN USING (UInt32, UInt64, UInt64), 20M x 1M: hash 0.499 / 0.588, parallel_hash 0.542 / 0.667 (8 / 32 threads); fan-out 100 at the default output block limits 0.920; with max_joined_block_size_rows = 1024 0.977 in order, 1 thread: runs of 65536 GROUP BY 0.833, DISTINCT 0.361; unique sorting key GROUP BY 1.026 (+8 ns per run), DISTINCT 0.857 group_by_fixed_keys.xml e,f / e,f,g / e,i / e,f,i (unchanged): 0.998-1.031, every CI includes 1 PMU, per input row, 1 thread, master -> fix: `ld_blocks.store_forward` 9.20 -> 0.002, cycles 173.5 -> 93.6 (UInt32, UInt64, UInt64); UInt64 x3 0.022 -> 0.022, cycles 85.3 -> 85.0. Unchanged-path controls with a timing shift have identical cycles and instructions; one in-order DISTINCT control (+6.7%, 13 ms) runs a hot function that is byte-identical in both binaries and shows a DSB-miss signature, i.e. code placement. `.text` grows by 168 KiB. Based on the patch by @ snelheidai in ClickHouse#123658 (comment) (simplified `SubRangeStateOf`, oracle-based unit tests added). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the store-forwarding rationale in useOrderedPreparedKeys to one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
cc @nickitat @nihalzp, could you review this and add the |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20261108-234100 |
|
Workflow [PR], commit [6df7a4f] Summary: ❌
AI ReviewSummaryReviewed #130251 at Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. |
CI finish ledger - 6df7a4fEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The other CIDB Session id: cron:our-pr-ci-monitor:20261109-073003 |
|
Superseded by #131393: its third commit packs the same |
Related: #123658
Related: #126098
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
GROUP BY,DISTINCT,INandJOINover several fixed-width keys that together take 17 to 32 bytes, one of them narrower than 8 bytes (for exampleUInt32, UInt64, UInt64), are faster: the keys of a block are packed at once instead of row by row. Such queries take up to 60% less time; with tens of millions of groups the gain disappears at 8 or more threads.Description
A non-nullable
keys256key set is packed row by row: one narrow store per key into a 32-byte slot, which the hash and the hash table then read as 8-byte words. A word holding a key narrower than 8 bytes spans several in-flight stores and cannot be store-forwarded, so every row stalls. Smaller key sets are batch-packed per block, and nullable ones are since #126098.useOrderedPreparedKeysnow also accepts non-nullablekeys256sets with a key narrower than 8 bytes, packed once per block withpackFixedBatchOrderedin clause order. Those are the per-row bytes, so hashes, two-level buckets, output columns andJointables are unchanged. Sets of only 8- and 16-byte keys stay per row (their words forward). These states also get a sub-range variant, so aggregation andDISTINCTin order pack only their run (#120259); without it the new in-order perf queries take 9 s instead of 0.03 s.Measured (release, Xeon 6975P-C, one NUMA node, 16 interleaved pairs, ratio to master):
UInt32, UInt64, UInt64with 91K groups 0.52x / 0.43x / 0.65x at 1 / 8 / 32 threads,ld_blocks.store_forwardper row 9.2 -> 0.002; nineUInt16keys 0.40x / 0.56x; three hits key sets 0.66x-0.74x;DISTINCT0.40x / 0.48x;IN0.48x;JOIN USING0.50x-0.67x, with fan-out 100 0.92x;a, e, fofgroup_by_fixed_keys.xml(10M groups) 0.65x at 1 thread, 0.98x-0.99x at 8-32; aggregation in order over one-row runs 1.03x; controls on unchanged paths 0.99x-1.03x with equal cycles. This PR's own performance comparison is the arbiter for x86 and ARM.Based on the patch by @ snelheidai in #123658 (comment).
Workflow [PR]
Sync PR [sync-upstream/pr/130251]