Repository navigation
Conversation
Aggregations with lots of buckets can get expensive. Here's an excerpt from a profile:
```
ROUTINE ======================== DB::IAggregateFunctionHelper<DB::AggregateFunctionSum<unsigned long, unsigned long, DB::AggregateFunctionSumData<unsigned long>, (DB::AggregateFunctionSumType)0> >::addBatch(unsigned long, unsigned long, char**, unsigned long, DB::IColumn const**, DB::Arena*, long) const in src/AggregateFunctions/IAggregateFunction.h
1.79s 2.32s (flat, cum) 15.07% of Total
. . 549: }
. . 550: }
. . 551: else
. . 552: {
. . 553: for (size_t i = row_begin; i < row_end; ++i)
1.79s 1.79s 554: if (places[i])
. 526.32ms 555: static_cast<const Derived *>(this)->add(places[i] + place_offset, columns, i, arena);
. . 556: }
. . 557: }
. . 558:
. . 559: void serializeBatch(const PaddedPODArray<AggregateDataPtr> & data, size_t start, size_t size, WriteBuffer & buf, std::optional<size_t> version) const final // NOLINT
. . 560: {
```
We can get some of this back by specializing the common non-null version.
For a big query doing a very wide aggregation:
* 1 thread: 77.7s -> 74.3s (both wall and cpu)
* 64 threads: 17.0s -> 16.0s (wall), 99.5s -> 92.4s (cpu)
With a synthetic benchmark:
* Baseline: 1.24s
```
$ ../bench.py --clickhouse=/tmp/clickhouse-baseline --path=$HOME/clickhouse-no-nulls --runs=3 --query="SELECT toUInt16(bitAnd(number, 8191)) AS key, sum(number) FROM numbers(500000000) GROUP BY key FORMAT Null SETTINGS max_threads = 1"
wall: 1.24s, cpu: 1.23s, memory: 0.00GiB
wall: 1.25s, cpu: 1.24s, memory: 0.00GiB
wall: 1.23s, cpu: 1.23s, memory: 0.00GiB
```
* Patched: 1.10s
```
$ ../bench.py --clickhouse=/tmp/clickhouse-no-nulls --path=$HOME/clickhouse-no-nulls --runs=3 --query="SELECT toUInt16(bitAnd(number, 8191)) AS key, sum(number) FROM numbers(500000000) GROUP BY key FORMAT Null SETTINGS max_threads = 1"
wall: 1.10s, cpu: 1.09s, memory: 0.00GiB
wall: 1.10s, cpu: 1.09s, memory: 0.00GiB
wall: 1.11s, cpu: 1.10s, memory: 0.00GiB
```
Move the non-null batch implementation to `IAggregateFunctionHelper` and propagate the guarantee through `AggregateFunctionIf`, `AggregateFunctionOrFill`, and `AggregateFunctionTuple`. Preserve specialized timeseries batching. For `sumOrDefault` over 500 million rows and 8192 groups: 1.64s -> 1.5s. No measurable change was observed for `sumIf`.
Aggregations with lots of buckets can get expensive. Here's an excerpt from a profile:
```
ROUTINE ======================== DB::IAggregateFunctionHelper<DB::AggregateFunctionSum<unsigned long, unsigned long, DB::AggregateFunctionSumData<unsigned long>, (DB::AggregateFunctionSumType)0> >::addBatch(unsigned long, unsigned long, char**, unsigned long, DB::IColumn const**, DB::Arena*, long) const in src/AggregateFunctions/IAggregateFunction.h
1.79s 2.32s (flat, cum) 15.07% of Total
. . 549: }
. . 550: }
. . 551: else
. . 552: {
. . 553: for (size_t i = row_begin; i < row_end; ++i)
1.79s 1.79s 554: if (places[i])
. 526.32ms 555: static_cast<const Derived *>(this)->add(places[i] + place_offset, columns, i, arena);
. . 556: }
. . 557: }
. . 558:
. . 559: void serializeBatch(const PaddedPODArray<AggregateDataPtr> & data, size_t start, size_t size, WriteBuffer & buf, std::optional<size_t> version) const final // NOLINT
. . 560: {
```
We can get some of this back by specializing the common non-null version.
For a big query doing a very wide aggregation:
* 1 thread: 77.7s -> 74.3s (both wall and cpu)
* 64 threads: 17.0s -> 16.0s (wall), 99.5s -> 92.4s (cpu)
With a synthetic benchmark:
* Baseline: 1.24s
```
$ ../bench.py --clickhouse=/tmp/clickhouse-baseline --path=$HOME/clickhouse-no-nulls --runs=3 --query="SELECT toUInt16(bitAnd(number, 8191)) AS key, sum(number) FROM numbers(500000000) GROUP BY key FORMAT Null SETTINGS max_threads = 1"
wall: 1.24s, cpu: 1.23s, memory: 0.00GiB
wall: 1.25s, cpu: 1.24s, memory: 0.00GiB
wall: 1.23s, cpu: 1.23s, memory: 0.00GiB
```
* Patched: 1.10s
```
$ ../bench.py --clickhouse=/tmp/clickhouse-no-nulls --path=$HOME/clickhouse-no-nulls --runs=3 --query="SELECT toUInt16(bitAnd(number, 8191)) AS key, sum(number) FROM numbers(500000000) GROUP BY key FORMAT Null SETTINGS max_threads = 1"
wall: 1.10s, cpu: 1.09s, memory: 0.00GiB
wall: 1.10s, cpu: 1.09s, memory: 0.00GiB
wall: 1.11s, cpu: 1.10s, memory: 0.00GiB
```
Move the non-null batch implementation to `IAggregateFunctionHelper` and propagate the guarantee through `AggregateFunctionIf`, `AggregateFunctionOrFill`, and `AggregateFunctionTuple`. Preserve specialized timeseries batching. For `sumOrDefault` over 500 million rows and 8192 groups: 1.64s -> 1.5s. No measurable change was observed for `sumIf`.
…tinity/ClickHouse into backports/antalya-26.8/117009
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
...
Documentation entry for user-facing changes
...
CI/CD Options
Exclude tests:
Regression jobs to run: