目录
- 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索引属于一种基于位图的快速数据结构,旨在提升查询效率。
1.2 Bitmap索引使用的注意事项
使用限制:
- 目前仅支持为bitmap类型的列创建索引。
- 该索引只能在单列上建立。
- 在Duplicate和Uniq数据模型中,所有列均可应用Bitmap索引;而在Aggregate模型中,仅Key列支持。
- Bitmap索引仅在Segment V2存储格式下生效。创建索引时,表的存储格式将默认转换为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%准确,但能告知调用者元素存在或不存在于集合中。
2.2 BloomFilter原理
其核心由一个超长的二进制位数组和一系列哈希函数组成。初始时位数组全为0,当给定元素时,经哈希函数计算映射出的多个位置被置为1。查询时,若任一哈希映射位置为0,则该元素不在集合中。
下图展示了一个m=18, k=3(m为Bit数组大小,k为Hash函数个数)的Bloom Filter示例。集合中的x、y、z三个元素通过3个不同哈希函数散列到位数组中。查询元素w时,因有一个比特为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几乎包含所有取值,索引将失去意义。
2.5 Doris BloomFilter使用注意事项
- 不支持对Tinyint、Float、Double类型的列建立Bloom Filter索引。
- Bloom Filter索引仅对In和等号(=)过滤查询有加速效果。
- 可通过查询的Profile信息查看是否命中了Bloom Filter索引。
到此这篇关于Apache Doris的Bitmap索引和BloomFilter索引使用的文章就介绍到这了,更多相关Apache Doris索引内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!