Commit b603617e authored by Douwe Maan's avatar Douwe Maan

Merge branch '43552-user-owned-projects-query-performance-improvement' into 'master'

Resolve "Slow SQL running in production queryid 3257994922"

Closes #43552

See merge request gitlab-org/gitlab-ce!17934
parents 6c8d4b80 5f22dbdb
...@@ -623,9 +623,7 @@ class User < ActiveRecord::Base ...@@ -623,9 +623,7 @@ class User < ActiveRecord::Base
end end
def owned_projects def owned_projects
@owned_projects ||= @owned_projects ||= Project.from("(#{owned_projects_union.to_sql}) AS projects")
Project.where('namespace_id IN (?) OR namespace_id = ?',
owned_groups.select(:id), namespace.id).joins(:namespace)
end end
# Returns projects which user can admin issues on (for example to move an issue to that project). # Returns projects which user can admin issues on (for example to move an issue to that project).
...@@ -1196,6 +1194,15 @@ class User < ActiveRecord::Base ...@@ -1196,6 +1194,15 @@ class User < ActiveRecord::Base
private private
def owned_projects_union
Gitlab::SQL::Union.new([
Project.where(namespace: namespace),
Project.joins(:project_authorizations)
.where("projects.namespace_id <> ?", namespace.id)
.where(project_authorizations: { user_id: id, access_level: Gitlab::Access::OWNER })
], remove_duplicates: false)
end
def ci_projects_union def ci_projects_union
scope = { access_level: [Gitlab::Access::MASTER, Gitlab::Access::OWNER] } scope = { access_level: [Gitlab::Access::MASTER, Gitlab::Access::OWNER] }
groups = groups_projects.where(members: scope) groups = groups_projects.where(members: scope)
......
---
title: Improves the performance of projects list page
merge_request: 17934
author:
type: performance
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