mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
PERF: Preload voters_count and has_voted (#28808)
These fields are often used when serializing topics which may contain multiple polls. On average, serializing a poll took 2+N queries where N is the number of options. This change reduces the number of queries to 3, one for each field (Poll#voters_count, PollOption#voters_count and Poll#has_voted?).
This commit is contained in:
@ -182,12 +182,12 @@ after_initialize do
|
||||
end
|
||||
|
||||
if post_with_polls.present?
|
||||
Poll
|
||||
.where(post_id: post_with_polls)
|
||||
.each do |p|
|
||||
polls[p.post_id] ||= []
|
||||
polls[p.post_id] << p
|
||||
end
|
||||
all_polls = Poll.includes(:poll_options).where(post_id: post_with_polls)
|
||||
Poll.preload!(all_polls, user_id: @user&.id)
|
||||
all_polls.each do |p|
|
||||
polls[p.post_id] ||= []
|
||||
polls[p.post_id] << p
|
||||
end
|
||||
end
|
||||
|
||||
polls
|
||||
|
Reference in New Issue
Block a user