Optimize DB indexes for ES indexing of notes

To index notes, we exclude system notes and use `find_in_batches` to
load them in batches for submission to the ES bulk import API.
These queries often result in DB timeouts because the usage of
`ORDER BY id` results in the `notes_pkey` index being used.

This adds an optimized partial index, and removes the unused index
`index_notes_on_noteable_type` which is already covered for our
usage by the existing `index_notes_on_noteable_id_and_noteable_type`.

Newer versions of PostgreSQL (at least 11) are smarter about this and
use `index_notes_on_project_id_and_noteable_type` instead, so we might
be able to remove the partial index again in the future.
This commit is contained in:
Markus Koller 2019-08-15 11:56:54 +02:00 committed by Mayra Cabrera
parent b7f191c5d0
commit a3e716104a
4 changed files with 66 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
title: Optimize DB indexes for ES indexing of notes
merge_request: 31846
author:
type: performance

View File

@ -0,0 +1,30 @@
# frozen_string_literal: true
class AddIndexNotesOnProjectIdAndIdAndSystemFalse < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index(*index_arguments)
end
def down
remove_concurrent_index(*index_arguments)
end
private
def index_arguments
[
:notes,
[:project_id, :id],
{
name: 'index_notes_on_project_id_and_id_and_system_false',
where: 'NOT system'
}
]
end
end

View File

@ -0,0 +1,29 @@
# frozen_string_literal: true
class RemoveIndexNotesOnNoteableType < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
remove_concurrent_index(*index_arguments)
end
def down
add_concurrent_index(*index_arguments)
end
private
def index_arguments
[
:notes,
[:noteable_type],
{
name: 'index_notes_on_noteable_type'
}
]
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_08_12_070645) do
ActiveRecord::Schema.define(version: 2019_08_15_093949) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
@ -2279,7 +2279,7 @@ ActiveRecord::Schema.define(version: 2019_08_12_070645) do
t.index ["line_code"], name: "index_notes_on_line_code"
t.index ["note"], name: "index_notes_on_note_trigram", opclass: :gin_trgm_ops, using: :gin
t.index ["noteable_id", "noteable_type"], name: "index_notes_on_noteable_id_and_noteable_type"
t.index ["noteable_type"], name: "index_notes_on_noteable_type"
t.index ["project_id", "id"], name: "index_notes_on_project_id_and_id_and_system_false", where: "(NOT system)"
t.index ["project_id", "noteable_type"], name: "index_notes_on_project_id_and_noteable_type"
t.index ["review_id"], name: "index_notes_on_review_id"
end