From d13aebabc36b6f5fcf41ba32a9c6ee45b91daf3f Mon Sep 17 00:00:00 2001 From: Andreas Brandl Date: Tue, 13 Feb 2018 16:13:17 +0100 Subject: Replace OR clause with UNION. --- app/models/project.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/project.rb b/app/models/project.rb index 2ba6a863500..2dc3dd38099 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -325,7 +325,13 @@ class Project < ActiveRecord::Base levels = Gitlab::VisibilityLevel.levels_for_user(user) - where('EXISTS (?) OR projects.visibility_level IN (?)', authorized, levels) + authorized_projects = where('EXISTS (?)', authorized).select(:id) + visible_projects = where('visibility_level IN (?)', levels).select(:id) + + # We use a UNION here instead of OR clauses since this results in better + # performance. + union = Gitlab::SQL::Union.new([authorized_projects, visible_projects]) + where("projects.id IN (#{union.to_sql})") # rubocop:disable GitlabSecurity/SqlInjection else public_to_user end -- cgit v1.2.1