PERF: Remove N+1 queries on user messages page.

This commit is contained in:
Guo Xiang Tan 2016-06-28 10:01:00 +08:00
parent b4cb2e367c
commit e221414935
No known key found for this signature in database
GPG Key ID: 19C321C8952B0F72
5 changed files with 9 additions and 5 deletions

View File

@ -293,13 +293,15 @@ class ApplicationController < ActionController::Base
Middleware::AnonymousCache.anon_cache(request.env, time_length)
end
def fetch_user_from_params(opts=nil)
def fetch_user_from_params(opts=nil, eager_load = [])
opts ||= {}
user = if params[:username]
username_lower = params[:username].downcase.chomp('.json')
find_opts = { username_lower: username_lower }
find_opts[:active] = true unless opts[:include_inactive] || current_user.try(:staff?)
User.find_by(find_opts)
result = User
(result = result.includes(*eager_load)) if !eager_load.empty?
result.find_by(find_opts)
elsif params[:external_id]
external_id = params[:external_id].chomp('.json')
SingleSignOnRecord.find_by(external_id: external_id).try(:user)

View File

@ -109,7 +109,7 @@ class ListController < ApplicationController
[:topics_by, :private_messages, :private_messages_sent, :private_messages_unread, :private_messages_archive, :private_messages_group, :private_messages_group_archive].each do |action|
define_method("#{action}") do
list_opts = build_topic_list_options
target_user = fetch_user_from_params(include_inactive: current_user.try(:staff?))
target_user = fetch_user_from_params({ include_inactive: current_user.try(:staff?) }, [:user_stat, :user_option])
guardian.ensure_can_see_private_messages!(target_user.id) unless action == :topics_by
list = generate_list_for(action.to_s, target_user, list_opts)
url_prefix = "topics" unless action == :topics_by

View File

@ -67,6 +67,7 @@ class TopicListItemSerializer < ListableTopicSerializer
def include_tags?
SiteSetting.tagging_enabled
end
def tags
object.tags.map(&:name)
end

View File

@ -26,6 +26,7 @@ class TopicListSerializer < ApplicationSerializer
def include_tags?
SiteSetting.tagging_enabled && SiteSetting.show_filter_by_tag
end
def tags
Tag.top_tags
end

View File

@ -355,10 +355,10 @@ class TopicQuery
options = @options
options.reverse_merge!(per_page: per_page_setting)
result = Topic
result = Topic.includes(:tags)
if type == :group
result = result.includes(:allowed_groups)
result = result.includes(:allowed_users)
result = result.where("topics.id IN (SELECT topic_id FROM topic_allowed_groups
WHERE group_id IN (
SELECT group_id FROM group_users WHERE user_id = #{user.id.to_i}) AND