Skip to content

Commit f92ecf1

Browse files
TheOneFromNorwaythomasleese
authored andcommitted
Replace IN loop with EXIST
- EXIST makes use of Bitmap Index Scan on subquery eliminating ca. 500k loops
1 parent fcee053 commit f92ecf1

1 file changed

Lines changed: 4 additions & 16 deletions

File tree

app/policies/vaccination_record_policy.rb

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,16 @@ def resolve
6767
team = user.selected_team
6868
return scope.none if team.nil?
6969

70-
patient_subquery =
70+
relevant_patients =
7171
Patient
72+
.select("1")
7273
.joins(patient_sessions: :session)
73-
.select(:id)
74-
.distinct
74+
.where("patients.id = vaccination_records.patient_id")
7575
.where(sessions: { team_id: team.id })
7676
.arel
77-
.as("patients")
7877
scope
79-
.joins(
80-
VaccinationRecord
81-
.arel_table
82-
.join(patient_subquery, Arel::Nodes::OuterJoin)
83-
.on(
84-
VaccinationRecord.arel_table[:patient_id].eq(
85-
patient_subquery[:id]
86-
)
87-
)
88-
.join_sources
89-
)
9078
.kept
91-
.where(patient_subquery[:id].not_eq(nil))
79+
.where(relevant_patients.exists)
9280
.or(scope.kept.where(session: team.sessions))
9381
.or(
9482
scope.kept.where(

0 commit comments

Comments
 (0)