Skip to content

Add indexes to more patient join tables#6737

Merged
alistair-white-horne-tng merged 3 commits intonextfrom
alistair/patient-deleter-add-indexes
Apr 30, 2026
Merged

Add indexes to more patient join tables#6737
alistair-white-horne-tng merged 3 commits intonextfrom
alistair/patient-deleter-add-indexes

Conversation

@alistair-white-horne-tng
Copy link
Copy Markdown
Contributor

@alistair-white-horne-tng alistair-white-horne-tng commented Apr 30, 2026

These missing indexes were causing patient deletion queries to be
extremely slow.

Deleting a patient used to trigger sequential scans on each of these
tables, in order to enforce the FK constraint on patient_id.

Testing in data replication shows that patient delete queries reduce
from 100s of millisecs to 1s of ms; 2 orders of magnitude increase.

Also:

  • This change adds logging for each step of the patient deletion process
  • And uses find_each when deleting Patients to mitigate the memory issues which arise when deleting a lot of patients (console crashed with >15,000)

MAV-7304

We are seeing severe performance issues with calling this class. This
logging will be the first step towards allowing us to diagnose these problems.

Jira-Issue: MAV-7304
These missing indexes were causing patient deletion queries to be
extremely slow.

Deleting a patient used to trigger sequential scans on each of these
tables, in order to enforce the FK constraint on `patient_id`.

Testing in data replication shows that patient delete queries reduce
from 100s of millisecs to 1s of ms; 2 orders of magnitude increase.

Jira-Issue: MAV-7304
@alistair-white-horne-tng alistair-white-horne-tng force-pushed the alistair/patient-deleter-add-indexes branch from 02b6421 to b77eb52 Compare April 30, 2026 10:08
For a large number of patients (>15,000), the `@patients.each(&:destroy)`
is causing memory issues. Changing this to `find_each` should mitigate
this issue.

This issue was also present when destroying `VaccinationRecord`s in
`bin/mavis teams reset-national-reporting`

Jira-Issue: MAV-7304
@alistair-white-horne-tng alistair-white-horne-tng force-pushed the alistair/patient-deleter-add-indexes branch from b77eb52 to dbdc3b0 Compare April 30, 2026 10:51
@alistair-white-horne-tng alistair-white-horne-tng marked this pull request as ready for review April 30, 2026 13:21
@alistair-white-horne-tng alistair-white-horne-tng requested a review from a team as a code owner April 30, 2026 13:21
@alistair-white-horne-tng alistair-white-horne-tng merged commit 7de67c7 into next Apr 30, 2026
32 checks passed
@alistair-white-horne-tng alistair-white-horne-tng deleted the alistair/patient-deleter-add-indexes branch April 30, 2026 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants