Commit 2171d30b authored by Mikolaj Wawrzyniak's avatar Mikolaj Wawrzyniak

Add index to ci_builds table for usage_data

To make usage data queries that run on ci_builds
table more performant we need to add new dedicated
index
parent 1a50cb13
---
title: Add index on ci_builds relation to improve Usage Ping metrics collection performance.
merge_request: 37581
author:
type: added
# frozen_string_literal: true
class AddPartialIndexToCiBuildsTableOnUserIdName < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
INDEX_NAME = 'index_partial_ci_builds_on_user_id_name_parser_features'
FILTER_CONDITION = <<~SQL
(((type)::text = 'Ci::Build'::text) AND
((name)::text = ANY (
ARRAY[
('container_scanning'::character varying)::text,
('dast'::character varying)::text,
('dependency_scanning'::character varying)::text,
('license_management'::character varying)::text,
('license_scanning'::character varying)::text,
('sast'::character varying)::text,
('coverage_fuzzing'::character varying)::text,
('secret_detection'::character varying)::text
]
))
)
SQL
def up
add_concurrent_index(:ci_builds,
[:user_id, :name],
where: FILTER_CONDITION,
name: INDEX_NAME)
end
def down
remove_concurrent_index_by_name :ci_builds, INDEX_NAME
end
end
e73076f6d7540372ee16fe26dcb44e8b84dde69e27c46483ee26883e81059501
\ No newline at end of file
...@@ -20758,6 +20758,8 @@ CREATE INDEX index_pages_domains_on_wildcard ON pages_domains USING btree (wildc ...@@ -20758,6 +20758,8 @@ CREATE INDEX index_pages_domains_on_wildcard ON pages_domains USING btree (wildc
CREATE UNIQUE INDEX index_partial_am_alerts_on_project_id_and_fingerprint ON alert_management_alerts USING btree (project_id, fingerprint) WHERE (status <> 2); CREATE UNIQUE INDEX index_partial_am_alerts_on_project_id_and_fingerprint ON alert_management_alerts USING btree (project_id, fingerprint) WHERE (status <> 2);
CREATE INDEX index_partial_ci_builds_on_user_id_name_parser_features ON ci_builds USING btree (user_id, name) WHERE (((type)::text = 'Ci::Build'::text) AND ((name)::text = ANY (ARRAY[('container_scanning'::character varying)::text, ('dast'::character varying)::text, ('dependency_scanning'::character varying)::text, ('license_management'::character varying)::text, ('license_scanning'::character varying)::text, ('sast'::character varying)::text, ('coverage_fuzzing'::character varying)::text, ('secret_detection'::character varying)::text])));
CREATE UNIQUE INDEX index_partitioned_foreign_keys_unique_index ON partitioned_foreign_keys USING btree (to_table, from_table, from_column); CREATE UNIQUE INDEX index_partitioned_foreign_keys_unique_index ON partitioned_foreign_keys USING btree (to_table, from_table, from_column);
CREATE INDEX index_pat_on_user_id_and_expires_at ON personal_access_tokens USING btree (user_id, expires_at); CREATE INDEX index_pat_on_user_id_and_expires_at ON personal_access_tokens USING btree (user_id, expires_at);
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200908064229_add_partial_index_to_ci_builds_table_on_user_id_name.rb')
RSpec.describe AddPartialIndexToCiBuildsTableOnUserIdName do
let(:migration) { described_class.new }
describe '#up' do
it 'creates temporary partial index on type' do
expect { migration.up }.to change { migration.index_exists?(:ci_builds, [:user_id, :name], name: described_class::INDEX_NAME) }.from(false).to(true)
end
end
describe '#down' do
it 'removes temporary partial index on type' do
migration.up
expect { migration.down }.to change { migration.index_exists?(:ci_builds, [:user_id, :name], name: described_class::INDEX_NAME) }.from(true).to(false)
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