pg_search pg_search_scope表现不佳

时间:2016-05-25 15:30:57

标签: ruby-on-rails postgresql pg-search

我有一个带有标题字符串列的产品模型,我已将其复制到索引的tvector列中。

这是模型文件中的pg_search实现:

class Product
  include PgSearch
  pg_search_scope :search_by_title, against: :title,
    using:{
      tsearch: {
        dictionary: 'simple',
        prefix: true,
        tsvector_column: 'tsv'
      }
}
end

这是Product.search_by_title('windo')生成的查询:

SELECT "products".* 
FROM "products" 
INNER JOIN (SELECT "products"."id" AS pg_search_id, (ts_rank(("products"."tsv"), (to_tsquery('simple', ''' ' || 'windo' || ' ''' || ':*')), 0)) AS rank FROM "products" WHERE ((("products"."tsv") @@ (to_tsquery('simple', ''' ' || 'windo' || ' ''' || ':*'))))
) AS pg_search_0a3e27b8ca818264d75c8d ON "products"."id" = pg_search_0a3e27b8ca818264d75c8d.pg_search_id  
ORDER BY pg_search_0a3e27b8ca818264d75c8d.rank DESC, "products"."id" ASC

这个连接查询搞乱我的性能:-(,为什么不pg_search构建这样的查询:

SELECT "products".*, (ts_rank(("products"."tsv"), (to_tsquery('simple', ''' ' || 'wind' || ' ''' || ':*')), 0)) AS rank 
FROM "products" 
WHERE ((("products"."tsv") @@ (to_tsquery('simple', ''' ' || 'wind' || ' ''' || ':*'))))
ORDER BY rank DESC, "products"."id" ASC

...这快100倍,我缺少什么?

谢谢你的帮助!

0 个答案:

没有答案