From 25f0b01d50b61c63065e12a2d9dc3fa3d1015a80 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 18 May 2022 13:59:04 -0500 Subject: [PATCH] posts: optimize has: metatag. Use EXISTS queries instead of `id IN (?)` subqueries because they're faster, especially when negated. --- app/models/post.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index bf2d5869b..84cb04950 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1088,17 +1088,17 @@ class Post < ApplicationRecord when "source" where.not(source: "") when "appeals" - where(id: PostAppeal.select(:post_id)) + where(PostAppeal.where("post_appeals.post_id = posts.id").arel.exists) when "flags" - where(id: PostFlag.by_users.select(:post_id)) + where(PostFlag.by_users.where("post_flags.post_id = posts.id").arel.exists) when "replacements" - where(id: PostReplacement.select(:post_id)) + where(PostReplacement.where("post_replacements.post_id = posts.id").arel.exists) when "comments" - where(id: Comment.undeleted.select(:post_id)) + where(Comment.undeleted.where("comments.post_id = posts.id").arel.exists) when "commentary" - where(id: ArtistCommentary.undeleted.select(:post_id)) + where(ArtistCommentary.undeleted.where("artist_commentaries.post_id = posts.id").arel.exists) when "notes" - where(id: Note.active.select(:post_id)) + where(Note.active.where("notes.post_id = posts.id").arel.exists) when "pools" where(id: Pool.undeleted.select("unnest(post_ids)")) else