【发布时间】:2019-08-28 00:56:43
【问题描述】:
我在玩pg_trgm 扩展,我有点困惑。这是会议:
postgres=# create table t(i int, x text);
CREATE TABLE
postgres=# insert into t select i, random()::text from generate_series(1,50000000) as i;
INSERT 0 50000000
postgres=# explain analyze select * from t where x ilike '%666666%';
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------
Gather (cost=1000.00..531870.29 rows=12954 width=36) (actual time=131.436..11408.176 rows=432 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Parallel Seq Scan on t (cost=0.00..529574.89 rows=5398 width=36) (actual time=108.771..11304.946 rows=144 loops=3)
Filter: (x ~~* '%666666%'::text)
Rows Removed by Filter: 16666523
Planning Time: 0.121 ms
Execution Time: 11408.279 ms
(8 rows)
postgres=# explain analyze select * from t where x ilike '%666666%';
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------
Gather (cost=1000.00..580654.94 rows=5000 width=21) (actual time=124.986..11070.983 rows=432 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Parallel Seq Scan on t (cost=0.00..579154.94 rows=2083 width=21) (actual time=72.207..11010.876 rows=144 loops=3)
Filter: (x ~~* '%666666%'::text)
Rows Removed by Filter: 16666523
Planning Time: 0.283 ms
Execution Time: 11071.065 ms
(8 rows)
postgres=# create index i on t using gin (x gin_trgm_ops);
CREATE INDEX
postgres=# analyze t;
ANALYZE
postgres=# explain analyze select * from t where x ilike '%666666%';
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on t (cost=54.75..18107.93 rows=5000 width=21) (actual time=116.114..26995.773 rows=432 loops=1)
Recheck Cond: (x ~~* '%666666%'::text)
Rows Removed by Index Recheck: 36257910
Heap Blocks: exact=39064 lossy=230594
-> Bitmap Index Scan on i (cost=0.00..53.50 rows=5000 width=0) (actual time=75.363..75.363 rows=592216 loops=1)
Index Cond: (x ~~* '%666666%'::text)
Planning Time: 0.389 ms
Execution Time: 26996.429 ms
(8 rows)
postgres=# explain analyze select * from t where x ilike '%666666%';
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on t (cost=54.75..18107.93 rows=5000 width=21) (actual time=128.859..29231.765 rows=432 loops=1)
Recheck Cond: (x ~~* '%666666%'::text)
Rows Removed by Index Recheck: 36257910
Heap Blocks: exact=39064 lossy=230594
-> Bitmap Index Scan on i (cost=0.00..53.50 rows=5000 width=0) (actual time=79.147..79.147 rows=592216 loops=1)
Index Cond: (x ~~* '%666666%'::text)
Planning Time: 0.252 ms
Execution Time: 29231.945 ms
(8 rows)
如您所见,没有索引的查询比使用索引快两倍以上。就目前而言,有默认的 PostgreSQL 设置(共享缓冲区、工作内存等)
我错过了什么?
PS:x86_64-pc-linux-gnu 上的 PostgreSQL 11.5 (Ubuntu 11.5-1.pgdg18.04+1),由 gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0 编译,64 位
PPS:使用gist 索引会更慢。
【问题讨论】:
标签: postgresql