mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 14:42:46 +08:00
PERF: Prefer subquery instead of two queries (#25167)
The query that is now a subquery could return a long list of category IDs, which slowed down the query considerably. This improvement reduces the execution time from over 2 seconds down to about 100ms.
This commit is contained in:
parent
8c6144d116
commit
be46acce8f
|
@ -58,10 +58,9 @@ class CategoryList
|
|||
if SiteSetting.fixed_category_positions
|
||||
categories.order(:position, :id)
|
||||
else
|
||||
allowed_category_ids = categories.pluck(:id) << nil # `nil` is necessary to include categories without any associated topics
|
||||
categories
|
||||
.left_outer_joins(:featured_topics)
|
||||
.where(topics: { category_id: allowed_category_ids })
|
||||
.where("topics.category_id IS NULL OR topics.category_id IN (?)", categories.select(:id))
|
||||
.group("categories.id")
|
||||
.order("max(topics.bumped_at) DESC NULLS LAST")
|
||||
.order("categories.id ASC")
|
||||
|
|
Loading…
Reference in New Issue
Block a user