Commit 4ce8219e authored by Adam Hegyi's avatar Adam Hegyi

Improve query performance of merged_at filter

This change significantly improves the performance of productivity
analytics and throughput analytics features.
parent 22b5c365
......@@ -11,7 +11,9 @@ module MergedAtFilter
mr_metrics_scope = mr_metrics_scope.merged_after(merged_after) if merged_after.present?
mr_metrics_scope = mr_metrics_scope.merged_before(merged_before) if merged_before.present?
items.joins(:metrics).merge(mr_metrics_scope)
scope = items.joins(:metrics).merge(mr_metrics_scope)
scope = target_project_id_filter_on_metrics(scope) if Feature.enabled?(:improved_mr_merged_at_queries)
scope
end
# rubocop: enable CodeReuse/ActiveRecord
......@@ -22,4 +24,10 @@ module MergedAtFilter
def merged_before
params[:merged_before]
end
# rubocop: disable CodeReuse/ActiveRecord
def target_project_id_filter_on_metrics(scope)
scope.where(MergeRequest.arel_table[:target_project_id].eq(MergeRequest::Metrics.arel_table[:target_project_id]))
end
# rubocop: enable CodeReuse/ActiveRecord
end
---
name: improved_mr_merged_at_queries
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39329
rollout_issue_url:
group: group::analytics
type: development
default_enabled: false
# frozen_string_literal: true
class AddIndexToMrMetricsTargetProjectId < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = 'index_merge_request_metrics_on_target_project_id_merged_at'
disable_ddl_transaction!
def up
add_concurrent_index :merge_request_metrics, [:target_project_id, :merged_at], name: INDEX_NAME
end
def down
remove_concurrent_index_by_name(:merge_request_metrics, INDEX_NAME)
end
end
4fb7fda59db193250ca393cae5561fa52d61e025bb93b10829f114ac7fef4cfa
\ No newline at end of file
......@@ -19990,6 +19990,8 @@ CREATE INDEX index_merge_request_metrics_on_pipeline_id ON public.merge_request_
CREATE INDEX index_merge_request_metrics_on_target_project_id ON public.merge_request_metrics USING btree (target_project_id);
CREATE INDEX index_merge_request_metrics_on_target_project_id_merged_at ON public.merge_request_metrics USING btree (target_project_id, merged_at);
CREATE UNIQUE INDEX index_merge_request_user_mentions_on_note_id ON public.merge_request_user_mentions USING btree (note_id) WHERE (note_id IS NOT NULL);
CREATE INDEX index_merge_requests_closing_issues_on_issue_id ON public.merge_requests_closing_issues USING btree (issue_id);
......
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