【已解决】物化视图中具体怎么使用 count

Viewed 79

drois版本:
doris-2.1.2-rc04-b130df2488
建表:

create table demo1(
    email varchar(100) not null,
    country varchar(20) not null,
    ip varchar(60) REPLACE,
    last_access DateTime REPLACE,
    fix_1 bigint REPLACE default "1",
    flag_1 bigint SUM default "1"
)
AGGREGATE KEY(`email`,`country`)
DISTRIBUTED BY HASH(`email`) BUCKETS 32
PROPERTIES (
    "replication_allocation" = "tag.location.default: 3",
    "compression"="zstd"
);

因为没有一个用户表来维护一个自增长的id,
如何建一个物化视图来加速查询:每一个城市去重后的用户。

select country, count(1) from demo1 group by country;

创建物化视图的语句如下:

create materialized view demo1_cnt as select country,count(1) from demo1 group by country;

错误信息如下:

ERROR 1105 (HY000): errCode = 2, detailMessage = The mvItem[mva_SUM__CASE WHEN 1 IS NULL THEN 0 ELSE 1 END] require slot because it is value column

将 count(1) 换成 count(fix_1) 或者 换成 sum(fix_1)都会报错。

我看文档:
https://doris.apache.org/zh-CN/docs/query-acceleration/materialized-view
支持count
支持聚合函数

目前物化视图创建语句支持的聚合函数有:

SUM, MIN, MAX (Version 0.12)
COUNT, BITMAP_UNION, HLL_UNION (Version 0.13)
通用聚合 (Version 2.0)

请问这个count是应该怎么正确打开呢?

1 Answers

报这个错的原因是这个base表是agg表,如果你在上面做count(1),语义其实不太明确,到底是预聚合前的行数,还是预聚合后的行数。
如果是dup表,是可以直接建含有count(1)的mv的。在agg表上的话,如果你想要的是原始的(预聚合前的)行数,那可以在基表添加一列默认值为1的sum列。
如果是想要预聚合后的行数,可以在基表添加一列bitmap类型的bitmap_union列,并在导入数据的时候给这列导入bitmap_hash(email),此时可以建立select country, bitmap_union_count(bitmap_email) from demo1 group by country;的物化视图。