Repository navigation
Cherry pick #125897 to 26.8: Do not prefetch in a two-level GROUP BY that holds few keys - #129833
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 #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 (#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 #108311 shape, 20K-key two-level tables at 1 MiB L2 (prefetch stays on), 2M-key single-level tables Closes #125845 Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 #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: #126578
`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]>
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]>
Do not prefetch in a two-level GROUP BY that holds few keys
|
The three conflicts ( On the 26.8 tree (26.8.35.1, clang-22, debug) I cannot push to |
|
@groeneai please resolve conflicts and re-run the CI |
|
I don't have push access to this repository, so I can't push to After that this PR becomes mergeable, and the robot merges it and opens the 26.8 backport PR, where CI runs. |
|
@copilot resolve the merge conflicts in this pull request |
…use/ClickHouse into cherrypick/26.8/125897 # Conflicts: # src/Common/ColumnsHashing.h # src/Common/HashTable/Prefetching.h # src/Interpreters/Aggregator.cpp Co-authored-by: nickitat <[email protected]>
|
|
df2bb81
into
backport/26.8/125897
Original pull-request #125897
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job