Commit d45724c1 authored by Stan Hu's avatar Stan Hu

Add index to optimize loading pipeline charts

The pipeline charts were hitting a database timeout because the SQL
query used `ORDER BY id desc`, which caused the database to scan all
rows instead of only the rows indexed by `project_id`. To fix this
query, we create an index on (project_id, id DESC).

Closes https://gitlab.com/gitlab-org/gitlab/issues/118746
parent 610aa44c
---
title: Add index to optimize loading pipeline charts
merge_request: 22052
author:
type: performance
# frozen_string_literal: true
class AddIndexOnProjectIdToCiPipelines < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = 'index_ci_pipelines_on_project_id_and_id_desc'
disable_ddl_transaction!
def up
add_concurrent_index :ci_pipelines, [:project_id, :id], name: INDEX_NAME, order: { id: :desc }
end
def down
remove_concurrent_index :ci_pipelines, [:project_id, :id], name: INDEX_NAME, order: { id: :desc }
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_12_16_183532) do
ActiveRecord::Schema.define(version: 2019_12_18_225624) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
......@@ -860,6 +860,7 @@ ActiveRecord::Schema.define(version: 2019_12_16_183532) do
t.index ["external_pull_request_id"], name: "index_ci_pipelines_on_external_pull_request_id", where: "(external_pull_request_id IS NOT NULL)"
t.index ["merge_request_id"], name: "index_ci_pipelines_on_merge_request_id", where: "(merge_request_id IS NOT NULL)"
t.index ["pipeline_schedule_id"], name: "index_ci_pipelines_on_pipeline_schedule_id"
t.index ["project_id", "id"], name: "index_ci_pipelines_on_project_id_and_id_desc", order: { id: :desc }
t.index ["project_id", "iid"], name: "index_ci_pipelines_on_project_id_and_iid", unique: true, where: "(iid IS NOT NULL)"
t.index ["project_id", "ref", "id"], name: "index_ci_pipelines_on_project_idandrefandiddesc", order: { id: :desc }
t.index ["project_id", "ref", "status", "id"], name: "index_ci_pipelines_on_project_id_and_ref_and_status_and_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