Skip to content

Index CurrentCycle's join-table lookups - #528

Merged
tmfrnz merged 1 commit into
sg-dev-fix-archived-sibling-currentfrom
sg-dev-index-join-tables
Nov 29, 2026
Merged

tmfrnz merged 1 commit into
sg-dev-fix-archived-sibling-currentfrom
sg-dev-index-join-tables

Conversation

@parndt

@parndt parndt commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

recommendation_categories and measure_indicators back CurrentCycle's hot queries but, unlike recommendation_measures, were never indexed on either foreign key - every lookup by category_id, recommendation_id, measure_id or indicator_id was a sequential scan over the whole table.

@parndt
parndt requested a review from tmfrnz September 1, 2026 23:27

@tmfrnz tmfrnz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct - both tables only had updated_by_id indexed, and stale_parents filters and plucks on exactly these four columns.

Two notes, neither blocking.

The description says every lookup "was a sequential scan." That states what the planner did; what the schema shows is that no index was available. Postgres will likely still choose sequential scans here - recommendation_categories has 2655 rows and measure_indicators has 0 on sg-production, both small enough that a sequential scan beats an index lookup plus heap fetches. So this reads as schema correctness that matters as the tables grow, rather than a speedup today, and I'd frame it that way.

stale_parents filters on one column and plucks the other in both directions, so composites ([category_id, recommendation_id] plus the reverse, same for measure_indicators) would allow index-only scans and subsume two of these four - same number of indexes, just paired. Not worth changing at this size, noting it for later.

@tmfrnz
tmfrnz force-pushed the sg-dev-index-join-tables branch from 2b70a9d to 262bb0b Compare November 27, 2026 16:19
recommendation_categories and measure_indicators back CurrentCycle's hot
queries but, unlike recommendation_measures, were never indexed on either
foreign key - every lookup by category_id, recommendation_id, measure_id
or indicator_id was a sequential scan over the whole table.
@tmfrnz
tmfrnz force-pushed the sg-dev-index-join-tables branch from 262bb0b to ff89109 Compare November 27, 2026 16:32
tmfrnz added a commit that referenced this pull request Nov 29, 2026
…552)

Brings the `is_current` optimisation stack from sg-dev (#524, #525,
#529, #528, #526) to the NZ branch. The 12 commits are cherry-picked
unchanged apart from conflict resolutions, plus one NZ-specific spec
fix.

## What it does
- `is_current` for recommendations, measures, indicators and progress
reports is resolved once per request by `CurrentCycle` instead of per
record (N+1 at every hop, run twice per request: filter + serialiser).
`Category#is_current` itself is unchanged in approach and runs once per
reporting-cycle category.
- Ordering of sibling cycles is well defined: undated cycles sort last,
ties break on id (#525).
- Archived cycle categories are excluded from the sibling comparison and
are never current themselves (#529).
- Indexes on `recommendation_categories` and `measure_indicators`
foreign keys (#528).
- `ResetsCurrentCycle` invalidates the per-request resolver on relevant
writes only.

## NZ-specific changes
- Conflict resolutions: NZ's category factory has no `:published` trait
(draft defaults to false here); NZ's
`only_manager_and_admin_users_can_be_assigned` validation kept.
- Request specs sign in with the factory password and only stub
`enable_mfa` where it exists.

## Tested on nz-uat
Response sizes unchanged to the byte. Sibling logic additionally
verified with test data (newest wins, drafts current, undated cycle no
longer unseats dated ones, archived runner-up promoted, recs without a
cycle stay current, propagation to measures/indicators), and cache
invalidation confirmed in the running app.

## User-visible changes
None expected with current NZ data (one cycle per treaty). In general:
- An undated cycle no longer makes its dated siblings non-current.
- Archived cycles, and recommendations linked only to them, drop out of
`current_only`.
- Progress reports without an indicator are excluded from `current_only`
instead of erroring.

---------

Co-authored-by: Philip Arndt <git@p.arndt.io>
@tmfrnz
tmfrnz merged commit bca96df into sg-dev Nov 29, 2026
3 checks passed
@tmfrnz
tmfrnz deleted the sg-dev-index-join-tables branch November 29, 2026 15:14
tmfrnz added a commit that referenced this pull request Nov 29, 2026
) (#554)

Releases the `is_current` optimisation stack (#524, #525, #529, #528,
#526) from sg-dev to sg-production. Replaces #553.

## Why a release branch
sg-production received the security changes in #533 as a single squash
commit, so git did not recognise sg-dev's individual commits as already
released and #553 showed 32 commits with conflicts. This branch is
sg-dev plus a `-s ours` merge of sg-production, which records that
sg-production is contained without changing any content. The diff
against sg-production is the stack only.

## What changes
- `is_current` is resolved once per request by `CurrentCycle` instead of
per record and per serialisation (#526).
- Undated reporting cycles no longer make their dated siblings
non-current (#525).
- Archived cycle categories drop out of the sibling comparison and are
never current themselves (#529).
- Indexes on `recommendation_categories` and `measure_indicators`
foreign keys (#528).
- Tests characterising `Category#is_current` and the propagation chain
(#524).

## Tested on sg-uat
Same change verified on nz-uat and nz-production (recommendations,
measures, indicators): identical results, 15–40× faster in production.

## User-visible changes
None expected with current data. In general: an undated cycle no longer
hides its dated siblings' recommendations, and archived cycles (and
recommendations linked only to them) drop out of `current_only`.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Philip Arndt <git@p.arndt.io>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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