我有一个带有标题字符串列的产品模型,我已将其复制到索引的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倍,我缺少什么?
谢谢你的帮助!