From bd56acd37814933e5fb51f1fe1adce6171532979 Mon Sep 17 00:00:00 2001 From: Andreas Brandl Date: Mon, 2 Sep 2019 17:26:15 +0200 Subject: [PATCH] Create partial index for gitlab-monitor CI metrics This creates a partial index intended to speed up queries on `ci_builds`. Particularly, `gitlab-monitor` has rather heavy queries. Those have been changed to only look back 7 days and benefit from this index tremendously. Relates to https://gitlab.com/gitlab-org/gitlab-exporter/merge_requests/101. --- .../ab-add-index-for-ci-builds-metrics.yml | 5 +++++ ...2152329_add_index_for_ci_builds_metrics.rb | 19 +++++++++++++++++++ db/schema.rb | 1 + 3 files changed, 25 insertions(+) create mode 100644 changelogs/unreleased/ab-add-index-for-ci-builds-metrics.yml create mode 100644 db/migrate/20190902152329_add_index_for_ci_builds_metrics.rb diff --git a/changelogs/unreleased/ab-add-index-for-ci-builds-metrics.yml b/changelogs/unreleased/ab-add-index-for-ci-builds-metrics.yml new file mode 100644 index 00000000000..03a37dc0c04 --- /dev/null +++ b/changelogs/unreleased/ab-add-index-for-ci-builds-metrics.yml @@ -0,0 +1,5 @@ +--- +title: Create partial index for gitlab-monitor CI metrics +merge_request: 32546 +author: +type: performance diff --git a/db/migrate/20190902152329_add_index_for_ci_builds_metrics.rb b/db/migrate/20190902152329_add_index_for_ci_builds_metrics.rb new file mode 100644 index 00000000000..b755d60e311 --- /dev/null +++ b/db/migrate/20190902152329_add_index_for_ci_builds_metrics.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class AddIndexForCiBuildsMetrics < ActiveRecord::Migration[5.2] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + INDEX_NAME = 'ci_builds_gitlab_monitor_metrics' + + def up + add_concurrent_index(:ci_builds, [:status, :created_at, :project_id], where: "type = 'Ci::Build'", name: INDEX_NAME) + end + + def down + remove_concurrent_index_by_name(:ci_builds, INDEX_NAME) + end +end diff --git a/db/schema.rb b/db/schema.rb index f2d6f70217b..79f4551ee26 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -605,6 +605,7 @@ ActiveRecord::Schema.define(version: 2019_09_02_160015) do t.index ["scheduled_at"], name: "partial_index_ci_builds_on_scheduled_at_with_scheduled_jobs", where: "((scheduled_at IS NOT NULL) AND ((type)::text = 'Ci::Build'::text) AND ((status)::text = 'scheduled'::text))" t.index ["stage_id", "stage_idx"], name: "tmp_build_stage_position_index", where: "(stage_idx IS NOT NULL)" t.index ["stage_id"], name: "index_ci_builds_on_stage_id" + t.index ["status", "created_at", "project_id"], name: "ci_builds_gitlab_monitor_metrics", where: "((type)::text = 'Ci::Build'::text)" t.index ["status", "type", "runner_id"], name: "index_ci_builds_on_status_and_type_and_runner_id" t.index ["token"], name: "index_ci_builds_on_token", unique: true t.index ["token_encrypted"], name: "index_ci_builds_on_token_encrypted", unique: true, where: "(token_encrypted IS NOT NULL)"