Skip to content

[draft] parallel distinct in order - #48988

Closed
devcrafter wants to merge 3 commits into
masterfrom
igor/parallel-distinct-in-order
Closed

devcrafter wants to merge 3 commits into
masterfrom
igor/parallel-distinct-in-order

Conversation

@devcrafter

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Avoid the final single-threaded distinct step by reading in sorted non-intersecting ranges from MergeTree storage

@devcrafter
devcrafter marked this pull request as draft April 20, 2023 19:18
@robot-clickhouse robot-clickhouse added the pr-performance Pull request with some performance improvements label Apr 20, 2023
@devcrafter

Copy link
Copy Markdown
Member Author

@robot-clickhouse-ci-1

robot-clickhouse-ci-1 commented Jul 14, 2023 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit 742995e with description of existing statuses. It's updated for the latest CI running
The full report is available here
The overall status of the commit is 🔴 failure

Check nameDescriptionStatus
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR🟡 pending
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here🔴 failure
Mergeable CheckChecks if all other necessary checks are successful🟢 success
Push to DockerhubThe check for building and pushing the CI related docker images to docker hub🟢 success
Style CheckRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report🟢 success

@clickhouse-gh

clickhouse-gh Bot commented Jun 25, 2024

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

12 similar comments
@clickhouse-gh

clickhouse-gh Bot commented Aug 27, 2024

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Oct 29, 2024

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Dec 31, 2024

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Mar 4, 2025

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented May 6, 2025

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2025

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2025

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2025

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Jan 13, 2026

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Dear @devcrafter, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@alexey-milovidov

Copy link
Copy Markdown
Member

Closing this draft: the motivation is still valid, but nothing here can be revived.

The problem is unchanged - the final DISTINCT is still single-threaded (DistinctStep::transformPipeline does pipeline.resize(1)), and on 20M distinct UInt64 it does not scale past 4 threads while the equivalent GROUP BY scales ~7.7x. I filed #114532 with the numbers so the problem stays tracked.

The patch itself no longer applies in any part: src/Core/Settings.h is gone, distinctReadInOrder.cpp (the file this mostly edits) was deleted and its logic moved into optimizeDistinctInOrder in optimizeReadInOrder.cpp, DataStream/ActionsDAGPtr no longer exist, and spreadMarkRangesAmongStreamsFinal - where the add_merging_final=false flag was added - has been rewritten and partly moved into MergeTreeFinalMerge.cpp. The draft also still carries its debug logging, has no test, and was restricted to DISTINCT columns matching the whole sorting key.

Two things have landed since that make a fresh attempt cheaper: PartsSplitter now exposes splitPartsRanges and friends as a reusable API, and optimizeAggregationPerPartition/optimizeLimitByPerPartition establish the pattern for skipping a single-threaded merge stage. I am working on a replacement based on repartitioning the pipeline by hash before the final pass, which covers every DISTINCT rather than only the sorting-key-aligned ones.

Blargian pushed a commit to yiyang-shao/ClickHouse that referenced this pull request Oct 20, 2026
The final `DISTINCT` merged all of its input streams into one and deduplicated
them on a single thread, so `SELECT DISTINCT` did not scale with `max_threads`
while the equivalent `GROUP BY` did - aggregation parallelizes its merge over
two-level hash tables, `DISTINCT` had nothing of the sort.

Repartition the input streams by the hash of the `DISTINCT` columns instead of
merging them: equal values then land in the same stream, so every stream can be
deduplicated on its own, and what is left to merge is already deduplicated. The
repartitioning uses `scatterByPartition`, which `SortingStep` and
`ShuffleSendStep` already use.

The new setting `allow_parallel_distinct`, on by default, controls it. Whether
the order of the step's output is free is only known where the sorting
properties are, so the decision is taken in `applyOrder`. It is not applied
when the input is globally sorted, when `max_rows_in_distinct` or
`max_bytes_in_distinct` is set, when there is a `LIMIT`, or when the streams
are already disjoint - `allow_distinct_partitions_independently` covers that
case and skips the merge entirely.

Every input chunk is split across all partitions, so the number of partitions
is capped rather than following `max_threads`: on `SELECT DISTINCT number FROM
numbers_mt(4e7)` the total CPU time the scatter adds grows by 8% at 4
partitions, 20% at 16 and 96% at 96.

Wall clock, minimum of 3 runs, aarch64:

    workload                     threads    before     after   speedup
    20M distinct UInt64                4   0.442 s   0.205 s     2.16x
    (MergeTree)                       16   0.458 s   0.148 s     3.09x
                                      96   0.502 s   0.165 s     3.04x
    40M distinct UInt64                4   0.965 s   0.545 s     1.77x
    (numbers_mt)                      16   0.929 s   0.360 s     2.58x
                                      96   0.886 s   0.299 s     2.96x
    8M distinct String                 4   0.515 s   0.288 s     1.79x
    (numbers_mt)                      16   0.517 s   0.093 s     5.56x
                                      96   0.518 s   0.083 s     6.24x

`DISTINCT` without `ORDER BY` has no defined row order, and the repartitioning
changes the order it happens to produce. Two tests relied on it:
`04109_sort_propagation_apply_order` asserts that per-stream sortedness reaches
`DISTINCT`, so it pins the setting off, and
`00585_union_all_subquery_aggregation_column_removal` gets the explicit
`ORDER BY` it was missing.

Closes: ClickHouse#114532
Related: ClickHouse#48988

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants