Doris数据库使用
1、表结构设置
【1】建表时指定副本数量:relication_num
【2】排序键
明细模型:DUPLICATE KEY(site_id, city_code)
聚合模型:AGGREGATE KEY(site_id, city_code)
更新模型:UNIQUE KEY(site_id, city_code)
BloomFilter索引:PROPERTIES ( "bloom_filter_columns"="k1,k2,k3" )
【3】分桶配置
DISTRIBUTED BY HASH(site_id) BUCKETS 10
【4】查看Docker容器IP
docker inspect --format='{{.NetworkSettings.IPAddress}}' doris-be1
【5】表配置
PROPERTIES (
"replication_num" = "1", //副本数
"colocate_with" = "group1",
"in_memory" = "false",
"storage_format" = "DEFAULT"
);
【6】Olap表
ENGINE=OLAP
2、基础操作
【1】建表
明细模型 CREATE TABLE site_access_duplicate ( site_id INT DEFAULT '10', city_code SMALLINT, user_name VARCHAR(32) DEFAULT '', pv BIGINT DEFAULT '0' ) DUPLICATE KEY(site_id, city_code) DISTRIBUTED BY HASH(site_id) BUCKETS 10; 聚合模型 CREATE TABLE site_access_aggregate ( site_id INT DEFAULT '10', city_code SMALLINT, pv BIGINT SUM DEFAULT '0' ) AGGREGATE KEY(site_id, city_code) DISTRIBUTED BY HASH(site_id) BUCKETS 10; 更新模型 CREATE TABLE site_access_unique ( site_id INT DEFAULT '10', city_code SMALLINT, user_name VARCHAR(32) DEFAULT '', pv BIGINT DEFAULT '0' ) UNIQUE KEY(site_id, city_code) DISTRIBUTED BY HASH(site_id) BUCKETS 10;
【2】明细模型插入测试数据
INSERT INTO site_access_duplicate VALUES(10010,10,"wangshida",1), (10011,10,"xiaohong",2), (10012,10,"xiaoming",15)
【3】更新数据(不支持),通过更新模型插入数据方式实现
【4】删除数据(支持,比较慢)
delete from site_access_duplicate where site_id=10022
3、分析Sql
site_access_duplicate 明细模型
site_access_aggregate 聚合模型
site_access_unique 更新模型
【1】限制两个排序键
explain select * from site_access_duplicate where site_id = 10010 and city_code = 10;
0:OlapScanNode
TABLE: site_access_duplicate
PREAGGREGATION: ON
PREDICATES: `site_id` = 10010, `city_code` = 10
partitions=1/1
rollup: site_access_duplicate
tabletRatio=1/10
tabletList=11012
cardinality=4
avgRowSize=144.75
numNodes=3
tuple ids: 0
【2】只限制第一个排序键site_id
explain select * from site_access_duplicate where site_id = 10010
0:OlapScanNode
TABLE: site_access_duplicate
PREAGGREGATION: ON
PREDICATES: `site_id` = 10010
partitions=1/1
rollup: site_access_duplicate
tabletRatio=1/10
tabletList=11012
cardinality=4
avgRowSize=144.75
numNodes=3
tuple ids: 0
【3】只限制第二个排序键city_code
explain select * from site_access_duplicate where city_code = 2;
0:OlapScanNode
TABLE: site_access_duplicate
PREAGGREGATION: ON
PREDICATES: `city_code` = 10
partitions=1/1
rollup: site_access_duplicate
tabletRatio=10/10
tabletList=11004,11008,11012,11016,11020,11024,11028,11032,11036,11040
cardinality=11
avgRowSize=262.45456
numNodes=3
tuple ids: 0
4、物化视图,对于走不了shortkey的可以建物化视图解决
基础表 CREATE TABLE site_access_duplicate ( site_id INT DEFAULT '10', city_code SMALLINT, user_name VARCHAR(32) DEFAULT '', pv BIGINT DEFAULT '0' ) DUPLICATE KEY(site_id, city_code) DISTRIBUTED BY HASH(site_id) BUCKETS 10;
【1】创建物化视图
【注】报错errCode = 2, detailMessage = The materialized view is coming soon
对明细模型创建物化视图,需要在Fe配置文件中新增
enable_materialized_view=true
CREATE MATERIALIZED VIEW `site_access_duplicate_pv_view` AS SELECT city_code, SUM(pv) AS sum_pv FROM site_access_duplicate GROUP BY city_code ORDER BY city_code
【2】查看数据库下物化视图
SHOW ALTER TABLE ROLLUP FROM test;
如果State为"FINISHED"说明基表到物化视图已经创建完成
【3】查看表物化视图结果
desc site_access_duplicate all;
【4】分析查询Sql是否走物化视图
PREAGGREGATION: ON 和 rollup: site_access_duplicate_pv_view说明使用物化视图
![0](https://img2020.cnblogs.com/blog/2350829/202107/2350829-20210721115058259-608118404.jpg)
![0](https://img2020.cnblogs.com/blog/2350829/202107/2350829-20210721115058447-477065998.jpg)
【5】删除物化视图
DROP MATERIALIZED VIEW IF EXISTS site_access_duplicate_pv_view from site_access_duplicate;
【6】智能路由规则
选择包含所有查询列的MV表
按照过滤和排序的Column筛选最符合的MV表
按照Join的Column筛选最符合的MV表
行数最小的MV表
列数最小的MV表
注意点:
(1)必须是单个表聚合
(2)支持以下聚合函数
COUNT
MAX
MIN
SUM
PERCENTILE_APPROX
HLL_UNION
(3)RollUp表的模型必须和Base表保持一致(聚合表的RollUp表是聚合模型,明细表的RollUp表是明细模型)
(4)Delete 操作时,如果 Where 条件中的某个 Key 列在某个 RollUp表中不存在,则不允许进行 Delete。
例删除username,该字段在物化视图不存在,则不允许删除。要不物化视图不更新数据可能对不上,解决办法删物化视图
5、bitmap索引
参考文章:https://zhuanlan.zhihu.com/p/54783053
【1】创建索引
CREATE INDEX idx_site_id ON site_access_duplicate (city_code)
USING BITMAP COMMENT '城市索引';
【2】查看表配置的索引
SHOW INDEX FROM site_access_duplicate;
【3】删除索引
DROP INDEX idx_site_id ON site_access_duplicate;
注意事项
(1)列都可以建Bitmap 索引;对于聚合模型,只有Key列可以建Bitmap 索引
(2)Bitmap索引, 应该在取值为枚举型, 取值大量重复, 较低基数
(3)不支持对Float、Double、Decimal 类型的列建Bitmap 索引
6、Bloomfilter索引
【1】添加索引,建表时指定
PROPERTIES ( "bloom_filter_columns"="city_code,pv" )
【2】查看索引
SHOW CREATE TABLE site_access_duplicate;
【3】删除索引
ALTER TABLE site_access_duplicate SET ("bloom_filter_columns" = "");
【4】修改索引
ALTER TABLE site_access_duplicate SET ("bloom_filter_columns" = "city_code,pv");
【5】打开Fe的Report开关 set is_report_success=true;
![0](https://img2020.cnblogs.com/blog/2350829/202107/2350829-20210721115057867-1884376269.jpg)
验证Sql
ALTER TABLE site_access_duplicate SET ("bloom_filter_columns" = "user_name");
select * from site_access_duplicate where user_name = 'xiaoming';
![0](https://img2020.cnblogs.com/blog/2350829/202107/2350829-20210721115058170-2047440845.jpg)
注意事项
(1)不支持对Tinyint、Float、Double 类型的列建Bloom Filter索引
(2)Bloom Filter索引只对in和=过滤查询有加速效果
(3)如果要查看某个查询是否命中了Bloom Filter索引,可以通过查询的Profile信息查看
7、集群管理
查看Fe集群状态 show frontends \G
查看Be集群状态 show backends \G