A platform for community discussion. Free, open, simple.
PERF: Preload user options when status is enabled (#28827)
Bianca Nenciu38592dc48e6c
diff --git a/lib/topic_view.rb b/lib/topic_view.rbindex f2df2c26b3d..23f21afa66c 100644--- a/lib/topic_view.rb+++ b/lib/topic_view.rb@@ -752,8 +752,9 @@ class TopicViewusernames.flatten!usernames.uniq!- users =- User.where(username_lower: usernames).includes(:user_status).index_by(&:username_lower)+ users = User.where(username_lower: usernames)+ users = users.includes(:user_option, :user_status) if SiteSetting.enable_user_status+ users = users.index_by(&:username_lower)mentions.reduce({}) do |hash, (post_id, post_mentioned_usernames)|hash[post_id] = post_mentioned_usernames.map { |username| users[username] }.compactdiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 782fafb702f..06d6bb3f19c 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -2438,6 +2438,25 @@ RSpec.describe TopicsController doexpect(second_request_queries.count).to eq(first_request_queries.count)end+ it "does not result in N+1 queries loading mentioned users" do+ SiteSetting.enable_user_status = true++ post =+ Fabricate(+ :post,+ raw:+ "post with many mentions: @#{user.username}, @#{user_2.username}, @#{admin.username}, @#{moderator.username}",+ )++ queries = track_sql_queries { get "/t/#{post.topic_id}.json" }++ user_statuses_queries = queries.filter { |q| q =~ /FROM "?user_statuses"?/ }+ expect(user_statuses_queries.size).to eq(2) # for current user and for all mentioned users++ user_options_queries = queries.filter { |q| q =~ /FROM "?user_options"?/ }+ expect(user_options_queries.size).to eq(1) # for all mentioned users+ end+context "with registered redirect_to_correct_topic_additional_query_parameters" dolet(:modifier_block) { Proc.new { |allowed_params| allowed_params << :silly_param } }