首页 > 解决方案 > 我可以在不经过物化视图的情况下在 AggregatingMergeTree 中插入聚合状态吗

问题描述

我一直在使用 AggregatingMergeTree 通过从原始数据表生成聚合状态的物化视图填充它们。这很好用。

我想知道是否有一种方法可以从我收到的数组中生成聚合状态,而无需通过每个数据点包含一行的表。例子:

我发现我可以通过这种方式从数组中生成聚合状态:

SELECT countState([1,2,3,4,5])
SELECT uniqState(['user1', 'user2', 'user3'])
# Does not work with quantiles

尽管我没有设法将它们直接插入聚合合并树中。例子:

CREATE TABLE state_test_agg(
    id UInt64,
    count_state AggregateFunction(count, UInt32),
    uniq_state AggregateFunction(uniq, String)
)
ENGINE = AggregatingMergeTree()
ORDER BY (id)

INSERT INTO state_test_agg (id, count_state, uniq_state) 
VALUES (1, countState([1,2,3,4]), uniqState(['a', 'b', 'b']))


Code: 184. DB::Exception: Aggregate function countState([1, 2, 3, 4]) is found in wrong place in query

相反,如果我首先将存储桶(数组)存储在一个表中并尝试将它们移动到我得到的聚合合并树中

CREATE TABLE state_test_raw(
    id UInt64,
    count_bucket Array(UInt64),
    uniq_bucket Array(String)
)
ENGINE MergeTree
ORDER BY id

INSERT INTO state_test_raw (
    id, 
    count_bucket, 
    uniq_bucket)
VALUES
(1, [1,2,3,4,5,5], ['a', 'b', 'c'])

-- so far so good

INSERT INTO state_test_agg 
VALUES (id, count_state, uniq_state) 
SELECT 
    id, 
    countState(count_bucket) as count_state,  
    uniqState(uniq_bucket) as uniq_state 
FROM state_test_raw
GROUP BY id

Code: 47. DB::Exception: Missing columns: 'id' while processing query: 'id', required columns: 'id', source columns: '_dummy'

尚不清楚,尽管我认为这是一个问题,因为 Clickhouse 无法知道 id 列是否唯一。

有没有办法插入从数组而不是通过物化视图生成的聚合状态?

场景:我想记录指标并在其上构建汇总表。简单的方法是将每个数据点存储为 MergeTree 中的一行,然后创建一个物化视图,通过填充聚合合并树来生成汇总。例子:

timestamp, response_time, 200
timestamp2, response_time, 205
...

尽管这些数据点已经在源(因此是数组)中存储。

time_bucket_1, response_time, [200, 205]

我可以扩展它们并为每个数据点写入一行,但似乎我可以通过跳过该步骤并尝试直接从存储桶生成聚合状态来节省 Clickhouse 的存储和带宽,以及实现步骤使用的内存

谢谢。菲利波

标签: clickhouse

解决方案


考虑使用arrayReduce将项目数组转换为聚合状态:

WITH
    arrayReduce('countState', [1, 2, 3, 4, 5]) AS state_1,
    arrayReduce('uniqState', ['user1', 'user2', 'user3', 'user1']) AS state_2
SELECT
    countMerge(state_1) AS merge_1,
    uniqMerge(state_2) AS merge_2

/*
┌─merge_1─┬─merge_2─┐
│       5 │       3 │
└─────────┴─────────┘
*/

INSERT INTO state_test_agg (id, count_state, uniq_state) 
VALUES 
  (1, 
   arrayReduce('countState', [toUInt32(1),2,3,4]), 
   arrayReduce('uniqState', ['a', 'b', 'b'])),
  (1, 
   arrayReduce('countState', [toUInt32(5),5]), 
   arrayReduce('uniqState', ['a', 'b', 'b','c']));
SELECT
    id,
    countMerge(count_state) AS c,
    uniqMerge(uniq_state) AS u
FROM state_test_agg
GROUP BY id

/*
┌─id─┬─c─┬─u─┐
│  1 │ 6 │ 3 │
└────┴───┴───┘
*/

推荐阅读