From a7f89d13d24cc8d4a52ee6049ea5b943ab9ea594 Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 7 Oct 2025 14:37:40 +0200 Subject: [PATCH] Change index on `follows` table to improve performance of some queries (#36374) --- ..._index_follows_on_target_account_id_and_account_id.rb | 9 +++++++++ ...07100813_remove_index_follows_on_target_account_id.rb | 9 +++++++++ db/schema.rb | 4 ++-- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20251007100627_add_index_follows_on_target_account_id_and_account_id.rb create mode 100644 db/migrate/20251007100813_remove_index_follows_on_target_account_id.rb diff --git a/db/migrate/20251007100627_add_index_follows_on_target_account_id_and_account_id.rb b/db/migrate/20251007100627_add_index_follows_on_target_account_id_and_account_id.rb new file mode 100644 index 000000000..23c08d2d0 --- /dev/null +++ b/db/migrate/20251007100627_add_index_follows_on_target_account_id_and_account_id.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddIndexFollowsOnTargetAccountIdAndAccountId < ActiveRecord::Migration[8.0] + disable_ddl_transaction! + + def change + add_index :follows, [:target_account_id, :account_id], algorithm: :concurrently + end +end diff --git a/db/migrate/20251007100813_remove_index_follows_on_target_account_id.rb b/db/migrate/20251007100813_remove_index_follows_on_target_account_id.rb new file mode 100644 index 000000000..142086f35 --- /dev/null +++ b/db/migrate/20251007100813_remove_index_follows_on_target_account_id.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class RemoveIndexFollowsOnTargetAccountId < ActiveRecord::Migration[8.0] + disable_ddl_transaction! + + def change + remove_index :follows, [:target_account_id], algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index af60a1b11..47937f865 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[8.0].define(version: 2025_10_02_140103) do +ActiveRecord::Schema[8.0].define(version: 2025_10_07_100813) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -571,7 +571,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_10_02_140103) do t.boolean "notify", default: false, null: false t.string "languages", array: true t.index ["account_id", "target_account_id"], name: "index_follows_on_account_id_and_target_account_id", unique: true - t.index ["target_account_id"], name: "index_follows_on_target_account_id" + t.index ["target_account_id", "account_id"], name: "index_follows_on_target_account_id_and_account_id" end create_table "generated_annual_reports", force: :cascade do |t|