Skip to content

Instantly share code, notes, and snippets.

@den-crane
Last active February 10, 2023 04:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save den-crane/dcec8056ccd9f129f401817bc8036d0e to your computer and use it in GitHub Desktop.
Save den-crane/dcec8056ccd9f129f401817bc8036d0e to your computer and use it in GitHub Desktop.
AggregatingMergeTree + projection
create table test (
A Int64,
B String,
SomeID AggregateFunction(uniq, Int64),
projection p1 (select B, uniqMergeState(SomeID) group by B)
)
Engine=AggregatingMergeTree order by (A, B);
insert into test select number A, number%3 B, uniqState(toInt64(rand64())) from numbers(1e7) group by A,B;
select B, finalizeAggregation(uniqMergeState(SomeID)) from test group by B
settings allow_experimental_projection_optimization = 0;
┌─B─┬─finalizeAggregation(uniqMergeState(SomeID))─┐
│ 2 │ 3315471 │
│ 1 │ 3329550 │
│ 0 │ 3325098 │
└───┴─────────────────────────────────────────────┘
3 rows in set. Elapsed: 0.364 sec. Processed 10.00 million rows, 1.46 GB (27.49 million rows/s., 4.00 GB/s.)
select B, finalizeAggregation(uniqMergeState(SomeID)) from test group by B;
┌─B─┬─finalizeAggregation(uniqMergeState(SomeID))─┐
│ 1 │ 3329550 │
│ 0 │ 3325098 │
│ 2 │ 3315471 │
└───┴─────────────────────────────────────────────┘
3 rows in set. Elapsed: 0.014 sec.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment