PostgreSQL 13新特性:增強GIN索引邏輯推理能力

PostgreSQL13中對gin索引的邏輯推理能力進行了加強:
在這裏插入圖片描述

例子:
分別在pg12和pg13的環境中創建測試表:

bill@bill=>create table tt1(id int,info text);
CREATE TABLE

bill@bill=>insert into tt1 select generate_series(1,10000),md5(random()::text);
INSERT 0 10000

bill@bill=>create index idx_tt1 on tt1 using gin(info gin_trgm_ops);
CREATE INDEX

bill@bill=>vacuum ANALYZE tt1;
VACUUM

pg12查詢:
觀察下面這個查詢中and前後的兩個條件,我們可以發現滿足前面一個條件必然滿足第二個條件。在pg12中需要10ms。

bill@bill=>explain analyze select * from tt1 where info like '%7d8%' and info like '%d%';    
                                                     QUERY PLAN                                                      
---------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on tt1  (cost=12023.71..12025.02 rows=1 width=37) (actual time=9.940..10.032 rows=68 loops=1)
   Recheck Cond: ((info ~~ '%7d8%'::text) AND (info ~~ '%d%'::text))
   Heap Blocks: exact=43
   ->  Bitmap Index Scan on idx_tt1  (cost=0.00..12023.71 rows=1 width=0) (actual time=9.919..9.919 rows=68 loops=1)
         Index Cond: ((info ~~ '%7d8%'::text) AND (info ~~ '%d%'::text))
 Planning Time: 0.144 ms
 Execution Time: 10.073 ms
(7 rows)

pg13查詢:
同樣的查詢在pg13中只需要0.12ms,可見性能提升還是十分明顯的。

bill=# explain analyze select * from tt1 where info like '%eb17%' and info like '%b%';
                                                   QUERY PLAN                                                    
-----------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on tt1  (cost=6.51..7.82 rows=1 width=37) (actual time=0.073..0.094 rows=9 loops=1)
   Recheck Cond: ((info ~~ '%eb17%'::text) AND (info ~~ '%b%'::text))
   Rows Removed by Index Recheck: 1
   Heap Blocks: exact=9
   ->  Bitmap Index Scan on idx_tt1  (cost=0.00..6.51 rows=1 width=0) (actual time=0.061..0.061 rows=10 loops=1)
         Index Cond: ((info ~~ '%eb17%'::text) AND (info ~~ '%b%'::text))
 Planning Time: 0.121 ms
 Execution Time: 0.122 ms
(8 rows)

參考鏈接:
https://www.postgresql.org/docs/13/release-13.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章