PERF: Preload topic thumbnails for all topic lists (#11238)

Previously thumbnails were only preloaded for queries using `TopicQuery#default_results`, which meant that requests for PM topic lists would lead to N+1 queries.

This commit moves the preloading into TopicList#load_topics, along with other similar preloads (e.g. plugin custom fields)

The direct call to `ActiveRecord::Associations::Preloader#preload` is necessary because `@topics` can be an array, not an `ActiveRecord::Relation`
This commit is contained in:
David Taylor 2020-11-16 13:23:49 +00:00 committed by GitHub
parent bc8423a1bf
commit 86ffa3ba4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 6 deletions

View File

@ -130,6 +130,8 @@ class TopicList < DraftableList
ft.topic_list = self
end
ActiveRecord::Associations::Preloader.new.preload(@topics, [:image_upload, topic_thumbnails: :optimized_image])
if preloaded_custom_fields.present?
Topic.preload_custom_fields(@topics, preloaded_custom_fields)
end

View File

@ -873,8 +873,6 @@ class TopicQuery
result = result.where('topics.posts_count <= ?', options[:max_posts]) if options[:max_posts].present?
result = result.where('topics.posts_count >= ?', options[:min_posts]) if options[:min_posts].present?
result = preload_thumbnails(result)
result = TopicQuery.apply_custom_filters(result, self)
result
@ -1115,10 +1113,6 @@ class TopicQuery
result.order('topics.bumped_at DESC')
end
def preload_thumbnails(result)
result.preload(:image_upload, topic_thumbnails: :optimized_image)
end
private
def append_read_state(list, group)