Skip to content

The distinct operation can be executed in parallel on a per-partition basis - #89129

Closed
lgbo-ustc wants to merge 2 commits into
ClickHouse:masterfrom
bigo-sg:distinct
Closed

lgbo-ustc wants to merge 2 commits into
ClickHouse:masterfrom
bigo-sg:distinct

Conversation

@lgbo-ustc

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Performance Improvement

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

Partition the data and process each partition in parallel to perform the distinct operation. when the distinct columns have high cardinality, this approach is faster than the current implementation and a group by.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@lgbo-ustc
lgbo-ustc force-pushed the distinct branch 4 times, most recently from c8c21e1 to b947724 Compare October 29, 2025 07:30
@lgbo-ustc

lgbo-ustc commented Oct 29, 2025 •

Copy link
Copy Markdown
Contributor Author

enable_partition_distinct=1 (after)

select distinct a,b from (select rand() as a , toString(number) as b from numbers(1e7)) Format Null settings 

enable_partition_distinct=1

0 rows in set. Elapsed: 0.175 sec. Processed 10.00 million rows, 80.00 MB (57.00 million rows/s., 456.02 MB/s.)


select distinct a from (select rand() as a , toString(number) as b from numbers(1e7)) Format Null settings enable_partition_distinct=1

0 rows in set. Elapsed: 0.096 sec. Processed 10.00 million rows, 80.00 MB (104.57 million rows/s., 836.53 MB/s.)

enable_partition_distinct=0

select distinct a,b from (select rand() as a , toString(number) as b from numbers(1e7)) Format Null settings 
enable_partition_distinct=0

0 rows in set. Elapsed: 5.012 sec. Processed 10.00 million rows, 80.00 MB (2.00 million rows/s., 15.96 MB/s.)


select a from (select rand() as a , toString(number) as b from numbers(1e7)) group by a Format Null settings enable_partition_distinct=0
0 rows in set. Elapsed: 1.026 sec. Processed 10.00 million rows, 80.00 MB (9.75 million rows/s., 77.97 MB/s.)

use a group by

select a,b from (select rand() as a , toString(number) as b from numbers(1e7)) group by a, b Format Null

0 rows in set. Elapsed: 3.094 sec. Processed 10.00 million rows, 80.00 MB (3.23 million rows/s., 25.86 MB/s.)


select a from (select rand() as a , toString(number) as b from numbers(1e7)) group by a Format Null

0 rows in set. Elapsed: 0.700 sec. Processed 10.00 million rows, 80.00 MB (14.28 million rows/s., 114.25 MB/s.)

@lgbo-ustc
lgbo-ustc marked this pull request as ready for review October 29, 2025 09:00
for (size_t i = 0; i < output_streams; ++i)
{
Chunk output_chunk(columns, rows);
output_chunk.getChunkInfos().add(std::make_shared<PartitionedChunkInfo>(selectors[i], ref_count, filter));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If filter is attached to chunk, does it mean that this step cannot be pushed down ORDER BY transforms, because in this case order may change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @UnamedRus can you explain more about this case ? I don't quite understand the situation being described.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's scattered without copying data, I think the order should be kept in a block

@UnamedRus UnamedRus Oct 29, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClickHouse have a feature to "split" distinct step in predistinct before ORDER BY steps, and (final) distinct after them.
And PartialSortingTransform to reorder data within block.
But, probably it shouldn't cause problem in your case, if you will have both DISTINCT steps before sorting transforms
graphviz (1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, may be you can consider using two-level hashtables (as this feature cater to DISTINCT with many keys) they can have much nicer rescaling properties (as individual bucket can be scaled up, instead of whole table).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, may be you can consider using two-level hashtables (as this feature cater to DISTINCT with many keys) they can have much nicer rescaling properties (as individual bucket can be scaled up, instead of whole table).

Yes, it should be better to support two-level hash table.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 16, 2025
@clickhouse-gh

clickhouse-gh Bot commented Nov 16, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f4d2754]

Summary: ❌

job_name test_name status info comment
Style check failure
cpp failure cidb
PR formatter dropped
Fast test failure
01881_join_on_conditions_merge FAIL cidb
00200_shard_distinct_order_by_limit_distributed FAIL cidb
02317_distinct_in_order_optimization_explain FAIL cidb
01134_set_overflow_mode FAIL cidb
02862_sorted_distinct_sparse_fix FAIL cidb
03413_experimental_settings_cannot_be_enabled_by_default FAIL cidb
01582_distinct_optimization FAIL cidb
02535_analyzer_group_by_use_nulls FAIL cidb
01655_plan_optimizations FAIL cidb
00635_shard_distinct_order_by FAIL cidb
13 more test cases not shown
Build (arm_tidy) failure
Build ClickHouse failure cidb
Build (amd_debug) dropped
Build (amd_asan) dropped
Build (amd_tsan) dropped
Build (amd_msan) dropped
Build (amd_ubsan) dropped
Build (amd_binary) dropped

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 16, 2025
@Algunenano Algunenano self-assigned this Nov 17, 2025
@clickhouse-gh

clickhouse-gh Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Dear @Algunenano, 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.

@abashkeev abashkeev added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Sep 3, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Thank you for the contribution! The same approach (hash-partitioning the rows by the DISTINCT columns and deduplicating each partition in parallel) is implemented in #114550 by reusing the existing scatterByPartition in DistinctStep, so I'm closing this one in favor of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants