diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-03-24 12:27:54 +0000 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2017-03-24 23:11:19 +0800 |
commit | 48ddef887beb93c4f1684249b7adbbc097bf9a57 (patch) | |
tree | e4ef75d3d3376d2708f54db54436a2c0a24a2b3b | |
parent | 8ba5a367f233bee72f09325cf0857ffbbfcd9e1a (diff) | |
download | gitlab-ce-48ddef887beb93c4f1684249b7adbbc097bf9a57.tar.gz |
Merge branch '22145-slow-search' into 'master'
Simplify projects, merge requests search queries
See merge request !10053
-rw-r--r-- | app/models/merge_request.rb | 6 | ||||
-rw-r--r-- | app/models/project.rb | 13 |
2 files changed, 8 insertions, 11 deletions
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 0f7b8311588..aa45ecf1b5b 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -154,8 +154,10 @@ class MergeRequest < ActiveRecord::Base # # Returns an ActiveRecord::Relation. def self.in_projects(relation) - source = where(source_project_id: relation).select(:id) - target = where(target_project_id: relation).select(:id) + # unscoping unnecessary conditions that'll be applied + # 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]) where("merge_requests.id IN (#{union.to_sql})") diff --git a/app/models/project.rb b/app/models/project.rb index 04641dd58a0..f1bba56d32c 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -314,20 +314,15 @@ class Project < ActiveRecord::Base ntable = Namespace.arel_table 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). or(ptable[:name].matches(pattern)). or(ptable[:description].matches(pattern)) ) - # We explicitly remove any eager loading clauses as they're: - # - # 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). + namespaces = unscoped.select(:id). joins(:namespace). where(ntable[:name].matches(pattern)) |