PERF: Remove N+1 query when generating posters summary.

This commit is contained in:
Guo Xiang Tan 2017-09-14 12:07:35 +08:00
parent f3fadf41b7
commit 195982fd41
2 changed files with 9 additions and 2 deletions

View File

@ -24,7 +24,9 @@ class PrimaryGroupLookup
.select(:id, :primary_group_id) .select(:id, :primary_group_id)
group_lookup = {} group_lookup = {}
group_ids = users_with_primary_group.map(&:primary_group_id).compact group_ids = users_with_primary_group.map(&:primary_group_id)
group_ids.uniq!
Group.where(id: group_ids).select(self.class.lookup_columns) Group.where(id: group_ids).select(self.class.lookup_columns)
.each { |g| group_lookup[g.id] = g } .each { |g| group_lookup[g.id] = g }

View File

@ -346,8 +346,13 @@ class TopicQuery
end end
avatar_lookup = AvatarLookup.new(user_ids) avatar_lookup = AvatarLookup.new(user_ids)
primary_group_lookup = PrimaryGroupLookup.new(user_ids)
topics.each do |t| topics.each do |t|
t.posters = t.posters_summary(avatar_lookup: avatar_lookup) t.posters = t.posters_summary(
avatar_lookup: avatar_lookup,
primary_group_lookup: primary_group_lookup
)
end end
end end