Skip to content

Commit 7a01255

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 2e59f01 commit 7a01255

7 files changed

Lines changed: 36 additions & 13 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ def perform(import_id)
1313
return
1414
end
1515

16+
patients_in_import_changesets = import.changesets.from_file.ready_for_review
17+
1618
patients_in_import =
17-
import.changesets.from_file.ready_for_review.map(&:patient) +
19+
Patient.joins(:changesets).merge(patients_in_import_changesets) +
1820
import.patients
1921

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

app/models/class_import.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,26 @@ 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_changesets =
60+
changesets.from_file.where.not(status: %i[cancelled processed])
61+
5962
patients_in_import =
60-
(changesets.from_file - changesets.cancelled - changesets.processed).map(
61-
&:patient
62-
)
63+
Patient.joins(:changesets).merge(patients_in_import_changesets)
6364

6465
unknown_patients = patients_to_create_moves_for(patients_in_import)
6566

6667
if Flipper.enabled?(:import_review_screen)
6768
valid_changesets =
6869
changesets.not_from_file.committing.where(
69-
patient_id: unknown_patients.pluck(:id)
70+
patient_id: unknown_patients.ids
7071
)
71-
valid_ids = valid_changesets.pluck(:id)
72-
valid_patients = Patient.where(id: valid_changesets.pluck(:patient_id))
72+
valid_patients = Patient.joins(:changesets).merge(valid_changesets)
7373

74-
changesets.not_from_file.committing.where.not(id: valid_ids).destroy_all
74+
changesets
75+
.not_from_file
76+
.committing
77+
.where.not(id: valid_changesets.ids)
78+
.destroy_all
7579

7680
missed_patients = unknown_patients - valid_patients
7781
if missed_patients.any? && (processed? || partially_processed?)

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)