bloom索引时PG9.6新增的索引类型,支持任意列的组合查询。下面将在AntDB中简单地试用。node
建立表,插入数据。git
create table tt (github
i1 int4,sql
i2 int4,dom
i3 int4,oop
i4 int4,post
i5 int4,postgresql
i6 int4索引
);源码
插入数据:
insert into tt (i1,i2,i3,i4,i5,i6)
select
random()*1000000,
random()*1000000,
random()*1000000,
random()*1000000,
random()*1000000,
random()*1000000
from generate_series(1,2000000);
比较:
当没有索引时,根据 i2, 耗时601.534 ms。
根据i2和i3查询,耗时592.506 ms。
postgres=# explain (verbose, analyze) select * from tt where i2 = 345678;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------
Data Node Scan on "__REMOTE_FQS_QUERY__" (cost=0.00..0.00 rows=0 width=0) (actual time=300.605..601.462 rows=2 loops=1)
Output: tt.i1, tt.i2, tt.i3, tt.i4, tt.i5, tt.i6
Primary node/s: dm0
Node/s: dm0, dm1
Remote query: SELECT i1, i2, i3, i4, i5, i6 FROM public.tt tt WHERE (i2 = 345678)
Planning time: 0.200 ms
Execution time: 601.534 ms
(7 rows)
postgres=# explain (verbose, analyze) select * from tt where i2 = 345678 and i3 = 45678;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------
Data Node Scan on "__REMOTE_FQS_QUERY__" (cost=0.00..0.00 rows=0 width=0) (actual time=592.444..592.444 rows=0 loops=1)
Output: tt.i1, tt.i2, tt.i3, tt.i4, tt.i5, tt.i6
Primary node/s: dm0
Node/s: dm0, dm1
Remote query: SELECT i1, i2, i3, i4, i5, i6 FROM public.tt tt WHERE ((i2 = 345678) AND (i3 = 45678))
Planning time: 0.206 ms
Execution time: 592.506 ms
(7 rows)
当建立bloom索引后,根据i2查询耗时121.513 ms。
根据i2和i3,查询耗时70.326 ms。
create extension bloom;
create index bloom_id on tt using bloom (i1,i2,i3,i4,i5,i6);
postgres=# explain (verbose, analyze) select * from tt where i2 = 345678 and i3 = 45678;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Data Node Scan on "__REMOTE_FQS_QUERY__" (cost=0.00..0.00 rows=0 width=0) (actual time=70.252..70.252 rows=0 loops=1)
Output: tt.i1, tt.i2, tt.i3, tt.i4, tt.i5, tt.i6
Primary node/s: dm0
Node/s: dm0, dm1
Remote query: SELECT i1, i2, i3, i4, i5, i6 FROM public.tt tt WHERE ((i2 = 345678) AND (i3 = 45678))
Planning time: 0.802 ms
Execution time: 70.326 ms
(7 rows)
postgres=# explain (verbose, analyze) select * from tt where i2 = 345678;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
Data Node Scan on "__REMOTE_FQS_QUERY__" (cost=0.00..0.00 rows=0 width=0) (actual time=59.891..121.443 rows=2 loops=1)
Output: tt.i1, tt.i2, tt.i3, tt.i4, tt.i5, tt.i6
Primary node/s: dm0
Node/s: dm0, dm1
Remote query: SELECT i1, i2, i3, i4, i5, i6 FROM public.tt tt WHERE (i2 = 345678)
Planning time: 0.203 ms
Execution time: 121.513 ms
(7 rows)
删除bloom index,建立btree类型的index,根据i2和i3查询耗时3.594ms
postgres=# drop index bloom_id;
DROP INDEX
postgres=# create index btree_id on tt using btree(i2, i3);
CREATE INDEX
postgres=# explain (verbose, analyze) select * from tt where i2 = 345678 and i3 = 45678;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
Data Node Scan on "__REMOTE_FQS_QUERY__" (cost=0.00..0.00 rows=0 width=0) (actual time=3.531..3.531 rows=0 loops=1)
Output: tt.i1, tt.i2, tt.i3, tt.i4, tt.i5, tt.i6
Primary node/s: dm0
Node/s: dm0, dm1
Remote query: SELECT i1, i2, i3, i4, i5, i6 FROM public.tt tt WHERE ((i2 = 345678) AND (i3 = 45678))
Planning time: 0.214 ms
Execution time: 3.594 ms
(7 rows)
对于多列索引,btree类型的索引效率比bloom要高许多,可是须要建立不一样的多列组合索引,索引过多,维护起来不方便。
当表中列较多时,能够建立bloom类型的索引,能够适当提升查询效率,虽然效率不是最高的,可是免去了在各个列上建立索引的麻烦。
参考:
QQ交流群:496464280
源码地址:http://github.com/ADBSQL
欢迎广大postgresql爱好者使用和交流。