Skip to content

Commit c910dfc

Browse files
committed
Add fast path for patient lookup when patient_id is present
Previously, patient lookup always called existing_patients which performs expensive queries to match on name, DOB, and postcode, even when we already had a patient_id from a previous match. This significantly improves performance on the review screen where the patient is looked up to identify if it is a cross-team school move.
1 parent 3fd1860 commit c910dfc

7 files changed

Lines changed: 34 additions & 11 deletions

app/controllers/class_imports_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def show
6868
end
6969
end
7070

71-
@nhs_discrepancies = @class_import.changesets.nhs_number_discrepancies
71+
@nhs_discrepancies =
72+
@class_import.changesets.includes(:patient).nhs_number_discrepancies
7273

7374
@cancelled = @class_import.changesets.from_file.cancelled
7475
end

app/controllers/cohort_imports_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def show
6767
end
6868
end
6969

70-
@nhs_discrepancies = @cohort_import.changesets.nhs_number_discrepancies
70+
@nhs_discrepancies =
71+
@cohort_import.changesets.includes(:patient).nhs_number_discrepancies
7172

7273
@cancelled = @cohort_import.changesets.from_file.cancelled
7374
end

app/jobs/commit_import_job.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ def perform(import_global_id)
3131
imported_school_move_ids = []
3232

3333
ActiveRecord::Base.transaction do
34-
import
35-
.changesets
34+
changesets =
35+
PatientChangeset.includes(:patient).where(id: import.changesets.ids)
36+
37+
# Reset patient_ids to avoid stale associations
38+
changesets.update_all(patient_id: nil)
39+
40+
changesets
3641
.from_file
3742
.includes(:school)
3843
.find_in_batches(batch_size: 100) do |changesets|

app/jobs/commit_patient_changesets_job.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class CommitPatientChangesetsJob
1919
queue_as :imports
2020

2121
def perform(patient_changeset_ids)
22-
changesets = PatientChangeset.where(id: patient_changeset_ids)
22+
changesets =
23+
PatientChangeset.includes(:patient).where(id: patient_changeset_ids)
2324
import = changesets.first.import
2425
imported_school_move_ids = []
2526

@@ -30,6 +31,9 @@ def perform(patient_changeset_ids)
3031
.index_with { |col| import.public_send(col) || 0 }
3132

3233
ActiveRecord::Base.transaction do
34+
# Reset patient_ids to avoid stale associations
35+
changesets.update_all(patient_id: nil)
36+
3337
to_process = changesets.select { review_consistent?(it) }
3438

3539
if to_process.any?

app/jobs/review_class_import_school_move_job.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ def perform(import_id)
1313
return
1414
end
1515

16+
patient_in_import_ids =
17+
import.changesets.from_file.ready_for_review.pluck(:patient_id).compact
18+
1619
patients_in_import =
17-
import.changesets.from_file.ready_for_review.map(&:patient) +
18-
import.patients
20+
Patient.where(id: patient_in_import_ids) + import.patients
1921

2022
patients_in_future_review = import.changesets.needs_re_review.map(&:patient)
2123

app/models/class_import.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ def type_label
5656

5757
def postprocess_rows!
5858
# Remove patients already in the sessions but not in the class list.
59-
patients_in_import =
60-
(changesets.from_file - changesets.cancelled - changesets.processed).map(
61-
&:patient
62-
)
59+
patient_in_import_ids =
60+
changesets
61+
.from_file
62+
.where.not(status: %i[cancelled processed])
63+
.pluck(:patient_id)
64+
.compact
65+
66+
patients_in_import = Patient.where(id: patient_in_import_ids)
6367

6468
unknown_patients = patients_to_create_moves_for(patients_in_import)
6569

app/models/patient_changeset.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def processed!
188188
end
189189

190190
def patient
191+
return super if patient_id.present?
191192
@patient ||=
192193
if (existing_patient = existing_patients.first)
193194
prepare_patient_changes(existing_patient)
@@ -443,6 +444,7 @@ def changeset_type
443444

444445
def calculate_review_data!
445446
clear_review_data!
447+
reset_patient_id! if patient_id.present?
446448

447449
update_column(:record_type, changeset_type)
448450
return if new_patient?
@@ -466,4 +468,8 @@ def clear_review_data!
466468
data["review"] = { patient: {}, school_move: {} }
467469
save!
468470
end
471+
472+
def reset_patient_id!
473+
update_column(:patient_id, nil)
474+
end
469475
end

0 commit comments

Comments
 (0)