Commit 6947dd1a authored by pbair's avatar pbair

Add index to audit_events to prevent timeouts

Add index on audit_events (entity_id, entity_type, id desc), to
replace existing index on (entity_id, entity_type)
parent 5880a933
---
title: Add index to audit_events (entity_id, entity_type, id)
merge_request: 23998
author:
type: performance
# frozen_string_literal: true
class AddIndexOnAuditEventsIdDesc < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
OLD_INDEX_NAME = 'index_audit_events_on_entity_id_and_entity_type'
NEW_INDEX_NAME = 'index_audit_events_on_entity_id_and_entity_type_and_id_desc'
disable_ddl_transaction!
def up
add_concurrent_index :audit_events, [:entity_id, :entity_type, :id], name: NEW_INDEX_NAME,
order: { entity_id: :asc, entity_type: :asc, id: :desc }
remove_concurrent_index_by_name :audit_events, OLD_INDEX_NAME
end
def down
add_concurrent_index :audit_events, [:entity_id, :entity_type], name: OLD_INDEX_NAME
remove_concurrent_index_by_name :audit_events, NEW_INDEX_NAME
end
end
...@@ -465,7 +465,7 @@ ActiveRecord::Schema.define(version: 2020_01_30_161817) do ...@@ -465,7 +465,7 @@ ActiveRecord::Schema.define(version: 2020_01_30_161817) do
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.index ["created_at", "author_id"], name: "analytics_index_audit_events_on_created_at_and_author_id" t.index ["created_at", "author_id"], name: "analytics_index_audit_events_on_created_at_and_author_id"
t.index ["entity_id", "entity_type"], name: "index_audit_events_on_entity_id_and_entity_type" t.index ["entity_id", "entity_type", "id"], name: "index_audit_events_on_entity_id_and_entity_type_and_id_desc", order: { id: :desc }
end end
create_table "award_emoji", id: :serial, force: :cascade do |t| create_table "award_emoji", id: :serial, force: :cascade do |t|
......
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