perf: fix N+1 queries in notification jobs and discussion update serialization#96
Open
perf: fix N+1 queries in notification jobs and discussion update serialization#96
Conversation
…alization
In all three notification jobs, `stateFor($user)` was called inside
`reject()` callbacks — once per user per tag, resulting in N×T queries.
Replace with a single `TagState::whereIn()` pre-load keyed by user ID,
via a new `preloadTagStates()` helper on `NotificationJob`.
Also adds an `eagerLoadWhere` for the `tags` relation on the
`DiscussionResource` `Update` endpoint, closing a gap left by
`flarum/tags` which only covers `Index`, `Show`, and `Create`. Without
this, the `subscription` field getter fell back to `stateFor()` per tag
on discussion edit responses.
Confirmed via Clockwork: PATCH /api/discussions/{id} with 3 tags now
runs 0 tag_user state queries (was 3×) against the Update endpoint.
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
stateFor($user)was called insidereject()callbacks in all three notification job classes — once per user per tag (N×T queries). Replaced with a singleTagState::whereIn()pre-load via a newpreloadTagStates()helper onNotificationJob, reducing to 1 query regardless of user/tag count.flarum/tagseager-loads tag state viawithStateFor()forIndex,Show, andCreateonDiscussionResource— but notUpdate. Thesubscriptionfield getter was falling back tostateFor()per tag on discussion edit responses. AddedeagerLoadWhere('tags', withStateFor(...))for theUpdateendpoint to close this gap.Confirmed via Clockwork
PATCH /api/discussions/{id}with 3 tags: 0tag_userstate queries (was 3×, one per tag). Notification job fix is correct by inspection — async queue jobs are not captured in HTTP Clockwork entries.What remains unfixed (by design)
whereVisibleTo($user)andisVisibleTo($user)inreject()remain as per-user queries — these cannot be batched without framework changes.