Add index on events using created_at and id

To suport Service Ping metrics performance dedicated index is required.
This is follow up commit to
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/73273
which scheduled async index creation,
Changelog: other
This commit is contained in:
Mikolaj Wawrzyniak 2021-11-03 17:31:15 +01:00
parent 4bfc014ef1
commit 7c60b263cd
5 changed files with 24 additions and 2 deletions

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
class AddIndexOnEventsUsingBtreeCreatedAtId < Gitlab::Database::Migration[1.0]
INDEX_NAME = 'index_events_on_created_at_and_id'
TABLE = :events
COLUMNS = %i[created_at id]
CONSTRAINTS = "created_at > '2021-08-27 00:00:00+00'::timestamp with time zone"
disable_ddl_transaction!
def up
add_concurrent_index TABLE, COLUMNS, name: INDEX_NAME, where: CONSTRAINTS
end
def down
remove_concurrent_index TABLE, COLUMNS, name: INDEX_NAME, where: CONSTRAINTS
end
end

View file

@ -0,0 +1 @@
2ed9198926eb0579fccd4a8b1866f10ba4f42683d676e0664db3fadefe0bed39

View file

@ -25853,6 +25853,8 @@ CREATE INDEX index_events_on_author_id_and_created_at_merge_requests ON events U
CREATE INDEX index_events_on_author_id_and_project_id ON events USING btree (author_id, project_id);
CREATE INDEX index_events_on_created_at_and_id ON events USING btree (created_at, id) WHERE (created_at > '2021-08-27 00:00:00+00'::timestamp with time zone);
CREATE INDEX index_events_on_group_id_partial ON events USING btree (group_id) WHERE (group_id IS NOT NULL);
CREATE INDEX index_events_on_project_id_and_created_at ON events USING btree (project_id, created_at);

View file

@ -15,7 +15,7 @@
described_class.uncached_data
end
expect(recorder.count).to eq(57)
expect(recorder.count).to eq(59)
end
end
end

View file

@ -723,7 +723,9 @@ def stage_manage_events(time_period)
else
# rubocop: disable CodeReuse/ActiveRecord
# rubocop: disable UsageData/LargeTable
estimate_batch_distinct_count(::Event.where(time_period), :author_id)
start = ::Event.where(time_period).select(:id).order(created_at: :asc).first&.id
finish = ::Event.where(time_period).select(:id).order(created_at: :asc).first&.id
estimate_batch_distinct_count(::Event.where(time_period), :author_id, start: start, finish: finish)
# rubocop: enable UsageData/LargeTable
# rubocop: enable CodeReuse/ActiveRecord
end