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:
parent
bc8423a1bf
commit
86ffa3ba4f
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue