Skip to content

Add bounded distinct count optimization #21051

@brancz

Description

@brancz

Is your feature request related to a problem or challenge?

Optimize queries like

SELECT count(DISTINCT col) > 1 FROM table;

For cases where col has high cardinality. The issue is that currently the distinct accumulates all values first and only then are they counted, so we both insert values into the distinct accumulator, leaving CPU on the table, and we pay for memory unnecessarily.

Describe the solution you'd like

Add a bound to count distinct and only insert into the tracked values collection up to N+1.

Describe alternatives you've considered

n/a

Additional context

I have a prototype of this working, if the general idea sounds good, I can clean it up and submit the PR.

@alamb

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions