Skip to content

Commit

Permalink
Update PatientSession indexes
Browse files Browse the repository at this point in the history
This updates the indexes on the patient session model to remove an
unnecessary unique index and add missing indexes on the patient and
session column.

The index on the individual columns should improve performance when
looking up individual patient sessions for a patient or a session,
related to the foreign key added in
b350c8e.

The unique index on `session_id` and `patient_id` is unnecessary as
we already have an index on `patient_id` and `session_id`.
  • Loading branch information
thomasleese committed Dec 13, 2024
1 parent 2e4eeec commit 8eeaf4a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/models/patient_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
#
# Indexes
#
# index_patient_sessions_on_patient_id (patient_id)
# index_patient_sessions_on_patient_id_and_session_id (patient_id,session_id) UNIQUE
# index_patient_sessions_on_session_id_and_patient_id (session_id,patient_id) UNIQUE
# index_patient_sessions_on_session_id (session_id)
#
# Foreign Keys
#
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20241213104730_update_patient_session_indexes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class UpdatePatientSessionIndexes < ActiveRecord::Migration[8.0]
def change
remove_index :patient_sessions, %i[session_id patient_id], unique: true
add_index :patient_sessions, :patient_id
add_index :patient_sessions, :session_id
end
end
5 changes: 3 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2024_12_13_102755) do
ActiveRecord::Schema[8.0].define(version: 2024_12_13_104730) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
enable_extension "pg_trgm"
Expand Down Expand Up @@ -541,7 +541,8 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["patient_id", "session_id"], name: "index_patient_sessions_on_patient_id_and_session_id", unique: true
t.index ["session_id", "patient_id"], name: "index_patient_sessions_on_session_id_and_patient_id", unique: true
t.index ["patient_id"], name: "index_patient_sessions_on_patient_id"
t.index ["session_id"], name: "index_patient_sessions_on_session_id"
end

create_table "patients", force: :cascade do |t|
Expand Down
3 changes: 2 additions & 1 deletion spec/factories/patient_sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
#
# Indexes
#
# index_patient_sessions_on_patient_id (patient_id)
# index_patient_sessions_on_patient_id_and_session_id (patient_id,session_id) UNIQUE
# index_patient_sessions_on_session_id_and_patient_id (session_id,patient_id) UNIQUE
# index_patient_sessions_on_session_id (session_id)
#
# Foreign Keys
#
Expand Down
3 changes: 2 additions & 1 deletion spec/models/patient_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
#
# Indexes
#
# index_patient_sessions_on_patient_id (patient_id)
# index_patient_sessions_on_patient_id_and_session_id (patient_id,session_id) UNIQUE
# index_patient_sessions_on_session_id_and_patient_id (session_id,patient_id) UNIQUE
# index_patient_sessions_on_session_id (session_id)
#
# Foreign Keys
#
Expand Down

0 comments on commit 8eeaf4a

Please sign in to comment.