Commit 8cc4a39b authored by Yorick Peterse's avatar Yorick Peterse

Merge branch '22145-slow-search' into 'master'

Simplify projects, merge requests search queries

See merge request !10053
parents d561881b df0603cf
...@@ -154,8 +154,10 @@ class MergeRequest < ActiveRecord::Base ...@@ -154,8 +154,10 @@ class MergeRequest < ActiveRecord::Base
# #
# Returns an ActiveRecord::Relation. # Returns an ActiveRecord::Relation.
def self.in_projects(relation) def self.in_projects(relation)
source = where(source_project_id: relation).select(:id) # unscoping unnecessary conditions that'll be applied
target = where(target_project_id: relation).select(:id) # when executing `where("merge_requests.id IN (#{union.to_sql})")`
source = unscoped.where(source_project_id: relation).select(:id)
target = unscoped.where(target_project_id: relation).select(:id)
union = Gitlab::SQL::Union.new([source, target]) union = Gitlab::SQL::Union.new([source, target])
where("merge_requests.id IN (#{union.to_sql})") where("merge_requests.id IN (#{union.to_sql})")
......
...@@ -314,20 +314,15 @@ class Project < ActiveRecord::Base ...@@ -314,20 +314,15 @@ class Project < ActiveRecord::Base
ntable = Namespace.arel_table ntable = Namespace.arel_table
pattern = "%#{query}%" pattern = "%#{query}%"
projects = select(:id).where( # unscoping unnecessary conditions that'll be applied
# when executing `where("projects.id IN (#{union.to_sql})")`
projects = unscoped.select(:id).where(
ptable[:path].matches(pattern). ptable[:path].matches(pattern).
or(ptable[:name].matches(pattern)). or(ptable[:name].matches(pattern)).
or(ptable[:description].matches(pattern)) or(ptable[:description].matches(pattern))
) )
# We explicitly remove any eager loading clauses as they're: namespaces = unscoped.select(:id).
#
# 1. Not needed by this query
# 2. Combined with .joins(:namespace) lead to all columns from the
# projects & namespaces tables being selected, leading to a SQL error
# due to the columns of all UNION'd queries no longer being the same.
namespaces = select(:id).
except(:includes).
joins(:namespace). joins(:namespace).
where(ntable[:name].matches(pattern)) where(ntable[:name].matches(pattern))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment