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