Repository navigation
Speed up DISTINCT by running it independently per MergeTree partition - #108326
alexey-milovidov merged 33 commits into
Conversation
…st in partition order
|
Workflow [PR], commit [8c29cc5] Summary: ✅
AI ReviewSummaryThis PR adds a per-partition Findings
Tests
Final VerdictNeeds changes before merge: the size-limit contract is still violated in the preliminary request pass. |
|
📊 Cloud Performance Report ✅ AI verdict: This PR adds a default-on independent per-partition DISTINCT plan optimization plus a stream-disjointness propagation pass; it only activates when a table's partition key is a deterministic function of the DISTINCT/GROUP BY columns across many balanced partitions, which the unpartitioned ClickBench schema never triggers. Consistent with that, none of the flagged deltas are attributable to the change: Q4's -19.2% is a COUNT(DISTINCT) aggregation that the new DISTINCT step doesn't touch and sits inside master's variance band with flat CPU, and the small +5-7% GROUP BY deltas on Q28/Q32/Q33 are borderline or within-envelope noise. I sanity-checked the envelope-suppressed rows (Q4, Q33) against the diff and found no inconsistency — the deterministic no_change/not_sure verdicts all hold. clickbenchFlagged queries (3 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
|
Related too in case you want to have a look: Related: #89129 / ClickHouse/clickhouse-private#57450 In case you want to have a look |
|
Dear @novikd, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
…ndent-partitions plan
…ted disjointness
…-partitions optimization
| if (!dag) | ||
| return; | ||
|
|
||
| if (!reading->willOutputEachPartitionThroughSeparatePort() |
There was a problem hiding this comment.
The size-limit guard is too late here. applyStreamDisjointness now refuses to skip the final merge when max_rows_in_distinct / max_bytes_in_distinct is set, but by the time that pass runs this request has already flipped the read into output_each_partition_through_separate_port.
That means a size-limited query still changes the preliminary DistinctTransform stream topology even though the final merge stays global. This contradicts the new contract in Settings.cpp ("Not applied when max_rows_in_distinct or max_bytes_in_distinct is set"), and it can still change user-visible behavior for distinct_overflow_mode = 'break' as well as EXPLAIN / join-sharding decisions. The same has_size_limits check needs to live in this request pass too, before we touch the read step.
There was a problem hiding this comment.
This is intentional so that pre-distinct can benefit regardless. Skipping this optimization for pre-distinct is not beneficial if max_rows_in_distinct / max_bytes_in_distinct is set because pre-distinct is already checks these metric per stream.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 259/265 (97.74%) · Uncovered code |
…the new join algorithm from ClickHouse#109005 - tests/clickhouse-test: randomize `allow_distinct_partitions_independently`, `force_distinct_partitions_independently`, `max_number_of_partitions_for_independent_distinct` and `enable_parallel_single_level_merge`. `force_*` is conditional on `allow_*`, because forcing is a no-op when the optimization is not allowed. - ci/jobs/scripts/stress/stress.py: add `parallel_full_sorting_merge` to the `join_algorithm` rotation (skipped in Upgrade check, because the previous release may not know this value). - BuzzHouse: add the four new settings next to their aggregate analogs. Related: ClickHouse#109005 Related: ClickHouse#108326 Related: ClickHouse#110395 Co-Authored-By: Claude Fable 5 <[email protected]>
Related: #105126
If the partition expression is a function of the
DISTINCTcolumns, pre-DISTINCTwill now request data to be emitted in each partition from a single port. This will significantly reduce data for the next steps because, unlike the base case where a single key may be present in all streams, here only one stream will have it.Then, this PR adds
applyStreamDisjointness, which works similarly toapplyOrderbut propagates data disjointness. And, for common shapes, this can reach the finalDISTINCT, and the finalDISTINCTwill run in parallel instead of merging streams into one.applyStreamDisjointnessis more broad in that it can propagate this property to downstreamGROUP BYandLIMIT BYas well, making them run in parallel as well!Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Speed up and reduce the memory usage of
DISTINCTqueries on partitionedMergeTreetables by keeping each partition's rows within a single stream, so each stream's preliminaryDISTINCTworks on a disjoint set of keys, instead of the same set of keys appearing in multiple preliminaryDISTINCTs. This applies when the partition expression is a deterministic function of theDISTINCTcolumns, and is gated by a cost heuristic on partition count and skew (which can be toggled by settingforce_distinct_partitions_independently, disabled by default). The same per-partition disjointness is propagated throughExpression,FilterandARRAY JOINsteps to the finalDISTINCT,LIMIT BYandGROUP BYconsumers so they can skip their merge too. Controlled by the new settingsallow_distinct_partitions_independently(enabled by default) andmax_number_of_partitions_for_independent_distinctto tune the heuristic.