【发布时间】:2017-03-24 20:14:34
【问题描述】:
尽管所有文档都这么说,我发现 GIN 索引比 pg_trgm 相关搜索的 GIST 索引慢得多。这是在一个包含 2500 万行的表中,文本字段相对较短(平均长度为 21 个字符)。大多数文本行是“123 Main st, City”形式的地址。
GIST 索引搜索大约需要 4 秒
select suggestion from search_suggestions where suggestion % 'seattle';
但使用EXPLAIN ANALYZE 运行时,GIN 需要 90 秒,结果如下:
Bitmap Heap Scan on search_suggestions (cost=330.09..73514.15 rows=25043 width=22) (actual time=671.606..86318.553 rows=40482 loops=1)
Recheck Cond: ((suggestion)::text % 'seattle'::text)
Rows Removed by Index Recheck: 23214341
Heap Blocks: exact=7625 lossy=223807
-> Bitmap Index Scan on tri_suggestions_idx (cost=0.00..323.83 rows=25043 width=0) (actual time=669.841..669.841 rows=1358175 loops=1)
Index Cond: ((suggestion)::text % 'seattle'::text)
Planning time: 1.420 ms
Execution time: 86327.246 ms
请注意,索引选择了超过一百万行,尽管实际上只有 40k 行匹配。任何想法为什么这表现如此糟糕?这是在 PostgreSQL 9.4 上。
【问题讨论】:
-
缺少有关性能问题的一些信息。表定义、表和索引的总大小。 See instructions here.
标签: sql postgresql indexing pattern-matching postgresql-performance