Fix N+1 in tootctl search deploy (#26710)

This commit is contained in:
Claire 2023-08-29 14:06:22 +02:00 committed by GitHub
parent ae6cf33321
commit c0605747ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 17 deletions

View File

@ -2,23 +2,14 @@
class Importer::PublicStatusesIndexImporter < Importer::BaseImporter class Importer::PublicStatusesIndexImporter < Importer::BaseImporter
def import! def import!
indexable_statuses_scope.find_in_batches(batch_size: @batch_size) do |batch| scope.select(:id).find_in_batches(batch_size: @batch_size) do |batch|
in_work_unit(batch.map(&:status_id)) do |status_ids| in_work_unit(batch.pluck(:id)) do |status_ids|
bulk = ActiveRecord::Base.connection_pool.with_connection do bulk = ActiveRecord::Base.connection_pool.with_connection do
Chewy::Index::Import::BulkBuilder.new(index, to_index: Status.includes(:media_attachments, :preloadable_poll).where(id: status_ids)).bulk_body Chewy::Index::Import::BulkBuilder.new(index, to_index: Status.includes(:media_attachments, :preloadable_poll, :preview_cards).where(id: status_ids)).bulk_body
end end
indexed = 0 indexed = bulk.count { |entry| entry[:index] }
deleted = 0 deleted = bulk.count { |entry| entry[:delete] }
bulk.map! do |entry|
if entry[:index]
indexed += 1
else
deleted += 1
end
entry
end
Chewy::Index::Import::BulkRequest.new(index).perform(bulk) Chewy::Index::Import::BulkRequest.new(index).perform(bulk)
@ -35,7 +26,7 @@ class Importer::PublicStatusesIndexImporter < Importer::BaseImporter
PublicStatusesIndex PublicStatusesIndex
end end
def indexable_statuses_scope def scope
Status.indexable.select('"statuses"."id", COALESCE("statuses"."reblog_of_id", "statuses"."id") AS status_id') Status.indexable
end end
end end

View File

@ -14,7 +14,7 @@ class Importer::StatusesIndexImporter < Importer::BaseImporter
scope.find_in_batches(batch_size: @batch_size) do |tmp| scope.find_in_batches(batch_size: @batch_size) do |tmp|
in_work_unit(tmp.map(&:status_id)) do |status_ids| in_work_unit(tmp.map(&:status_id)) do |status_ids|
bulk = ActiveRecord::Base.connection_pool.with_connection do bulk = ActiveRecord::Base.connection_pool.with_connection do
Chewy::Index::Import::BulkBuilder.new(index, to_index: Status.includes(:media_attachments, :preloadable_poll).where(id: status_ids)).bulk_body Chewy::Index::Import::BulkBuilder.new(index, to_index: Status.includes(:media_attachments, :preloadable_poll, :preview_cards).where(id: status_ids)).bulk_body
end end
indexed = 0 indexed = 0