Skip to content

Commit 2cb9691

Browse files
Improve performance of lookup by ensuring patient results have hash
- This is guaranteed by forcing a JOIN instead of an IN clause on an indexed column - In testing this reduces execution time from ca 60s to 1s
1 parent ca74c7e commit 2cb9691

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

app/policies/vaccination_record_policy.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,28 @@ def resolve
6464
team = user.selected_team
6565
return scope.none if team.nil?
6666

67+
patient_subquery =
68+
Patient
69+
.joins(patient_sessions: :session)
70+
.select(:id)
71+
.distinct
72+
.where(sessions: { team_id: team.id })
73+
.arel
74+
.as("patients")
6775
scope
76+
.joins(
77+
VaccinationRecord
78+
.arel_table
79+
.join(patient_subquery, Arel::Nodes::OuterJoin)
80+
.on(
81+
VaccinationRecord.arel_table[:patient_id].eq(
82+
patient_subquery[:id]
83+
)
84+
)
85+
.join_sources
86+
)
6887
.kept
69-
.where(patient: team.patients)
88+
.where(patient_subquery[:id].not_eq(nil))
7089
.or(scope.kept.where(session: team.sessions))
7190
.or(
7291
scope.kept.where(

0 commit comments

Comments
 (0)