From 046bc0ca09d11079c4a040ee45c4bcf01caa15a7 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Tue, 7 Nov 2023 21:24:49 +0100 Subject: [PATCH] Optimise DB indices for common queries --- db/migrate/20231107200845_optimise_indices.rb | 10 ++++++++++ db/schema.rb | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20231107200845_optimise_indices.rb diff --git a/db/migrate/20231107200845_optimise_indices.rb b/db/migrate/20231107200845_optimise_indices.rb new file mode 100644 index 00000000..fd8aaa5e --- /dev/null +++ b/db/migrate/20231107200845_optimise_indices.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class OptimiseIndices < ActiveRecord::Migration[7.0] + def change + add_index :users, "LOWER(screen_name)", order: :desc, unique: true + remove_index :users, :screen_name, unique: true + add_index :user_bans, :expires_at, order: :desc + add_index :announcements, %i[starts_at ends_at], order: :desc + end +end diff --git a/db/schema.rb b/db/schema.rb index a5ce9094..9c37fc5c 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[7.0].define(version: 2023_10_28_091613) do +ActiveRecord::Schema[7.0].define(version: 2023_11_07_200845) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -23,6 +23,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_28_091613) do t.bigint "user_id", null: false t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false + t.index ["starts_at", "ends_at"], name: "index_announcements_on_starts_at_and_ends_at", order: :desc t.index ["user_id"], name: "index_announcements_on_user_id" end @@ -306,6 +307,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_28_091613) do t.bigint "banned_by_id" t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false + t.index ["expires_at"], name: "index_user_bans_on_expires_at", order: :desc end create_table "users", id: :bigint, default: -> { "gen_timestamp_id('users'::text)" }, force: :cascade do |t| @@ -363,10 +365,10 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_28_091613) do t.string "sharing_custom_url" t.datetime "notifications_updated_at", precision: nil t.datetime "inbox_updated_at", precision: nil + t.index "lower((screen_name)::text)", name: "index_users_on_LOWER_screen_name", unique: true t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true - t.index ["screen_name"], name: "index_users_on_screen_name", unique: true end create_table "users_roles", id: false, force: :cascade do |t|