A platform for community discussion. Free, open, simple.
PERF: Improve performance of `most_replied_to_users` (#26373)
Isaac Janzendb10dd53192c
diff --git a/app/models/user_summary.rb b/app/models/user_summary.rbindex c5e4c93fee0..8156afcc419 100644--- a/app/models/user_summary.rb+++ b/app/models/user_summary.rb@@ -88,7 +88,13 @@ class UserSummary.joins("JOIN posts replies ON posts.topic_id = replies.topic_id AND posts.reply_to_post_number = replies.post_number",)- .where("replies.user_id <> ?", @user.id)+ .joins(+ "JOIN topics ON replies.topic_id = topics.id AND topics.archetype <> 'private_message'",+ )+ .joins(+ "AND replies.post_type IN (#{Topic.visible_post_types(@user, include_moderator_actions: false).join(",")})",+ )+ .where("replies.user_id <> posts.user_id").group("replies.user_id").order("COUNT(*) DESC").limit(MAX_SUMMARY_RESULTS)diff --git a/spec/models/user_summary_spec.rb b/spec/models/user_summary_spec.rbindex 128a801b4aa..9146341fea4 100644--- a/spec/models/user_summary_spec.rb+++ b/spec/models/user_summary_spec.rb@@ -90,4 +90,45 @@ RSpec.describe UserSummary doexpect(summary.top_categories.first[:topic_count]).to eq(1)expect(summary.top_categories.first[:post_count]).to eq(1)end++ it "returns the most replied to users" do+ topic1 = create_post.topic+ topic1_post = create_post(topic: topic1)+ topic1_reply =+ create_post(topic: topic1, reply_to_post_number: topic1_post.post_number, user: topic1.user)++ # Create a second topic by the same user as topic1+ topic2 = create_post(user: topic1.user).topic+ topic2_post = create_post(topic: topic2)+ topic2_reply =+ create_post(topic: topic2, reply_to_post_number: topic2_post.post_number, user: topic2.user)++ # Don't include replies to whispers+ topic3 = create_post(user: topic1.user).topic+ topic3_post = create_post(topic: topic3, post_type: Post.types[:whisper])+ topic3_reply =+ create_post(topic: topic3, reply_to_post_number: topic3_post.post_number, user: topic3.user)++ # Don't include replies to private messages+ replied_to_user = Fabricate(:user)+ topic4 =+ create_post(+ user: topic1.user,+ archetype: Archetype.private_message,+ target_usernames: [replied_to_user.username],+ ).topic+ topic4_post = create_post(topic: topic4, user: replied_to_user)+ topic4_reply =+ create_post(topic: topic4, reply_to_post_number: topic4_post.post_number, user: topic4.user)++ user_summary = UserSummary.new(topic1.user, Guardian.new(topic1.user))+ most_replied_to_users = user_summary.most_replied_to_users++ counts =+ most_replied_to_users+ .index_by { |user_with_count| user_with_count[:id] }+ .transform_values { |c| c[:count] }++ expect(counts).to eq({ topic1_post.user_id => 1, topic2_post.user_id => 1 })+ endend