Skip to content

Speed up NIRC last housing, project, and protocol lookups - #787

Merged
labkey-martyp merged 1 commit into
release26.7-SNAPSHOTfrom
26.7_fb_last_housing_perf
Nov 17, 2026
Merged

labkey-martyp merged 1 commit into
release26.7-SNAPSHOTfrom
26.7_fb_last_housing_perf

Conversation

@labkey-martyp

Copy link
Copy Markdown
Contributor

Rationale

This PR fixes a severe slowdown when the Necropsies grid (nirc_ehr.necropsyTasks) includes animalId.lastHousing, lastProject, or lastProtocol columns. Those queries joined each dataset to its own max(date) aggregate, which Postgres underestimated by ~3000x (13 rows vs. 40k) and executed as an unindexed nested loop: 14.4s on a production-sized database, ~13s of it in that one join.

Related Pull Requests

None.

Changes

  • Find each animal's latest housing, project, and protocol row with an anti-join instead of a self-join on the aggregate, which Postgres estimates and plans correctly.
  • enddate on these lookups is now the latest row's own end date rather than the largest end date across all rows, so an animal whose latest record is open now shows no end date.
  • Verified the housing rewrite against production data: it plans as a hash anti-join and returns the same 40,413 rows as before in 0.85s. The full Necropsies grid timing with the change has not been measured yet.

@labkey-martyp
labkey-martyp merged commit 3392b53 into release26.7-SNAPSHOT Nov 17, 2026
7 checks passed
@labkey-martyp
labkey-martyp deleted the 26.7_fb_last_housing_perf branch November 17, 2026 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants