PostgreSQL bloom filter index 扩展 for bigint

背景
凡是支持HASH函数,以及相等operator的类型,都可以使用bloom filter index .

扩展方法见本文。

原文
https://obartunov.livejournal.com/201027.html

Bloom index by default works for int4 and text, but other types with hash function and equality operator could be supported.

Just use opclass interface, for example, for type bigint

create extension bloom;

postgres=# select * from pg_opclass where opcname='bigint_ops';

(0 rows)
创建bigint的bloom filter index支持。

CREATE OPERATOR CLASS bigint_ops
DEFAULT FOR TYPE bigint USING bloom AS
OPERATOR 1 = (bigint, bigint),
FUNCTION 1 hashint8(bigint);

postgres=# select * from pg_opclass where opcname='bigint_ops';

(1 row)
Now, you can build bloom index for bigint data type.

Data types, which could be supported by bloom index.

查询可以支持bloom filter的类型,HASH函数

凡是支持HASH函数,以及相等operator操作符的类型,都可以使用bloom filter index .

postgres=# select oid,* from pg_am;

oidamnameamhandleramtype
403btreebthandleri
405hashhashhandleri
783gistgisthandleri
2742ginginhandleri
4000spgistspghandleri
3580brinbrinhandleri
18204rumrumhandleri
136050bloomblhandleri

(8 rows)
查询可以支持bloom filter的类型,HASH函数

SELECT oc.opcintype::regtype, p.amproc FROM pg_opclass oc
JOIN pg_amproc p ON p.amprocfamily = oc.opcfamily
WHERE oc.opcmethod = 405 -- hash am
AND oc.opcdefault -- 默认proc for this am
-- https://www.postgresql.org/docs/devel/static/xindex.html Strategies number
AND p.amprocnum = 1
AND p.amproclefttype = oc.opcintype
AND p.amprocrighttype = oc.opcintype;

      opcintype          |     amproc       
characterhashbpchar
"char"hashchar
datehashint4
anyarrayhash_array
realhashfloat4
double precisionhashfloat8
inethashinet
smallinthashint2
integerhashint4
biginthashint8
intervalinterval_hash
macaddrhashmacaddr
namehashname
oidhashoid
oidvectorhashoidvector
texthashtext
time without time zonetime_hash
numerichash_numeric
timestamp with time zonetimestamp_hash
time with time zonetimetz_hash
timestamp without time zonetimestamp_hash
booleanhashchar
byteahashvarlena
xidhashint4
cidhashint4
abstimehashint4
reltimehashint4
aclitemhash_aclitem
uuiduuid_hash
pg_lsnpg_lsn_hash
macaddr8hashmacaddr8
anyenumhashenum
anyrangehash_range
jsonbjsonb_hash

(34 rows)
创建索引,例子

postgres=# create table test(id int, c1 int8, c2 int8, c3 int8);
CREATE TABLE
postgres=# create index idx_test_1 on test using bloom (c1,c2,c3);
CREATE INDEX

postgres=# set enable_seqscan=off;
SET
postgres=# explain select * from test where c1=1::int8 and c2=1::int8 and c3=1::int8;

                                   QUERY PLAN                                         

Bitmap Heap Scan on test (cost=15.73..15.75 rows=1 width=28)
Recheck Cond: ((c1 = '1'::bigint) AND (c2 = '1'::bigint) AND (c3 = '1'::bigint))
-> Bitmap Index Scan on idx_test_1 (cost=0.00..15.73 rows=1 width=0)

     Index Cond: ((c1 = '1'::bigint) AND (c2 = '1'::bigint) AND (c3 = '1'::bigint))  

(4 rows)

postgres=# set enable_seqscan =on;
SET
postgres=# explain select * from test where c1=1::int8 and c2=1::int8 and c3=1::int8;

                              QUERY PLAN                                    

Seq Scan on test (cost=0.00..9.85 rows=1 width=28)
Filter: ((c1 = '1'::bigint) AND (c2 = '1'::bigint) AND (c3 = '1'::bigint))
(2 rows)
参考
https://obartunov.livejournal.com/201027.html

https://www.postgresql.org/docs/devel/static/xindex.html
转自阿里云德哥

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值