【发布时间】:2021-01-23 01:00:14
【问题描述】:
我正在尝试在我的 rails 应用程序中实现 pg_search。我已经开始工作了,但搜索速度很慢,超过 20 秒。我的addresses 表中有超过700 万条记录。有什么方法可以让它更快?
app/models/address.rb
class Address < ApplicationRecord
include PgSearch::Model
pg_search_scope :search_for, against: %i[address_line_1 address_line_2], using: %i[tsearch trigram]
更新
我已经添加了这个索引,但它似乎仍然很慢
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
【问题讨论】:
-
您是否考虑将三元组索引添加到表中? about.gitlab.com/blog/2016/03/18/…
-
@max 我已经在做 trigram 和 tsearch 了。我应该只用三元组吗?
-
我需要手动创建索引还是由 pg_search 处理?
标签: sql ruby-on-rails ruby postgresql pg-search