Postgresql 什么';杜松子酒指数有问题,可以';不能避免序列扫描吗?
我创建了一个这样的表Postgresql 什么';杜松子酒指数有问题,可以';不能避免序列扫描吗?,postgresql,full-text-search,gwt-gin,Postgresql,Full Text Search,Gwt Gin,我创建了一个这样的表 create table mytable(hash char(40), title varchar(500)); create index name_fts on mytable using gin(to_tsvector('english', 'title')); CREATE UNIQUE INDEX md5_uniq_idx ON mytable(hash); 当我询问标题时 test=# explain analyze select * from mytable
create table mytable(hash char(40), title varchar(500));
create index name_fts on mytable using gin(to_tsvector('english', 'title'));
CREATE UNIQUE INDEX md5_uniq_idx ON mytable(hash);
当我询问标题时
test=# explain analyze select * from mytable where to_tsvector('english', title) @@ 'abc | def'::tsquery limit 10;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..277.35 rows=10 width=83) (actual time=0.111..75.549 rows=10 loops=1)
-> Seq Scan on mytable (cost=0.00..381187.45 rows=13744 width=83) (actual time=0.110..75.546 rows=10 loops=1)
Filter: (to_tsvector('english'::regconfig, (title)::text) @@ '''abc'' | ''def'''::tsquery)
Rows Removed by Filter: 10221
Planning time: 0.176 ms
Execution time: 75.564 ms
(6 rows)
未使用索引。有什么想法吗?我有10万行。索引定义中有一个输入错误,应该是
ON mytable USING gin (to_tsvector('english', title))
而不是
ON mytable USING gin (to_tsvector('english', 'title'))
按照您编写它的方式,它是一个常量,而不是索引的字段,这样的索引对于您执行的搜索来说确实是无用的
要查看是否可以使用索引,可以执行
SET enable_seqscan=off;
然后再次运行查询。如果仍然没有使用索引,则该索引可能无法使用
除此之外,我对你的执行计划感到奇怪。PostgreSQL估计,对
mytable
的顺序扫描将返回13744行,而不是您所说的1000万行。您是否禁用了autovacuum,或者是否有其他原因会导致表统计信息如此不准确?全文搜索不是这样工作的。@Phill您能详细说明一下吗?您的查询在执行时会转换值,这会很慢,它永远不会使用索引。您应该将tsvector存储在表中的一个单独的列中。@Phill,对不起,这是胡说八道。PostgreSQL可以很好地在表达式上使用索引。链接到它与tsvector一起使用,而不是性能问题@LaurenzAlbeIt在排印错误修复后就起作用了,limit 10
也阻止了索引的使用。索引可以与limit子句一起使用吗?@unniklimit
不会阻止索引的使用。