怎么使用PostgreSQL中的Bloom索引
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要讲解了"怎么使用PostgreSQL中的Bloom索引",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"怎么使用PostgreSQL中的Blo
千家信息网最后更新 2026年11月01日怎么使用PostgreSQL中的Bloom索引
这篇文章主要讲解了"怎么使用PostgreSQL中的Bloom索引",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"怎么使用PostgreSQL中的Bloom索引"吧!
简介
Bloom Index源于Bloom filter(布隆过滤器),布隆过滤器用于在使用少量的空间的情况下可以很快速的判定某个值是否在集合中,其缺点是存在假阳性False Positives,因此需要Recheck来判断该值是否在集合中,但布隆过滤器不存在假阴性,也就是说,对于某个值如果过滤器返回不存在,那就是不存在.
结构
其结构如下图所示:

第一个page为metadata,然后每一行都会有一个bit array(signature)和TID与其对应.
示例
创建数据表,插入数据
testdb=# drop table if exists t_bloom;DROP TABLEtestdb=# CREATE TABLE t_bloom (id int, dept int, id2 int, id3 int, id4 int, id5 int,id6 int,id7 int,details text, zipcode int);CREATE TABLEtestdb=# testdb=# INSERT INTO t_bloom testdb-# SELECT (random() * 1000000)::int, (random() * 1000000)::int,testdb-# (random() * 1000000)::int,(random() * 1000000)::int,(random() * 1000000)::int,(random() * 1000000)::int, testdb-# (random() * 1000000)::int,(random() * 1000000)::int,md5(g::text), floor(random()* (20000-9999 + 1) + 9999) testdb-# from generate_series(1,16*1024*1024) g;INSERT 0 16777216testdb=# testdb=# analyze t_bloom;ANALYZEtestdb=# testdb=# select pg_size_pretty(pg_table_size('t_bloom')); pg_size_pretty ---------------- 1619 MB(1 row)创建Btree索引
testdb=# testdb=# create index idx_t_bloom_btree on t_bloom using btree(id,dept,id2,id3,id4,id5,id6,id7,zipcode);CREATE INDEXtestdb=# \di+ idx_t_bloom_btree List of relations Schema | Name | Type | Owner | Table | Size | Description --------+-------------------+-------+-------+---------+--------+------------- public | idx_t_bloom_btree | index | pg12 | t_bloom | 940 MB | (1 row)
执行查询
testdb=# EXPLAIN ANALYZE select * from t_bloom where id4 = 305294 and zipcode = 13266; QUERY PLAN --------------------------------------------------------------------------------------------------------- Index Scan using idx_t_bloom_btree on t_bloom (cost=0.56..648832.73 rows=1 width=69) (actual time=2648.215..2648.215 rows=0 loops=1) Index Cond: ((id4 = 305294) AND (zipcode = 13266)) Planning Time: 3.244 ms Execution Time: 2659.804 ms(4 rows)testdb=# EXPLAIN ANALYZE select * from t_bloom where id5 = 241326 and id6 = 354198; QUERY PLAN --------------------------------------------------------------------------------------------------------- Index Scan using idx_t_bloom_btree on t_bloom (cost=0.56..648832.73 rows=1 width=69) (actual time=2365.533..2365.533 rows=0 loops=1) Index Cond: ((id5 = 241326) AND (id6 = 354198)) Planning Time: 1.918 ms Execution Time: 2365.629 ms(4 rows)
创建Bloom索引
testdb=# create extension bloom;CREATE EXTENSIONtestdb=# CREATE INDEX idx_t_bloom_bloom ON t_bloom USING bloom(id, dept, id2, id3, id4, id5, id6, id7, zipcode) testdb-# WITH (length=64, col1=4, col2=4, col3=4, col4=4, col5=4, col6=4, col7=4, col8=4, col9=4);CREATE INDEXtestdb=# \di+ idx_t_bloom_bloom List of relations Schema | Name | Type | Owner | Table | Size | Description --------+-------------------+-------+-------+---------+--------+------------- public | idx_t_bloom_bloom | index | pg12 | t_bloom | 225 MB | (1 row)
执行查询
testdb=# EXPLAIN ANALYZE select * from t_bloom where id4 = 305294 and zipcode = 13266; QUERY PLAN ------------------------------------------------------------------------------------------------- Bitmap Heap Scan on t_bloom (cost=283084.16..283088.18 rows=1 width=69) (actual time=998.727..998.727 rows=0 loops=1) Recheck Cond: ((id4 = 305294) AND (zipcode = 13266)) Rows Removed by Index Recheck: 12597 Heap Blocks: exact=12235 -> Bitmap Index Scan on idx_t_bloom_bloom (cost=0.00..283084.16 rows=1 width=0) (actual time=234.893..234.893 rows=12597 loops=1) Index Cond: ((id4 = 305294) AND (zipcode = 13266)) Planning Time: 31.482 ms Execution Time: 998.975 ms(8 rows)testdb=# EXPLAIN ANALYZE select * from t_bloom where id5 = 241326 and id6 = 354198; QUERY PLAN ------------------------------------------------------------------------------------------------- Bitmap Heap Scan on t_bloom (cost=283084.16..283088.18 rows=1 width=69) (actual time=1019.621..1019.621 rows=0 loops=1) Recheck Cond: ((id5 = 241326) AND (id6 = 354198)) Rows Removed by Index Recheck: 13033 Heap Blocks: exact=12633 -> Bitmap Index Scan on idx_t_bloom_bloom (cost=0.00..283084.16 rows=1 width=0) (actual time=204.873..204.873 rows=13033 loops=1) Index Cond: ((id5 = 241326) AND (id6 = 354198)) Planning Time: 0.441 ms Execution Time: 1019.811 ms(8 rows)
从执行结果来看,在查询条件中没有非前导列(上例中为id1)的情况下多列任意组合查询,bloom index会优于btree index.
感谢各位的阅读,以上就是"怎么使用PostgreSQL中的Bloom索引"的内容了,经过本文的学习后,相信大家对怎么使用PostgreSQL中的Bloom索引这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
索引
过滤器
查询
情况
布隆
学习
内容
就是
数据
结构
一行
也就是
也就是说
前导
思路
数据表
文章
更多
条件
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网与科技
广州淘业互联网络科技
重生之互联网科技帝国
互联网科技保险
佰商互联网科技有限公司
隆华科技工业互联网
深圳前海聚择互联网科技有限公司
湖南诚度互联网科技有限公司
坤平互联网科技
科技大学互联网
黑云互联网科技有限公司
21世纪互联网科技成就
上海创道互联网络科技有限公司
山东大美朱楼互联网科技有限公司
互联网科技手机排行榜
互联网科技和医药行业
科技互联网公司两年变化
深圳互联网科技园地址
六子台科技互联网展会
如何强化互联网金融科技
科技平台互联网
宜安科技 互联网金融
镁科技互联网资讯平台
互联网巨头投资的硬核科技
浙江互联网科技研学营
乌鲁木齐新华智原互联网科技学校
科技赋能视角下互联网保险发展
工业互联网科技创业
北京天晋互联网科技有限公司
科技信息时代互联网发展