search: optimize username metatags.

Optimize metatag searches involving usernames, including user:,
approver:, appealer:, commenter:, upvoter:, etc.

Do `User.find_by_name` instead of `User.name_matches` because this
fetches the user upfront instead of doing it inside a subquery. Using a
subquery makes the SQL more complicated and leads to worse query plans.
This especially helps searches involving multiple username metatags.
This commit is contained in:
evazion
2021-11-25 00:35:39 -06:00
parent 5ba6014a4e
commit 5dc67613e6

View File

@@ -266,7 +266,9 @@ class PostQueryBuilder
when "none" when "none"
Post.where(field => nil) Post.where(field => nil)
else else
Post.where(field => User.name_matches(username)) user = User.find_by_name(username)
return Post.none if user.nil?
Post.where(field => user)
end end
end end
@@ -278,7 +280,9 @@ class PostQueryBuilder
elsif username == "none" elsif username == "none"
Post.where("NOT EXISTS (#{subquery.to_sql})") Post.where("NOT EXISTS (#{subquery.to_sql})")
elsif block.nil? elsif block.nil?
subquery = subquery.where(field => User.name_matches(username)) user = User.find_by_name(username)
return Post.none if user.nil?
subquery = subquery.where(field => user)
Post.where("EXISTS (#{subquery.to_sql})") Post.where("EXISTS (#{subquery.to_sql})")
else else
subquery = subquery.merge(block.call(username)) subquery = subquery.merge(block.call(username))