ApacheDoris的Bitmap索引和BloomFilter索引使用及注意事项

Nora ·
更新时间:2024-09-21
· 98 次阅读

目录

1. Bitmap索引的使用

1.1 Bitmap索引介绍

1.2 Bitmap索引使用的注意事项

1.3 Bitmap索引的使用

2. BloomFilter索引

2.1 BloomFilter索引介绍

2.2 BloomFilter原理

2.3 BloomFilter索引的使用

2.4 Doris BloomFilter使用场景

2.5 Doris BloomFilter使用注意事项

1. Bitmap索引的使用 1.1 Bitmap索引介绍

bitmap index是一种位图索引,是一种快速数据结构,能够加快查询速度

1.2 Bitmap索引使用的注意事项

使用限制

目前索引仅支持bitmap类型的索引

bitmap索引仅在单列上创建

bitmap索引能够应用在Duplicate、Uniq数据模型的所有列和Aggregate模型的key列上

bitmap索引仅在Segment V2储存格式下生效。当创建index时,表的存储格式将默认转换为V2格式

bitmap索引支持的数据类型:

TINYINT

SMALLINT

INT

UNSIGNEDINT

BIGINT

CHAR

VARCHAR

DATE

DATETIME

LARGEINT

DECIMAL

BOOL

1.3 Bitmap索引的使用

创建索引

mysql> create index if not exists click_bitmap_index on test_db.click (user_id) using bitmap comment 'bitmap index test'; Query OK, 0 rows affected (0.05 sec) mysql>

查看索引

mysql> show index from test_db.click; +-------------------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-------------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------------------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-------------------+ | default_cluster:test_db.click | | click_bitmap_index | | user_id | | | | | | BITMAP | bitmap index test | +-------------------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-------------------+ 1 row in set (0.04 sec) mysql>

删除索引

mysql> drop index if exists click_bitmap_index on test_db.click; Query OK, 0 rows affected (0.03 sec) mysql> 2. BloomFilter索引 2.1 BloomFilter索引介绍

是一种多哈希函数映射的快速查找算法,本质上是一种位图结构。通常应用在一些需要快速判断某个元素是否属于集合,但是并不严格要求100%正确的场合,因为BloomFilter会告诉调用者一个元素存在或不存在一个集合。但存在不一定准确

2.2 BloomFilter原理

实际上是由一个超长的二进制位数组和一系列的哈希函数组成。二进制位数组初始全部为0,当给定一个元素时,这个元素会被一系列哈希函数计算映射出一系列的值,所有的值在位数组的偏移量处置为1。而对于一个待查询的元素,也会用相同的哈希函数映射到位数组上,只要有一个哈希函数映射没有命中之前的元素的偏移量,则不存在于集合中

下图所示出一个m=18, k=3(m是该Bit数组的大小,k是Hash函数的个数)的Bloom Filter示例。集合中的x、y、z三个元素通过3个不同的哈希函数散列到位数组中。当查询元素w时,通过Hash函数计算之后因为有一个比特为0,因此w不在该集合中

BloomFilter索引也是以Block为粒度创建的。每个Block中,指定列的值作为一个集合生成一个BloomFilter索引条目,用于在查询是快速过滤不满足条件的数据

2.3 BloomFilter索引的使用

创建表使用BloomFilter索引

mysql> create table order_tb( -> user_id bigint, -> order_date date, -> city varchar(32), -> url varchar(512) -> ) distributed by hash(user_id, city) buckets 8 -> properties( -> 'bloom_filter_columns'='user_id,order_date' -> ); Query OK, 0 rows affected (0.07 sec) mysql>

查看BloomFilter索引

mysql> show create table order_tb;

删除BloomFilter索引

mysql> alter table test_db.order_tb set ('bloom_filter_columns' = ''); Query OK, 0 rows affected (0.05 sec) mysql>

修改BloomFilter索引

mysql> alter table test_db.order_tb set ('bloom_filter_columns' = 'user_id,city'); Query OK, 0 rows affected (0.05 sec) mysql> 2.4 Doris BloomFilter使用场景

首先BloomFilter适用于非前缀过滤

查询会根据该列高频过滤,而且查询条件大多是in和=过滤

不同于Bitmap, BloomFilter适用于高基数列。比如UserID。因为如果创建在低基数的列上,比如”性别“列,则每个Block几乎都会包含所有取值,导致BloomFilter索引失去意义

2.5 Doris BloomFilter使用注意事项

不支持对Tinyint、Float、Double 类型的列建Bloom Filter索引

Bloom Filter索引只对in和=过滤查询有加速效果

如果要查看某个查询是否命中了Bloom Filter索引,可以通过查询的Profile信息查看

到此这篇关于Apache Doris的Bitmap索引和BloomFilter索引使用的文章就介绍到这了,更多相关Apache Doris索引内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



bitmap

需要 登录 后方可回复, 如果你还没有账号请 注册新账号