Repository navigation
Do not prefetch in a two-level GROUP BY that holds few keys - #125897
Conversation
A two-level hash table allocates all 256 buckets at their initial size up front, so its getBufferSizeInBytes() is a constant however few keys it holds: 2 MiB for string keys with a saved hash, 2.5 MiB for keys256 with a mapped value, about 6.5 MiB for TwoLevelStringHashMap. Since ClickHouse#108655 lowered the aggregation prefetch threshold from 4 x L2 to L2, every host whose L2 is below that constant prefetches every row of such a table, even with 13 keys. The look-ahead then hashes each key a second time for probes that touch a handful of cells (ClickHouse#125845: 2.3x slower on 26.8 than on 26.6). bytesForPrefetch() measures a two-level table that is still at its initial allocation by its keys, as the smallest table that holds them at the maximum load factor 1/2, and every other table by its buffer as before. Both users of the gate go through it: Aggregator::executeImpl and the self-prefetching HashMethodSerialized. StringHashTable gets initial_buffer_bytes so that TwoLevelStringHashTable is covered as well. Measured on a release build, base vs fix (executed PREFETCH instructions via perf sw_prefetch_access.any, and time): - issue query at an emulated 1 MiB L2: 301M -> 1.8M, 0.23 s -> 0.075 s - Nullable(String), 13 keys, 2 MiB L2: 99.9M -> 0.4M, 0.22 s -> 0.18 s - keys256, 13 keys, 2 MiB L2: 58.9M -> 0.4M, 0.15 s -> 0.11 s - unchanged: the ClickHouse#108311 shape, 20K-key two-level tables at 1 MiB L2 (prefetch stays on), 2M-key single-level tables Closes ClickHouse#125845 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (3 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261014-220300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261014-203300 |
|
Workflow [PR], commit [2432bbd] Summary: ✅
AI ReviewSummaryThis PR avoids unnecessary software prefetching for two-level Missing context / blind spots
Final VerdictStatus: ✅ Approve. No changes requested. |
|
cc @nickitat @nihalzp, could you review this? A two-level hash table preallocates all 256 buckets, so its buffer size is a constant above L2 even with 13 keys, and since #108655 lowered the gate to L2 the |
CI finish ledger - aa59bdaEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261016-003007 |
The gate only decides whether to prefetch, so a functional test cannot observe it. The test instead runs every two-level table shape behind `bytesForPrefetch` with `enable_software_prefetch_in_aggregation` on and off and checks that the rows match: the `LowCardinality(Nullable(String))` key from ClickHouse#125845, `Nullable(String)` (`TwoLevelStringHashMap` and its new `initial_buffer_bytes`), `keys256`, serialized keys (the `HashMethodSerialized` gate), and a two-level table that has outgrown its initial allocation (the fallback branch). Related: ClickHouse#126578
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units815 translation units recompiled, 7746 s compile time in total, 814 of them have a recent master baseline. Translation units without a recent master baseline:
|
`05498_group_by_two_level_few_keys_prefetch` failed the debug flaky check with "Test runs too long (> 180s)" in 1 of 50 runs (201.7 s; the other 49 took at most 58 s). Its `query_log` shows that run slow in every statement under heavy MergeTree randomization (`auto_statistics_types`, sparse serialization), so the cost follows the data size. Insert 300000 rows instead of 1000000. The 6 reading streams (`EXPLAIN PIPELINE`) still read about 50000 rows each, more distinct keys of `many` than the 32768 a two-level table fits in its initial 256 x 256 cells at load factor 1/2, so the grown case still grows. Pin `max_block_size = 8192` for the queries: a thread converts to two-level only after its first block, so the gate sees its two-level table only from the second block on, and with fewer rows per thread a large random block size would leave none. Results do not depend on the data size, so the reference only changes its counts. Measured on the CI debug binary of 70ff2d5: 2.1x less CPU locally (2.8x with the failing run's settings); under the flaky-check shape (8 jobs, 50 runs, thread fuzzer) the p50 drops by about 30%, and with the failing run's settings it is at least 2.4x faster (600 s against the 1500 s timeout of every old run). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Head branch was pushed to by a user without write access
|
@fm4v |
The previous run's jobs were cancelled by a self-hosted runner outage and never re-ran, so the head could not finish CI. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 2432bbdEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261105-203010 |
The parallel-INSERT section compared the entries of the newest manifest list (by file mtime) with the APPEND rows of system.iceberg_history. The insert streams commit their snapshots about 1 ms apart, so the current manifest list and its parent can share one coarse-clock mtime; `sort -n` then orders the tie by the path, which carries the random snapshot id, and `tail -1` can return the parent's list (one entry fewer), printing `0 1 1` instead of `1 1 1`. CI query_log of both failures (ClickHouse#125897, ClickHouse#128995) shows the test reading the parent snapshot's list. Find the list the way a reader does: the newest v<N>.metadata.json, the snapshot whose snapshot-id is current-snapshot-id, its manifest-list. Forcing every manifest list to one mtime, the old helper picked a wrong list in 23/40 trials and the new one in 0/40. 05496 and 05229 use the same mtime heuristic but write each snapshot from a separate client call, so they are not changed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
c1c4e0b
Backport #125897 to 26.10: Do not prefetch in a two-level GROUP BY that holds few keys
Backport #125897 to 26.9: Do not prefetch in a two-level GROUP BY that holds few keys
Cherry pick #125897 to 26.8: Do not prefetch in a two-level GROUP BY that holds few keys
Backport #125897 to 26.8: Do not prefetch in a two-level GROUP BY that holds few keys
Closes: #125845
Closes: #126578
Related: #108655
Related: #108311
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A two-level
GROUP BYthat holds few keys no longer enables software prefetch only because its preallocated buckets are larger than the L2 cache. Such queries were up to 3x slower since 26.7, for example 13 keys withgroup_by_two_level_threshold_bytes = 1. Closes #125845.Description
Since #108655 lowered the aggregation prefetch threshold from 4 x L2 to L2, the two-level
GROUP BYfrom #125845 (13 keys) runs 2-3x slower on hosts with a small L2.A two-level table allocates all 256 buckets up front, so
getBufferSizeInBytes()is a constant however few keys it holds: 2 MiB for string keys with a saved hash, 2.5 MiB forkeys256, about 6.5 MiB forNullable(String). Above L2 that turns prefetch on for every row, which costs an extra key hash per row while the probes touch only 13 cells.bytesForPrefetchmeasures a two-level table that is still at its initial allocation by its keys (2 * size * cell, the smallest table that holds them) instead of by its buffer. Grown two-level tables and all single-level tables keep the old measure. Both gates use it:Aggregator::executeImplandHashMethodSerialized.Validation on a release build, base vs fix, counting executed PREFETCH instructions (
perf stat -e sw_prefetch_access.any) and timing. The issue query at an emulated 1 MiB L2: 301M -> 1.8M prefetches, 0.23 s -> 0.075 s (the same as with prefetch disabled). On this 2 MiB-L2 host:Nullable(String)with 13 keys 0.22 s -> 0.18 s,keys2560.15 s -> 0.11 s. Unchanged: the #108311 shape (prefetch still wins 13%), 20K-key two-level tables at 1 MiB L2 (prefetch stays on), 2M-key tables. Results are identical. The JOIN gate was measured and left unchanged. The 1.6x memory in the issue is only accounting (#107490 tracks these tables; withmax_untracked_memory = 1both versions peak at 19 MB).I could only measure on Intel; small-L2 hosts are emulated by setting
getL2CacheSize().New perf test:
group_by_two_level_few_keys.xml.Workflow [PR]
Sync PR [sync-upstream/pr/125897]
Version info
26.11.1.1565-master(included in26.11and later)