Commit 6e0cd311 authored by Alexandru Croitor's avatar Alexandru Croitor

Add a query optimisation for fetching group labels

Try a query optimisation for fetching group labels when
transfering a project.
parent 9970eee1
......@@ -50,21 +50,32 @@ module Labels
# rubocop: disable CodeReuse/ActiveRecord
def group_labels_applied_to_issues
@group_labels_applied_to_issues ||= Label.joins(:issues)
.where(
issues: { project_id: project.id },
labels: { group_id: old_group.self_and_ancestors }
)
@labels_applied_to_issues ||= if use_optimized_group_labels_query?
Label.joins(:issues)
.joins("INNER JOIN namespaces on namespaces.id = labels.group_id AND namespaces.type = 'Group'" )
.where(issues: { project_id: project.id }).reorder(nil)
else
Label.joins(:issues).where(
issues: { project_id: project.id },
labels: { group_id: old_group.self_and_ancestors }
)
end
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def group_labels_applied_to_merge_requests
@group_labels_applied_to_merge_requests ||= Label.joins(:merge_requests)
.where(
merge_requests: { target_project_id: project.id },
labels: { group_id: old_group.self_and_ancestors }
)
@labels_applied_to_mrs ||= if use_optimized_group_labels_query?
Label.joins(:merge_requests)
.joins("INNER JOIN namespaces on namespaces.id = labels.group_id AND namespaces.type = 'Group'" )
.where(merge_requests: { target_project_id: project.id }).reorder(nil)
else
Label.joins(:merge_requests)
.where(
merge_requests: { target_project_id: project.id },
labels: { group_id: old_group.self_and_ancestors }
)
end
end
# rubocop: enable CodeReuse/ActiveRecord
......@@ -88,5 +99,9 @@ module Labels
.update_all(label_id: new_label_id)
end
# rubocop: enable CodeReuse/ActiveRecord
def use_optimized_group_labels_query?
Feature.enabled?(:use_optimized_group_labels_query, project.root_namespace, default_enabled: :yaml)
end
end
end
---
name: use_optimized_group_labels_query
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/73501
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/344957
milestone: '14.5'
type: development
group: group::workspace
default_enabled: false
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