mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 14:42:46 +08:00
PERF: N+1 query when searching with tags enabled.
This commit is contained in:
parent
ce57ff9fcf
commit
137f91d1cf
|
@ -777,8 +777,7 @@ class Search
|
|||
def aggregate_posts(post_sql)
|
||||
return [] unless post_sql
|
||||
|
||||
Post.includes(:topic => :category)
|
||||
.includes(:user)
|
||||
posts_eager_loads(Post)
|
||||
.joins("JOIN (#{post_sql}) x ON x.id = posts.topic_id AND x.post_number = posts.post_number")
|
||||
.order('row_number')
|
||||
end
|
||||
|
@ -806,9 +805,9 @@ class Search
|
|||
|
||||
def topic_search
|
||||
if @search_context.is_a?(Topic)
|
||||
posts = posts_query(@limit).where('posts.topic_id = ?', @search_context.id)
|
||||
.includes(:topic => :category)
|
||||
.includes(:user)
|
||||
posts = posts_eager_loads(posts_query(@limit))
|
||||
.where('posts.topic_id = ?', @search_context.id)
|
||||
|
||||
posts.each do |post|
|
||||
@results.add(post)
|
||||
end
|
||||
|
@ -817,4 +816,15 @@ class Search
|
|||
end
|
||||
end
|
||||
|
||||
def posts_eager_loads(query)
|
||||
query = query.includes(:user)
|
||||
topic_eager_loads = [:category]
|
||||
|
||||
if SiteSetting.tagging_enabled
|
||||
topic_eager_loads << :tags
|
||||
end
|
||||
|
||||
query.includes(topic: topic_eager_loads)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user