Skip to content

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

FIX: improve performance of post alerter job (#22378)

Krzysztof Kotlarek7a204e754c17
diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb
index f0d6848ece4..a17e675da2f 100644
--- a/app/services/post_alerter.rb
+++ b/app/services/post_alerter.rb
@@ -270,15 +270,12 @@ class PostAlerter
end
def category_or_tag_muters(topic)
- User
- .joins(
- "LEFT JOIN category_users ON users.id = category_users.user_id AND category_users.category_id = #{topic.category_id.to_i} AND category_users.notification_level = #{CategoryUser.notification_levels[:muted].to_i}",
- )
- .joins("LEFT JOIN topic_tags ON topic_tags.topic_id = #{topic.id.to_i}")
- .joins(
- "LEFT JOIN tag_users ON users.id = tag_users.user_id AND tag_users.tag_id = topic_tags.tag_id AND tag_users.notification_level = #{TagUser.notification_levels[:muted].to_i}",
- )
- .where("category_users.id IS NOT NULL OR tag_users.id IS NOT NULL")
+ user_ids_sql = <<~SQL
+ SELECT user_id FROM category_users WHERE category_id = #{topic.category_id.to_i} AND notification_level = #{CategoryUser.notification_levels[:muted]}
+ UNION
+ SELECT user_id FROM tag_users tu JOIN topic_tags tt ON tt.tag_id = tu.tag_id AND tt.topic_id = #{topic.id} AND tu.notification_level = #{TagUser.notification_levels[:muted]}
+ SQL
+ User.where("id IN (#{user_ids_sql})")
end
def notify_first_post_watchers(post, user_ids, notified = nil)