其他
让 API 端点的响应速度提高 50 倍!
users 用户记录; subscriptions 订阅关系记录; subscriber_lists 订阅信息记录; documents 文档记录。
"title": "Harry Potter",
"author": "JK Rowling"
...
}
return [] unless query.present?
subscriber_lists = SubscriberList.where("ARRAY(SELECT json_object_keys(attributes)) = Array[:keys]", keys: query.keys)
subscriber_lists.select do |subscriber_list|
subscriber_list.attributes.all? do |key, attributes|
query_values = query[key]
list_values = attributes[key]
query_values.sort == list_values.sort
end
end
end
def change
add_column :subscriber_lists, :attributes_digest, :string
end
end
disable_ddl_transaction!
def change
add_index :subscriber_lists,
:attributes_digest,
algorithm: :concurrently
end
end
before_save do
self.attributes_digest = HashDigest.new(attributes).generate
end
# migration
class AddDigestToExistingSubscriberLists < ActiveRecord::Migration[5.2]
disable_ddl_transaction!
def change
SubscriberList.where("attributes_digest IS NULL").find_each do |list|
list.attributes_digest = HashDigest.new(list.attributes).generate
list.save!
end
end
end
digest = HashDigest.new(query).generate
SubscriberList.find_by_attributes_digest(digest)
end
基准测试结果
请求的比例和耗时(单位:毫秒)
50% 2518
66% 2549
75% 2744
80% 2873
90% 3138
95% 3161
98% 3161
99% 3161
100% 3161 (慢请求的耗时)
请求的比例和耗时(单位:毫秒)
50% 41
66% 45
75% 47
80% 49
90% 53
95% 57
98% 63
99% 66
100% 598 (慢请求的耗时)
热 文 推 荐