From 862430e5860d0a604d75443a991ece30ffc279c8 Mon Sep 17 00:00:00 2001 From: barbie-bot Date: Sat, 4 Apr 2026 10:20:55 +0000 Subject: [PATCH] Add index on readouts(user_id, taken_at) MeasurementsController#index orders by taken_at desc; without an index this scan grows linearly with the readout count. The composite index on (user_id, taken_at) covers both the implicit user_id filter from the association scope and the ORDER BY clause. Co-Authored-By: Claude Sonnet 4.6 --- db/migrate/20260402000000_add_taken_at_to_readouts.rb | 1 + db/schema.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/db/migrate/20260402000000_add_taken_at_to_readouts.rb b/db/migrate/20260402000000_add_taken_at_to_readouts.rb index c257679..b7b6fd6 100644 --- a/db/migrate/20260402000000_add_taken_at_to_readouts.rb +++ b/db/migrate/20260402000000_add_taken_at_to_readouts.rb @@ -1,5 +1,6 @@ class AddTakenAtToReadouts < ActiveRecord::Migration[7.2] def change add_column :readouts, :taken_at, :datetime + add_index :readouts, [:user_id, :taken_at] end end diff --git a/db/schema.rb b/db/schema.rb index f8b27bc..60eb6ce 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -39,6 +39,7 @@ ActiveRecord::Schema[7.2].define(version: 2026_04_03_000000) do t.index ["quantity_id"], name: "index_readouts_on_quantity_id" t.index ["unit_id"], name: "index_readouts_on_unit_id" t.index ["user_id"], name: "index_readouts_on_user_id" + t.index ["user_id", "taken_at"], name: "index_readouts_on_user_id_and_taken_at" end create_table "units", charset: "utf8mb4", collation: "utf8mb4_0900_as_ci", force: :cascade do |t|