Commit 6bb185fb authored by Andreas Brandl's avatar Andreas Brandl

Create optimal indexes for created_at order

This is for the projects API. Note that we can't have one index only
because we cannot scan the index backwards for both `sort=asc,sort=desc`
options. That is because the tie breaker is always `id desc` and hence
the sort order is not suitable for a backward scan for one of the
options.

Relates to https://gitlab.com/gitlab-org/gitlab/issues/36390
parent 9bc4dfeb
---
title: Create optimal indexes for created_at order (Projects API)
merge_request: 22623
author:
type: performance
# frozen_string_literal: true
class CreateIndexesForProjectApiCreatedAtOrder < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :projects, %i(visibility_level created_at id), order: { id: :desc }, name: 'index_projects_on_visibility_level_created_at_id_desc'
add_concurrent_index :projects, %i(visibility_level created_at id), order: { created_at: :desc, id: :desc }, name: 'index_projects_on_visibility_level_created_at_desc_id_desc'
remove_concurrent_index_by_name :projects, 'index_projects_on_visibility_level_and_created_at_and_id'
end
def down
add_concurrent_index :projects, %i(visibility_level created_at id), name: 'index_projects_on_visibility_level_and_created_at_and_id'
remove_concurrent_index_by_name :projects, 'index_projects_on_visibility_level_created_at_id_desc'
remove_concurrent_index_by_name :projects, 'index_projects_on_visibility_level_created_at_desc_id_desc'
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_01_08_100603) do
ActiveRecord::Schema.define(version: 2020_01_08_155731) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
......@@ -3376,7 +3376,8 @@ ActiveRecord::Schema.define(version: 2020_01_08_100603) do
t.index ["runners_token"], name: "index_projects_on_runners_token"
t.index ["runners_token_encrypted"], name: "index_projects_on_runners_token_encrypted"
t.index ["star_count"], name: "index_projects_on_star_count"
t.index ["visibility_level", "created_at", "id"], name: "index_projects_on_visibility_level_and_created_at_and_id"
t.index ["visibility_level", "created_at", "id"], name: "index_projects_on_visibility_level_created_at_desc_id_desc", order: { created_at: :desc, id: :desc }
t.index ["visibility_level", "created_at", "id"], name: "index_projects_on_visibility_level_created_at_id_desc", order: { id: :desc }
end
create_table "prometheus_alert_events", 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