AntDB中bloom索引試用

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愛好者使用和交流。

相關文章
相關標籤/搜索