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,22 +50,33 @@ module Labels ...@@ -50,22 +50,33 @@ module Labels
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def group_labels_applied_to_issues def group_labels_applied_to_issues
@group_labels_applied_to_issues ||= Label.joins(:issues) @labels_applied_to_issues ||= if use_optimized_group_labels_query?
.where( 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 }, issues: { project_id: project.id },
labels: { group_id: old_group.self_and_ancestors } labels: { group_id: old_group.self_and_ancestors }
) )
end end
end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def group_labels_applied_to_merge_requests def group_labels_applied_to_merge_requests
@group_labels_applied_to_merge_requests ||= Label.joins(:merge_requests) @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( .where(
merge_requests: { target_project_id: project.id }, merge_requests: { target_project_id: project.id },
labels: { group_id: old_group.self_and_ancestors } labels: { group_id: old_group.self_and_ancestors }
) )
end end
end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
def find_or_create_label!(label) def find_or_create_label!(label)
...@@ -88,5 +99,9 @@ module Labels ...@@ -88,5 +99,9 @@ module Labels
.update_all(label_id: new_label_id) .update_all(label_id: new_label_id)
end end
# rubocop: enable CodeReuse/ActiveRecord # 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
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
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Labels::TransferService do RSpec.describe Labels::TransferService do
shared_examples 'transfer labels' do
describe '#execute' do describe '#execute' do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
...@@ -106,4 +107,17 @@ RSpec.describe Labels::TransferService do ...@@ -106,4 +107,17 @@ RSpec.describe Labels::TransferService do
end end
end end
end end
end
context 'with use_optimized_group_labels_query FF on' do
it_behaves_like 'transfer labels'
end
context 'with use_optimized_group_labels_query FF off' do
before do
stub_feature_flags(use_optimized_group_labels_query: false)
end
it_behaves_like 'transfer labels'
end
end end
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