Index notifications on updated_at

As we now update notifications with unread counts we sort them by the
time they were updated instead of when they were created. To keep that
performant we also have to change the previous indices that used
created_at to use updated_at.
This commit is contained in:
Stanko K.R.
2026-02-12 11:36:35 +01:00
parent 61144667fc
commit 22276a25c8
3 changed files with 12 additions and 4 deletions
@@ -0,0 +1,8 @@
class FixNotificationsOrderedIndex < ActiveRecord::Migration[8.2]
def change
add_index :notifications, [ :user_id, :read_at, :updated_at ],
order: { read_at: :desc, updated_at: :desc },
name: "index_notifications_on_user_id_and_read_at_and_updated_at"
remove_index :notifications, name: "index_notifications_on_user_id_and_read_at_and_created_at"
end
end
Generated
+2 -2
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.2].define(version: 2026_02_11_122517) do
ActiveRecord::Schema[8.2].define(version: 2026_02_12_102026) do
create_table "accesses", id: :uuid, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.datetime "accessed_at"
t.uuid "account_id", null: false
@@ -398,7 +398,7 @@ ActiveRecord::Schema[8.2].define(version: 2026_02_11_122517) do
t.index ["creator_id"], name: "index_notifications_on_creator_id"
t.index ["source_type", "source_id"], name: "index_notifications_on_source"
t.index ["user_id", "card_id"], name: "index_notifications_on_user_id_and_card_id", unique: true
t.index ["user_id", "read_at", "created_at"], name: "index_notifications_on_user_id_and_read_at_and_created_at", order: { read_at: :desc, created_at: :desc }
t.index ["user_id", "read_at", "updated_at"], name: "index_notifications_on_user_id_and_read_at_and_updated_at", order: { read_at: :desc, updated_at: :desc }
t.index ["user_id"], name: "index_notifications_on_user_id"
end
+2 -2
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.2].define(version: 2026_02_11_122517) do
ActiveRecord::Schema[8.2].define(version: 2026_02_12_102026) do
create_table "accesses", id: :uuid, force: :cascade do |t|
t.datetime "accessed_at"
t.uuid "account_id", null: false
@@ -398,7 +398,7 @@ ActiveRecord::Schema[8.2].define(version: 2026_02_11_122517) do
t.index ["creator_id"], name: "index_notifications_on_creator_id"
t.index ["source_type", "source_id"], name: "index_notifications_on_source"
t.index ["user_id", "card_id"], name: "index_notifications_on_user_id_and_card_id", unique: true
t.index ["user_id", "read_at", "created_at"], name: "index_notifications_on_user_id_and_read_at_and_created_at", order: { read_at: :desc, created_at: :desc }
t.index ["user_id", "read_at", "updated_at"], name: "index_notifications_on_user_id_and_read_at_and_updated_at", order: { read_at: :desc, updated_at: :desc }
t.index ["user_id"], name: "index_notifications_on_user_id"
end