From 369c9baeb983b16b018b584c9a1814fe6dfc2a9f Mon Sep 17 00:00:00 2001 From: Markus Koller Date: Thu, 15 Aug 2019 11:56:54 +0200 Subject: 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. --- changelogs/unreleased/optimize-note-indexes.yml | 5 ++++ ..._notes_on_project_id_and_id_and_system_false.rb | 30 ++++++++++++++++++++++ ...15093949_remove_index_notes_on_noteable_type.rb | 29 +++++++++++++++++++++ db/schema.rb | 4 +-- 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 changelogs/unreleased/optimize-note-indexes.yml create mode 100644 db/migrate/20190815093936_add_index_notes_on_project_id_and_id_and_system_false.rb create mode 100644 db/migrate/20190815093949_remove_index_notes_on_noteable_type.rb diff --git a/changelogs/unreleased/optimize-note-indexes.yml b/changelogs/unreleased/optimize-note-indexes.yml new file mode 100644 index 00000000000..bfb84779abf --- /dev/null +++ b/changelogs/unreleased/optimize-note-indexes.yml @@ -0,0 +1,5 @@ +--- +title: Optimize DB indexes for ES indexing of notes +merge_request: 31846 +author: +type: performance diff --git a/db/migrate/20190815093936_add_index_notes_on_project_id_and_id_and_system_false.rb b/db/migrate/20190815093936_add_index_notes_on_project_id_and_id_and_system_false.rb new file mode 100644 index 00000000000..cbbece35901 --- /dev/null +++ b/db/migrate/20190815093936_add_index_notes_on_project_id_and_id_and_system_false.rb @@ -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 diff --git a/db/migrate/20190815093949_remove_index_notes_on_noteable_type.rb b/db/migrate/20190815093949_remove_index_notes_on_noteable_type.rb new file mode 100644 index 00000000000..158c88e6258 --- /dev/null +++ b/db/migrate/20190815093949_remove_index_notes_on_noteable_type.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 7c4a91da750..6024c396af6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -2240,7 +2240,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 -- cgit v1.2.1