Repository navigation
perf(meditations,lectures): eliminate virtual-field N+1 on related-* + for-audience reads - #545
Merged
Merged
Conversation
The related-meditations / related-lectures / for-audience endpoints fetched
their candidate pool at depth >= 1 with no `select`, so every row ran the
expensive per-row afterReads — meditations `tagAssignments` (~4 user-choices
queries/row) and the native lectures `clips` join — an N+1 that scaled with
the pool size.
Add a bounded include-mode `select`, co-located with each shape helper
(MEDITATION_CARD_SELECT in meditationShape, LECTURE_FEED_SELECT in
lectureShape), so those hooks are stripped before they run. Co-select each
virtual field's dependency (`duration` for `durationMinutes`,
`subtleSystemNodeWeights` for `title`). For a clip's nested `fullLecture`
parent — which `select` can't narrow, since relationship fields are
boolean-only — add `Lectures.defaultPopulate: { clips: false }`, mirroring
`Meditations.defaultPopulate: { tagAssignments: false }`.
Closes #541
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Two regression guards for the bounded-select fix: - related-meditations: spy on `payload.find` and assert zero user-choices / frames sub-queries fire as the daily pool doubles (before the fix this was 4 user-choices finds per candidate — 12 for 3 rows). - for-audience: assert the `clips` join is absent from candidate lectures (top-level select) and from a clip's nested `fullLecture` parent (defaultPopulate), while the parent's `metadata` still populates. A direct read is used as the baseline to show `clips` genuinely populates otherwise. Refs #541 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
) Add a rule section: any internal depth >= 1 read that shapes a pool of rows must carry a bounded `select`, or every row's per-row afterReads (virtual + native join fields) fire as an N+1. Covers co-locating the select with the shape helper, co-selecting virtual-field dependencies, using the target collection's `defaultPopulate` to bound expensive fields on populated relationships (which `select` can't narrow), and count-based regression tests. Refs #541 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Drops the local `findCollections` function for the inline `findSpy.mock.calls.map((c) => c[0].collection)` idiom already used in meditations.int.spec.ts, snapshotting per measurement (before/after). Refs #541 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
🚅 Deployed to the SahajCloud-pr-545 environment in sahajcloud
|
This was referenced Jul 5, 2026
Closed
Closed
Ardnived
added a commit
that referenced
this pull request
Jul 11, 2026
…pages) (#560) (#562) * perf(app-cards): eliminate pages N+1 on for-audience reads (#560) Bound the app-cards/for-audience endpoint's internal find to a select excluding expensive virtual fields, mirroring the #545 pattern for meditations/lectures endpoints. The unselected find at depth:1 ran every field's afterRead across all candidate cards, including relationships to pages, lectures, albums, and meditations — an N+1 scaled by the pool size. Add APP_CARD_SELECT (all non-virtual fields) and pass it to the find call. Destination relationships still populate at depth:1 but their expensive fields (pages.appUrl, etc.) are gated by defaultPopulate on target collections. Add regression test: spy on payload.find and assert pages are fetched in a single batched query, not N times per card. Fixes #560 (pages N+1 on for-audience). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * perf(meditations): optimize frames query in node weights computation (#560) Add select parameter to the frames find call in recomputeWeightsForMeditation to only fetch the subtleSystemNode relationship, avoiding unnecessary data fetching. This makes the frames query more efficient even though it's called once per meditation (not per-row N+1). Also add regression test on the meditation-lectures endpoint to verify frames queries stay bounded as pool size grows (≤2 queries for 1 meditation). Refs #560. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
depth ≥ 1with noselect, so every row ran the expensive per-rowafterReadhooks — meditationstagAssignments(~4 user-choices queries/row) and the native lecturesclipsjoin — an N+1 that scaled with pool size (related-meditationswas measured at 432 DB queries / 2.97 s for 25 rows in perf(meditations): eliminate virtual-field N+1 on ungated depth>=1 reads (related-* endpoints + REST clients) #541).selectto each internal candidate-pool read, co-located with the shape helper it feeds, so those hooks are stripped before they run. Applies torelated-meditations,related-lectures, and the siblingfor-audience(same N+1, shared helper).Changes
src/lib/meditations/meditationShape.ts— exportMEDITATION_CARD_SELECT(the fieldsshapeMeditationreads, incl. the co-selectedduration→durationMinutesandsubtleSystemNodeWeights→titledependencies).src/lib/lectures/lectureShape.ts— exportLECTURE_FEED_SELECT(fieldsshapeLecture+selectAudienceFeedread;fullLectureselected wholesale so a clip's parent still populates).src/collections/Lectures/endpoints/relatedMeditations.ts,Meditations/endpoints/lectures.ts,Lectures/endpoints/forAudience.ts— apply the bounded selects to theirpayload.findcandidate-pool reads.src/collections/Lectures/Lectures.ts—defaultPopulate: { clips: false }bounds theclipsjoin on a clip's nestedfullLectureparent (selectcan't narrow a relationship's fields). MirrorsMeditations.defaultPopulate: { tagAssignments: false }..claude/rules/endpoints.md— new section documenting how to avoid this class of N+1 on future endpoints.Test Results
(
lecture-related-meditations,meditation-lectures,lectures-for-audience)Regression guards (both verified to fail without the fix):
related-meditations: spy onpayload.find, assert zero user-choices/frames sub-queries as the pool doubles (was 4/row — 12 finds for 3 rows before the fix).for-audience: assert theclipsjoin is absent from candidate lectures (select) and from a clip's nestedfullLectureparent (defaultPopulate), with a direct-read baseline proving it otherwise populates.Notes for reviewer
defaultPopulateis a read-time projection, not a schema change.related-lecturesshares the exactLECTURE_FEED_SELECT+defaultPopulatemechanism asfor-audience; the clips-skip is regression-tested once (in the for-audience spec) rather than duplicated. Its own existing suite (89 passing across the 3 affected specs) covers shaping under the bounded select.tagAssignmentsfactory) — unnecessary for the acceptance criteria since Ticket #1: Sentry Integration and Production Environment Setup #1 skips those hooks entirely, and organic client REST reads are alreadyselect-gated byvalidateClientQueryParamsHook. Worth a follow-up only if an ungated direct meditation read at depth ≥ 1 shows up in prod traces.Closes #541
🤖 Generated with Claude Code