[英]How can I make pg_search faster?
I'm trying to implement pg_search in my rails application.我正在尝试在我的 rails 应用程序中实现 pg_search。 I've got it working but searches are very slow, over 20 seconds.
我已经让它工作了,但搜索速度很慢,超过 20 秒。 I have over 7 million records in my
addresses
table.我的
addresses
表中有超过 700 万条记录。 Are there ways I can make it faster?有什么方法可以让它更快吗?
class Address < ApplicationRecord
include PgSearch::Model
pg_search_scope :search_for, against: %i[address_line_1 address_line_2], using: %i[tsearch trigram]
I've added this index but it still seems to be just as slow我已经添加了这个索引,但它似乎仍然一样慢
class IndexAddressesOnAddressLine1 < ActiveRecord::Migration[6.1]
# An index can be created concurrently only outside of a transaction.
disable_ddl_transaction!
def up
execute <<~SQL
CREATE INDEX pg_search_addresses_on_fields ON addresses USING gin(coalesce(address_line_1, address_line_2, ''::text) gin_trgm_ops)
SQL
end
def down
execute <<~SQL
DELETE INDEX pg_search_addresses_on_fields
SQL
end
end
By default, pg_search
uses a threshold of 0.3 for trigram searches.默认情况下,
pg_search
使用 0.3 的阈值进行三元搜索。 Higher numbers match more strictly and thus return fewer results.数字越大匹配越严格,因此返回的结果越少。 Lower numbers match more permissively, letting in more results.
较低的数字更容易匹配,从而获得更多结果。
pg_search_scope :search_for,
against: %i[address_line_1 address_line_2],
using: {
tsearch: { dictionary: 'english' },
trigram: { threshold: 0.5 } # increase the threshold
}
I'm not sure but maybe you need to reindex your model:我不确定,但也许你需要重新索引你的 model:
rake pg_search:multisearch:rebuild[Address]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.