Commit 3dac2744 authored by Vladimir Shushlin's avatar Vladimir Shushlin

Speed up searching environments by ref deployed

Current query looks over all project deployments
and aggregates by environment_id

New query loops over project's environments and
tries to find existing deployment for it

We also add a dedicated index on
[environment_id, ref] to speed up this new query.

Changelog: added
parent 074ebc96
...@@ -23,12 +23,18 @@ module Environments ...@@ -23,12 +23,18 @@ module Environments
deployments.none deployments.none
end end
environment_ids = deployments environments =
.group(:environment_id) if Feature.enabled?(:environments_by_deployments_finder_exists_optimization, default_enabled: :yaml)
.select(:environment_id) project.environments.available
.where('EXISTS (?)', deployments.where('environment_id = environments.id'))
else
environment_ids = deployments
.group(:environment_id)
.select(:environment_id)
environments = project.environments.available project.environments.available
.where(id: environment_ids) .where(id: environment_ids)
end
if params[:find_latest] if params[:find_latest]
find_one(environments.order_by_last_deployed_at_desc) find_one(environments.order_by_last_deployed_at_desc)
......
---
name: environments_by_deployments_finder_exists_optimization
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/72781/
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/343544
milestone: '14.5'
type: development
group: group::release
default_enabled: false
# frozen_string_literal: true
class AddDeploymentsEnvironmentIdAndRefIndex < Gitlab::Database::Migration[1.0]
INDEX_NAME = 'index_deployments_on_environment_id_and_ref'
disable_ddl_transaction!
def up
add_concurrent_index :deployments, [:environment_id, :ref], name: INDEX_NAME
end
def down
remove_concurrent_index_by_name :deployments, INDEX_NAME
end
end
87834e00821eb1ed8489c1d772dc3ac743bcf84669e78c04c7988f6f761970b8
\ No newline at end of file
...@@ -25120,6 +25120,8 @@ CREATE INDEX index_deployments_on_environment_id_and_id ON deployments USING btr ...@@ -25120,6 +25120,8 @@ CREATE INDEX index_deployments_on_environment_id_and_id ON deployments USING btr
CREATE INDEX index_deployments_on_environment_id_and_iid_and_project_id ON deployments USING btree (environment_id, iid, project_id); CREATE INDEX index_deployments_on_environment_id_and_iid_and_project_id ON deployments USING btree (environment_id, iid, project_id);
CREATE INDEX index_deployments_on_environment_id_and_ref ON deployments USING btree (environment_id, ref);
CREATE INDEX index_deployments_on_environment_id_status_and_finished_at ON deployments USING btree (environment_id, status, finished_at); CREATE INDEX index_deployments_on_environment_id_status_and_finished_at ON deployments USING btree (environment_id, status, finished_at);
CREATE INDEX index_deployments_on_environment_id_status_and_id ON deployments USING btree (environment_id, status, id); CREATE INDEX index_deployments_on_environment_id_status_and_id ON deployments USING btree (environment_id, status, id);
...@@ -11,7 +11,7 @@ RSpec.describe Environments::EnvironmentsByDeploymentsFinder do ...@@ -11,7 +11,7 @@ RSpec.describe Environments::EnvironmentsByDeploymentsFinder do
project.add_maintainer(user) project.add_maintainer(user)
end end
describe '#execute' do shared_examples 'execute' do
context 'tagged deployment' do context 'tagged deployment' do
let(:environment_two) { create(:environment, project: project) } let(:environment_two) { create(:environment, project: project) }
# Environments need to include commits, so rewind two commits to fit # Environments need to include commits, so rewind two commits to fit
...@@ -124,4 +124,16 @@ RSpec.describe Environments::EnvironmentsByDeploymentsFinder do ...@@ -124,4 +124,16 @@ RSpec.describe Environments::EnvironmentsByDeploymentsFinder do
end end
end end
end end
describe "#execute" do
include_examples 'execute'
context 'when environments_by_deployments_finder_exists_optimization is disabled' do
before do
stub_feature_flags(environments_by_deployments_finder_exists_optimization: false)
end
include_examples 'execute'
end
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