Commit a3e71610 authored by Markus Koller's avatar Markus Koller Committed by Mayra Cabrera

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.
parent b7f191c5
---
title: Optimize DB indexes for ES indexing of notes
merge_request: 31846
author:
type: performance
# 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
# 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
......@@ -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
......
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