summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTiago Botelho <tiagonbotelho@hotmail.com>2018-03-22 15:25:46 +0000
committerTiago Botelho <tiagonbotelho@hotmail.com>2018-03-26 13:25:03 +0100
commit7f0dd13b2bddaa2e18a54861b5c5033661aa0dd7 (patch)
tree509c880fe35f738a5cffde572cafbc5b1b211d37
parent40c338a4bbcfbcb1da0a54acfdf730f6bae6763f (diff)
downloadgitlab-ce-43552-user-owned-projects-query-performance-improvement.tar.gz
Improves User#owned_projects query performance43552-user-owned-projects-query-performance-improvement
-rw-r--r--app/models/user.rb14
-rw-r--r--changelogs/unreleased/43552-user-owned-projects-query-performance-improvement.yml5
2 files changed, 16 insertions, 3 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index b8c55205ab8..81eb6f3ab7b 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -623,9 +623,7 @@ class User < ActiveRecord::Base
end
def owned_projects
- @owned_projects ||=
- Project.where('namespace_id IN (?) OR namespace_id = ?',
- owned_groups.select(:id), namespace.id).joins(:namespace)
+ @owned_projects ||= Project.from("(#{owned_projects_union.to_sql}) AS projects")
end
# Returns projects which user can admin issues on (for example to move an issue to that project).
@@ -1196,6 +1194,16 @@ class User < ActiveRecord::Base
private
+ def owned_projects_union
+ Gitlab::SQL::Union.new([
+ Project.where(namespace: namespace).select(:id),
+ Project.joins(:project_authorizations)
+ .where("namespace_id <> ?", namespace.id)
+ .where(project_authorizations: { user_id: id, access_level: Gitlab::Access::OWNER })
+ .select(:id)
+ ], remove_duplicates: false)
+ end
+
def ci_projects_union
scope = { access_level: [Gitlab::Access::MASTER, Gitlab::Access::OWNER] }
groups = groups_projects.where(members: scope)
diff --git a/changelogs/unreleased/43552-user-owned-projects-query-performance-improvement.yml b/changelogs/unreleased/43552-user-owned-projects-query-performance-improvement.yml
new file mode 100644
index 00000000000..f92d2324b33
--- /dev/null
+++ b/changelogs/unreleased/43552-user-owned-projects-query-performance-improvement.yml
@@ -0,0 +1,5 @@
+---
+title: Improve User#owned_projects query performance
+merge_request: 17934
+author:
+type: performance