Repository navigation
Faster top-K GROUP BY an ascending order or with a large LIMIT in Adaptive Aggregator - #130281
Conversation
|
Workflow [PR], commit [52d5cf1] Summary: ✅
AI ReviewSummaryThe PR shares exact top-K thresholds across adaptive aggregation merge threads, filters groups that cannot enter the result, and stops ascending merges once enough groups have the smallest possible rank. Review of commit Missing context / blind spots
Final VerdictStatus: ✅ Approve |
ch-samuel-reviewer-1
left a comment
There was a problem hiding this comment.
Thank you @nihalzp for such a nice PR! I left one small comment about the new test, otherwise it looks good to me.
This review was checked, modified and approved by human (@NIKTONIKTO717).
Move the two count-among-others cells of `05567_adaptive_aggregator_top_k_threshold` into the first call, which also checks `rows_before_limit_at_least`. The counter `AdaptiveAggregationCountFirstUnits` and the exact results were the same on master, so the old cells passed without the change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
[Review suggestion] Check that the threshold reaches the count-first units in 05567 (for ClickHouse#130281)
NIKTONIKTO717
left a comment
There was a problem hiding this comment.
Looks good, thank you for another really nice PR!
# Conflicts: # src/Interpreters/Aggregator.cpp # src/Interpreters/Aggregator.h # tests/queries/0_stateless/05318_adaptive_aggregator_top_k_pruning.reference # tests/queries/0_stateless/05318_adaptive_aggregator_top_k_pruning.sh # tests/queries/0_stateless/05322_adaptive_aggregator_top_k_count_first.sh
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes18 object files changed (+56.45 KiB total), 1 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units91 translation units recompiled, 1007 s compile time in total, 90 of them have a recent master baseline. |
Queries 0, 2, 3 and 6 of tests/performance/adaptive_aggregation_top_k_threshold.xml take 1.29-1.80 s on MasterCI amd (query 6 1.13 s on arm): each is a GROUP BY URL over the 100M rows of hits_100m_single, and the top-K threshold the file measures is a part of that time. They get disabled="1" and are appended as queries 7-10 with the table changed to hits_10m_single, nothing else. No existing query number or text changes. Measured with perf.py on official MasterCI release builds (ClickHouse#129258 series rig): - size: each copy takes 0.124-0.138 of its original's time (3 A/A passes x 25 runs), projected 174-223 ms amd and 109-140 ms arm; about 64 s amd and 40 s arm less query time per master perf job; - regime: the copies freeze every producer, merge, and the ascending ones settle (8-9 of 256 units merged at 8 threads, 9-13 at 12), like the originals do on CI; - sensitivity, the builds before and after ClickHouse#130281: originals 1.252x / 1.308x / 1.201x / 1.419x faster, copies 1.196x / 1.465x / 1.167x / 1.353x. Query 1 stays enabled: its ClickHouse#130281 effect locally (1.066x) is below three times the A/A noise, so there is no measurement that a copy keeps it. Related: ClickHouse#129258 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #123531
Background
For
GROUP BY ... ORDER BY count() LIMIT n, and the same withuniqExactoruniqExactIf, ClickHouse does not sort every group: each part of the aggregation result keeps only its ownnbest groups, and the sort picks the top among them (query_plan_aggregation_bucket_top_k). The adaptive aggregator (enable_adaptive_aggregator) produces its result in many small parts, merged in parallel. Whennis larger than a part, every part keeps all its groups, and nearly every group still reaches the sort:hitshas 18.34 million distinct URLs, and 18.15 million of them reach the sort to return 50,000. An ascending order is worse:ORDER BY c ASC LIMIT 10merges every part, although 11.5 million URLs have one row, so the first parts already hold ten of them.Changes
The parts now share what they find. Every finished part reports the exact counts of its groups, and the merge keeps a threshold that all its threads read: the
n-th best count reported so far. A group ranked worse than the threshold already hasnbetter groups ahead of it, so every later part drops it before the sort. In the query above, 716,482 groups now reach the sort.count(), 0 forcountof aNullablecolumn,uniqExactanduniqExactIf. No group left can rank better, andLIMITmay return any of the tied groups, so the merge skips the parts left:ORDER BY c ASC LIMIT 10at 64 threads now merges 56 of its 2,048 parts.The threshold is exact, so every query returns the same counts as before. Keeping it is cheap: the descending queries with a small
LIMIT, which gain nothing, run as fast as before.Results
ClickBench
hits(100M rows) on AWSc8a.16xlarge(64 cores, one thread per core),clickhouse-local,FORMAT Null, withcompile_aggregate_expressions = 0. Master is the merge base27662da641a. Peak memory is the peak of the query from--memory-usage.GROUP BY URL:count,DESC LIMIT 10000GROUP BY URL:count,DESC LIMIT 50000GROUP BY URL:count,ASC LIMIT 10GROUP BY URL:count,ASC LIMIT 10000GROUP BY cityHash64(WatchID) % 4000037:count,ASC LIMIT 10000GROUP BY cityHash64(WatchID) % 4000037:count,ASC LIMIT 50000GROUP BY SearchPhrase:count(DISTINCT UserID), whereSearchPhrase <> '',DESC LIMIT 10000Descending queries with
LIMIT10 or 1000 keep few groups per part anyway and stay the same, as do ClickBench's own queries, which all end inDESC LIMIT 10(Q12-Q18 and Q30-Q35: 1.00× at both widths).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The adaptive aggregator (
enable_adaptive_aggregator, enabled by default) runsGROUP BYwithORDER BY count() LIMIT nfaster whennis large or the order is ascending, and the same withuniqExactanduniqExactIf: the threads of its merge share the best counts found so far, drop the groups that cannot reach the top, and stop merging an ascending order once it holdsngroups with the smallest possible count. On seven such queries over the ClickBenchhitstable, the geometric mean speedup is 1.15x at 16 threads and 1.17x at 64 threads, up to 1.57x, and peak memory drops by up to 16%.Workflow [PR]
Sync PR [sync-upstream/pr/130281]
Version info
26.11.1.2056-master(included in26.11and later)