Commit 354d1cce authored by Marius Bobin's avatar Marius Bobin

Merge branch '336436-change-db-index-for-ci_owned_runners' into 'master'

Optimize User#ci_owned_runners query

See merge request gitlab-org/gitlab!79729
parents 9f752b20 79085e71
...@@ -15,6 +15,9 @@ module Ci ...@@ -15,6 +15,9 @@ module Ci
end end
scope :by_namespace_id, -> (namespace_id) { where(namespace_id: namespace_id) } scope :by_namespace_id, -> (namespace_id) { where(namespace_id: namespace_id) }
scope :namespace_id_from_traversal_ids, -> do
select('ci_namespace_mirrors.traversal_ids[array_length(ci_namespace_mirrors.traversal_ids, 1)] AS namespace_id')
end
class << self class << self
def sync!(event) def sync!(event)
......
...@@ -2230,20 +2230,32 @@ class User < ApplicationRecord ...@@ -2230,20 +2230,32 @@ class User < ApplicationRecord
end end
def ci_owned_project_runners_from_group_members def ci_owned_project_runners_from_group_members
Ci::RunnerProject cte_project_ids = Gitlab::SQL::CTE.new(
.select('ci_runners.*') :cte_project_ids,
.joins(:runner) Ci::ProjectMirror
.joins('JOIN ci_project_mirrors ON ci_project_mirrors.project_id = ci_runner_projects.project_id') .select(:project_id)
.joins('JOIN ci_namespace_mirrors ON ci_namespace_mirrors.namespace_id = ci_project_mirrors.namespace_id') .joins('JOIN ci_namespace_mirrors ON ci_namespace_mirrors.traversal_ids[array_length(ci_namespace_mirrors.traversal_ids, 1)] = ci_project_mirrors.namespace_id')
.merge(ci_namespace_mirrors_for_group_members(Gitlab::Access::MAINTAINER)) .merge(ci_namespace_mirrors_for_group_members(Gitlab::Access::MAINTAINER))
)
Ci::Runner
.select('ci_runners.*')
.joins(:runner_projects)
.where('ci_runner_projects.project_id IN (SELECT project_id FROM cte_project_ids)')
.with(cte_project_ids.to_arel)
end end
def ci_owned_group_runners def ci_owned_group_runners
Ci::RunnerNamespace cte_namespace_ids = Gitlab::SQL::CTE.new(
:cte_namespace_ids,
ci_namespace_mirrors_for_group_members(Gitlab::Access::OWNER).namespace_id_from_traversal_ids
)
Ci::Runner
.select('ci_runners.*') .select('ci_runners.*')
.joins(:runner) .joins(:runner_namespaces)
.joins('JOIN ci_namespace_mirrors ON ci_namespace_mirrors.namespace_id = ci_runner_namespaces.namespace_id') .where('ci_runner_namespaces.namespace_id IN (SELECT namespace_id FROM cte_namespace_ids)')
.merge(ci_namespace_mirrors_for_group_members(Gitlab::Access::OWNER)) .with(cte_namespace_ids.to_arel)
end end
def ci_namespace_mirrors_for_group_members(level) def ci_namespace_mirrors_for_group_members(level)
......
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