注:大家觉得博客好的话,别忘了点赞收藏呀,本人每周都会更新关于人工智能和大数据相关的内容,内容多为原创,Python Java Scala SQL 代码,CV NLP 推荐系统等,Spark Flink Kafka Hbase Hive Flume等等~写的都是纯干货,各种顶会的论文解读,一起进步。
今天和大家分享一下Doris系列之高级功能-Rollup操作
#博学谷IT学习技术支持
接着上次的Doris系列继续和大家分享,上一次主要和大家分享了Doris的数据模型,今天和大家分享一下Doris系列之高级功能-Rollup操作。
这里一张普通的Duplicate数据模型的6列base表
# Doris数据模型-Duplicate 模型(冗余模型)
CREATE TABLE IF NOT EXISTS test_db.example_log
(
`timestamp` DATETIME NOT NULL COMMENT "日志时间",
`type` INT NOT NULL COMMENT "日志类型",
`error_code` INT COMMENT "错误码",
`error_msg` VARCHAR(1024) COMMENT "错误详细信息",
`op_id` BIGINT COMMENT "负责人id",
`op_time` DATETIME COMMENT "处理时间"
)
DUPLICATE KEY(`timestamp`, `type`)
DISTRIBUTED BY HASH(`timestamp`) BUCKETS 10;
insert into test_db.example_log values('2020-10-01 08:00:05',1,404,'not found page', 101, '2020-10-01 08:00:05');
insert into test_db.example_log values('2020-10-01 08:00:05',1,500,'service error', 201, '2021-10-01 08:00:05');
insert into test_db.example_log values('2020-10-01 08:00:05',2,404,'not found page', 101, '2020-10-01 08:00:06');
insert into test_db.example_log values('2020-10-01 08:00:06',2,404,'not found page', 101, '2020-10-01 08:00:07');
ALTER TABLE example_log ADD ROLLUP rollup1(type, error_code);
# 命中
explain select type, error_code from example_log;
# 不命中
explain select type, timestamp from example_log;
可以看到第一个查询语句是能命中Rollup,第二个则不能,原因是第二个查询语句查询的列超出了Rollup的范围。
所以验证了Rollup是手动创建,自动命中。
Rollup还有一个功能是可以调整索引。
因为建表时已经指定了列顺序,所以一个表只有一种前缀索引。这对于使用其他不能命中前缀索引的列作为条件进行的查询来说,效率上可能无法满足需求。因此,我们可以通过创建 ROLLUP 来人为的调整列顺序。
CREATE TABLE IF NOT EXISTS test_db.example_rollup_index
(
`user_id` LARGEINT NOT NULL COMMENT "用户id",
`age` SMALLINT COMMENT "用户年龄",
`message` varchar(100) COMMENT "信息",
`max_dwell_time` INT MAX DEFAULT "0" COMMENT "用户最大停留时间",
`min_dwell_time` INT MIN DEFAULT "99999" COMMENT "用户最小停留时间"
)
AGGREGATE KEY(`user_id`, `age`, `message`)
DISTRIBUTED BY HASH(`user_id`) BUCKETS 10;
-- 创建Rollup
ALTER TABLE test_db.example_rollup_index ADD ROLLUP rollup_city(age, user_id,message,max_dwell_time,min_dwell_time);
-- 通过命令查看完成状态
SHOW ALTER TABLE ROLLUP;
-- 查看是否命中ROLLUP
explain SELECT * FROM test_db.example_rollup_index where age=20 and message LIKE "%error%";
今天和大家分享一下Doris系列之高级功能-Rollup操作。