Skip to content

Enable count_distinct_optimization by default - #88281

Closed
Algunenano wants to merge 3 commits into
ClickHouse:masterfrom
Algunenano:count_distinct_optimization
Closed

Algunenano wants to merge 3 commits into
ClickHouse:masterfrom
Algunenano:count_distinct_optimization

Conversation

@Algunenano

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Improve performance of some countDistinct queries by enabling the setting count_distinct_optimization by default. This setting rewrites queries in the shape of SELECT countDistinct(a) FROM t into SELECT count() FROM (SELECT a FROM t GROUP BY a).

Proposed by @amosbird in #81944

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@Algunenano Algunenano self-assigned this Oct 9, 2025
@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0512bbf]

Summary: ❌

job_name test_name status info comment
Fast test failure
01244_optimize_distributed_group_by_sharding_key FAIL 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
Build (arm_asan) dropped
Build (arm_binary) dropped
Build (arm_tsan) dropped

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 9, 2025
@Algunenano

Algunenano commented Oct 9, 2025 •

Copy link
Copy Markdown
Member Author

Some notes about this:

  • The optimization is currently broken in the analyzer, because it fails to detect multiple columns correctly. It was fixed in Fix count distinct rewrite safety #81944

  • It's also broken with some shard queries. It was addressed in Fix count distinct rewrite safety #81944

  • After fixing the first issue locally I noticed a few things:

    • Q4 gets faster by around 1.05x
    • Q5 gets slower by around 1.2x
    • Implementing it for multiple columns is trivial. But testing with SELECT COUNT(DISTINCT SearchPhrase, UserID) FROM hits leads to the query becoming 2x as slow as before.
    • It's not necessary to project the columns, we can just project a 1. There doesn't seem to be a difference, probably because the analyzer will understand the columns are not used and not project them anyway.
    • The optimization skips allow_suspicious_types_in_group_by, pushing variant or other dynamic types into group by. I'm not sure if this is ok or different from countDistinct(variant).

I'll need to investigate more and compare profiles with and without the optimization, because it seems both are suboptimal in different cases

@alexey-milovidov

Copy link
Copy Markdown
Member

This is ok:

SELECT uniqExact(x)
FROM
(
    SELECT CAST(CAST('1', 'UInt64'), 'Variant(UInt64, Int64)') AS x
    UNION ALL
    SELECT CAST(CAST('1', 'Int64'), 'Variant(UInt64, Int64)')
)

Query id: 5b195dc2-07ef-40f0-8644-f0da9dfae231

   ┌─uniqExact(x)─┐
1. │            2 │
   └──────────────┘

it counts distinct values and types - this semantic is reasonable.

@clickhouse-gh

clickhouse-gh Bot commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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

Continued in #104472 with an update, the fix for distributed queries, and more tests.

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.

2 participants