ClickHouse:按不同时间范围聚合数据的有效方法

ClickHouse: Efficient way to aggregate data by different time ranges at

我需要聚合不同时间段的时间序列数据(具有平均函数),例如:

问题 1: 能否在 GROUP BY 语句内或至少使用单个查询完成?

问题 2: 我需要任何物化视图吗?

table 被 Month 分割并被 UserID

分片

所有查询都在UserID(单个分片)

使用 ROLLUP 分组

create table xrollup(metric Int64, b date, v Int64 ) engine=MergeTree partition by tuple() order by tuple();
insert into xrollup values (1,'2018-01-01', 1), (1,'2018-01-02', 1), (1,'2018-02-01', 1), (1,'2017-03-01', 1);
insert into xrollup values (2,'2018-01-01', 1), (2,'2018-02-02', 1);

SELECT metric, toYear(b) y, toYYYYMM(b) m, SUM(v) AS val
FROM   xrollup
GROUP BY metric, y, m  with ROLLUP
ORDER BY metric, y, m 

┌─metric─┬────y─┬──────m─┬─val─┐
│      0 │    0 │      0 │   6 │  overall
│      1 │    0 │      0 │   4 │  overall by metric1
│      1 │ 2017 │      0 │   1 │  overall by metric1 for 2017
│      1 │ 2017 │ 201703 │   1 │  overall by metric1 for march 2017
│      1 │ 2018 │      0 │   3 │
│      1 │ 2018 │ 201801 │   2 │
│      1 │ 2018 │ 201802 │   1 │
│      2 │    0 │      0 │   2 │
│      2 │ 2018 │      0 │   2 │
│      2 │ 2018 │ 201801 │   1 │
│      2 │ 2018 │ 201802 │   1 │
└────────┴──────┴────────┴─────┘