Repository navigation
Conversation
DISTINCTDISTINCT through aggregation
…gation # Conflicts: # src/Core/SettingsChangesHistory.cpp
Contributor
|
Workflow [PR], commit [46a5c41] Summary: ❌
|
Member
Author
|
Need streaming |
…tion Resolve the conflict with the move of the settings changes history into the setting declarations (ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as trailing arguments of the `DECLARE` of each setting. - retargeted PR record [email protected] to 26.10 (settings-history check rule) Also resolved conflicts in: - `src/Core/ProtocolDefines.h` - `src/Interpreters/executeQuery.cpp` - `src/Processors/QueryPlan/Optimizations/QueryPlanOptimizationSettings.cpp` - `src/Processors/QueryPlan/Optimizations/applyOrder.cpp` - `src/Processors/QueryPlan/ReadFromRemote.cpp` Master took query plan serialization versions 16 to 20, so this PR's aggregation limits now use version 21. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes: #114532
Related: #114550
Related: #48988
The final
DISTINCTdeduplicates on a single thread.DistinctStep::transformPipelineresizes the pipeline to one stream for the non-preliminary step and runs a singleDistinctTransformover it, with one hash set holding every distinct value. The preliminaryDISTINCTis parallel, but it only removes duplicates within its own stream, so everything that survives it still goes through that one set. As a resultSELECT DISTINCT a FROM tstops getting faster past a few threads, while the equivalentSELECT a FROM t GROUP BY ascales: aggregation fills a hash table per thread, switches to two-level tables when they grow, and merges the buckets in parallel.This replaces the final unordered
DISTINCTwith theAggregatingStepthatSELECT a FROM t GROUP BY awould produce. Deduplication then gets everything aggregation already has: per-thread hash tables, two-level conversion, the parallel bucket merge, packed string keys, prefetching, and the adaptive aggregator whenenable_adaptive_aggregatoris on. Constant columns take no part in the deduplication: they are left out of the keys and put back by an expression step after the aggregation.The new setting
query_plan_convert_distinct_to_aggregation, enabled by default, controls it. It is not applied when:DISTINCTkeeps the order of its input and aggregation would not.LIMIT,OFFSET, orLIMIT BY(or one of their negative and fractional variants) is above theDISTINCT, in the same query or an enclosing one: the streamingDISTINCTstops as soon as the limit is satisfied and returns the first values of its input, while an aggregation would consume the whole input first and return an arbitrary subset;DISTINCTforwards and aggregation discards;DISTINCTcolumns are constant, because the step then stops after its first non-empty chunk;DISTINCTkeys,allow_distinct_partitions_independentlylets the finalDISTINCTdeduplicate each partition's stream without merging them.max_rows_in_distinctandmax_bytes_in_distinctkeep their meaning. The row limit maps ontomax_rows_to_group_by, the byte limit onto a new internalAggregator::Params::max_bytes_to_group_by.Release build on a 64-core AMD (
x86_64). Times average the last three of five runs.SELECT DISTINCT a FROM tversusSELECT a FROM t GROUP BY aon aMergeTreetable withORDER BY tuple(), merged into one part, holding 20M distinctUInt64values (a = 0through19999999).max_threadsDISTINCT(master)DISTINCT(PR)GROUP BYWall-clock time with
query_plan_convert_distinct_to_aggregation = 0(before) and1(after):max_threadsUInt64,MergeTreeUInt64,numbers_mtString,numbers_mtAn explicit
GROUP BYis still slightly faster in the multithreaded runs because unlike it, the rewritten query keeps the preliminaryDISTINCTpass over its input.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Unordered
SELECT DISTINCTqueries with many distinct values are now several times faster: the finalDISTINCTstep is executed as an aggregation, so deduplication and merging run in parallel likeGROUP BYinstead of in a single thread. Up to 17 times faster high-cardinalityDISTINCT. The optimization is controlled by the new settingquery_plan_convert_distinct_to_aggregation, enabled by default.Workflow [PR]
Sync PR [sync-upstream/pr/119012]