Skip to content

Speed up uniq over numeric types by batching inserts when possible - #95904

Merged
Algunenano merged 5 commits into
ClickHouse:masterfrom
Algunenano:uniq_insertMany
Feb 6, 2026
Merged

Algunenano merged 5 commits into
ClickHouse:masterfrom
Algunenano:uniq_insertMany

Conversation

@Algunenano

@Algunenano Algunenano commented Feb 3, 2026 •

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):

Speed up uniq over numeric types by batching inserts when possible (not null, not -If, no GROUP BY, no IPv6 or String).

We have some performance tests already in cpu_synthetic:

<query>SELECT uniq(UserID) FROM hits_10m_single SETTINGS max_threads = 1</query>
<query>SELECT uniq(UserID) FROM hits_100m_single</query>

For reference, in my PC with release (no-LTO) build, clickhouse benchmark --port 49000 --query "SELECT uniq(UserID) from test.hits settings max_threads = 1" -i 200 did 27.3 QPS, and now it does 33.6 QPS, that is 1.23x as fast.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.2.1.326

@clickhouse-gh

clickhouse-gh Bot commented Feb 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c6b037b]

Summary: ❌

job_name test_name status info comment
Stress test (amd_msan) failure
Logical error: Block structure mismatch in A stream: different number of columns: (STID: 0993-38e6) FAIL cidb, issue ISSUE EXISTS

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Feb 3, 2026
@Algunenano

Copy link
Copy Markdown
Member Author

x86

Relevant only

image image image

The speed up in the destroy_aggregated_states is unexpected, since it should be adding values sequentially, not in batches. I'll have a look to confirm.

ARM

image image image image image

Again I see a speed up in destroy_aggregate_states, but also a slow down. Both I cannot explain so I'll have a look to see if it's related to the code changes or just due to changes in the binary layout.

There is also a slow down in set_index which I don't think uses any common code (it's a different set) but I'll investigate to confirm too

…e same

It isn't necessarily better or worse, just different. And the main change
is not intended to alter this
@Algunenano

Copy link
Copy Markdown
Member Author

Checking the improvement to destroy_aggregate_states:

  • It's using ::addBatch, which calls add. The only change to add is the minor style change to insertImpl
  • Checking the assembly there are some differences. Basically it changes which branch the compiler predicts will be more common to optimize, either the value already exists or the value is new.

I'm going to remove this change so the comparison of the batch changes is more precise (less noise for the change here).

@Algunenano

Copy link
Copy Markdown
Member Author
image image image image

I also checked the set_index changes and it does not use this code, it uses DB::Set::insertFromBlockImplCase

@pamarcos
pamarcos requested review from Copilot and pamarcos February 6, 2026 10:50

Copilot AI left a comment

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.

Pull request overview

This PR optimizes the uniq aggregate function for numeric types by introducing batch processing for hash value computation and insertion operations. The optimization applies when processing non-null numeric columns (excluding IPv6 and String types) without conditional aggregation flags.

Changes:

  • Added a new insertMany method to UniquesHashSet that processes elements in batches of 8
  • Modified AggregateFunctionUniq to use the batched insertion path for eligible numeric types
  • Added boundary condition tests to verify correctness across different input sizes

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/AggregateFunctions/UniquesHashSet.h Implements batched hash computation and insertion logic with configurable batch size
src/AggregateFunctions/AggregateFunctionUniq.h Routes eligible numeric types through the new batched insertion path
tests/queries/0_stateless/03821_uniq_speedup_boundaries.sql Adds test cases for batch boundaries and correctness verification
tests/queries/0_stateless/03821_uniq_speedup_boundaries.reference Expected output for boundary condition tests

Comment thread src/AggregateFunctions/UniquesHashSet.h Resolved
Comment thread src/AggregateFunctions/UniquesHashSet.h Resolved
Comment thread src/AggregateFunctions/UniquesHashSet.h Resolved
Comment thread src/AggregateFunctions/UniquesHashSet.h Resolved
@Algunenano

Copy link
Copy Markdown
Member Author

Some perf test failed because play wasn't accessible. I'm rerunning failed jobs

@pamarcos pamarcos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Couple of minor comments, but LGTM

SELECT uniq(number) FROM numbers(17); -- Two full batches + 1

-- Force resize during batching (depends on UNIQUES_HASH_MAX_SIZE)
-- Note this returns 100315 due to BJKST thinning (expect ~100000, actual varies).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I find the comment misleading, considering we expect a fixed value of 100315

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You are right. The file expects 100315. A person seeing the query expects 1000000. I added the comment to make it more clear but I'm not sure it helps

Comment on lines +383 to +397
while (buf[place_value] && buf[place_value] != x)
{
++place_value;
place_value &= mask();

#ifdef UNIQUES_HASH_SET_COUNT_COLLISIONS
++collisions;
#endif
}

if (buf[place_value] == x)
continue;

buf[place_value] = x;
++m_size;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nitpick: this code is exactly the same as the one in insertImpl. I guess this is on purpose cause refactoring to consolidate into a single method doesn't seem to be much worthy.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agree

@Algunenano
Algunenano enabled auto-merge February 6, 2026 15:12
@Algunenano
Algunenano added this pull request to the merge queue Feb 6, 2026
Merged via the queue into ClickHouse:master with commit 7906ebc Feb 6, 2026
250 of 258 checks passed
@Algunenano
Algunenano deleted the uniq_insertMany branch February 6, 2026 15:26
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 6, 2026
@fm4v

fm4v commented Feb 9, 2026

Copy link
Copy Markdown
Member

@Algunenano

Copy link
Copy Markdown
Member Author

@Algunenano destroy_aggregate_states have significant slowdown for ARM

The generated assembly is the same.: #95904 (comment)

In fact you can look for recent runs in master and the timings are the same as they were in the reference of this PR: https://s3.amazonaws.com/clickhouse-test-reports/REFs/master/8bf15e5389183849f574b0b90750ed50db66e22b/performance_comparison_arm_release_release_base_5_6/all-queries.html

image

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 pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants