Commit 7c60b263 authored by Mikolaj Wawrzyniak's avatar Mikolaj Wawrzyniak

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
parent 4bfc014e
# 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
2ed9198926eb0579fccd4a8b1866f10ba4f42683d676e0664db3fadefe0bed39
\ No newline at end of file
...@@ -25853,6 +25853,8 @@ CREATE INDEX index_events_on_author_id_and_created_at_merge_requests ON events U ...@@ -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_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_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); CREATE INDEX index_events_on_project_id_and_created_at ON events USING btree (project_id, created_at);
...@@ -15,7 +15,7 @@ RSpec.describe Gitlab::UsageDataNonSqlMetrics do ...@@ -15,7 +15,7 @@ RSpec.describe Gitlab::UsageDataNonSqlMetrics do
described_class.uncached_data described_class.uncached_data
end end
expect(recorder.count).to eq(57) expect(recorder.count).to eq(59)
end end
end end
end end
...@@ -723,7 +723,9 @@ module Gitlab ...@@ -723,7 +723,9 @@ module Gitlab
else else
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
# rubocop: disable UsageData/LargeTable # 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 UsageData/LargeTable
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
end 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