Skip to content

A platform for community discussion. Free, open, simple.

PERF: Batch post lookup in likes_export

Jake Goldsboroughafd29df27417
diff --git a/app/jobs/regular/export_user_archive.rb b/app/jobs/regular/export_user_archive.rb
index f5480010dcf..5508c2a10f8 100644
--- a/app/jobs/regular/export_user_archive.rb
+++ b/app/jobs/regular/export_user_archive.rb
@@ -396,25 +396,34 @@ module Jobs
def likes_export
return enum_for(:likes_export) unless block_given?
+
PostAction
.with_deleted
.where(user_id: @archive_for_user.id)
.where(post_action_type_id: post_action_type_view.types[:like])
- .order(:created_at)
- .each do |pa|
- post = Post.with_deleted.find_by(id: pa.post_id)
- yield(
- [
- pa.id,
- pa.post_id,
- post&.topic_id,
- post&.post_number,
- pa.created_at,
- pa.updated_at,
- pa.deleted_at,
- self_or_other(pa.deleted_by_id),
- ]
- )
+ .in_batches(of: 1000) do |batch|
+ posts_by_id =
+ Post
+ .with_deleted
+ .where(id: batch.select(:post_id))
+ .select(:id, :topic_id, :post_number)
+ .index_by(&:id)
+
+ batch.each do |pa|
+ post = posts_by_id[pa.post_id]
+ yield(
+ [
+ pa.id,
+ pa.post_id,
+ post&.topic_id,
+ post&.post_number,
+ pa.created_at,
+ pa.updated_at,
+ pa.deleted_at,
+ self_or_other(pa.deleted_by_id),
+ ]
+ )
+ end
end
end