【问题标题】:postgres full text search query on large table not using index不使用索引的大表上的postgres全文搜索查询
【发布时间】:2017-10-28 07:20:13
【问题描述】:

全文搜索查询

SELECT id
 FROM (
   SELECT id, 
   ts_rank_cd(setweight(to_tsvector('english', lower(title)), 'A') || 
   setweight(to_tsvector('english', lower(body)), 'B'), 
   plainto_tsquery('english', 'query')) AS rank
   FROM table) s
 ORDER BY rank DESC LIMIT 50 OFFSET 0;

创建索引命令

CREATE INDEX fts_index ON table USING 
gin((setweight(to_tsvector('english', lower(title)), 'A') || 
setweight(to_tsvector('english', lower(body)), 'B')));"

该表包含超过 90,000 行,但 postgres 选择执行顺序扫描,这需要几分钟,而不是使用索引。

【问题讨论】:

    标签: postgresql full-text-indexing


    【解决方案1】:

    索引无法使用。

    此处索引的唯一用途是支持ORDER BY 子句,为此该索引必须是名为@ 的完整 表达式上的常规B-Tree 索引987654322@ 在您的查询中。

    【讨论】:

    • 但我的查询中名为 rank 的 complete 表达式包含搜索词组。
    • 不,它没有。缺少对 ts_rank_cd(tsvector, tsquery) 的调用。
    • 有没有办法使用setweight并且仍然使用索引?
    • 据我所知,索引的使用与权重无关。它们用于排名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多