The index reads every plan's tags once, not once per plan - #200
Merged
Conversation
The last thing #196 left on the table. Its location preload had to count only the location tables, because counting everything would have failed on `tag_names` — which was doing a query a row — and the number would then have been bumped to whatever made it pass. That note called the pluck the cause and left it alone. The pluck isn't the cause. `pluck` on a relation that's already loaded reads the loaded records instead of querying; it has since Rails 6. So `tag_names` is free on a plan whose tags are loaded and one query on a plan whose tags aren't, and the whole story is that the API index never eager-loaded `:tags`. Five plans, five `Tag Pluck` queries; add `:tags` to the includes and there are none. Every other list endpoint that reads tags — the libraries organization API, the workspace index, home, search — already preloads them, which is why this only ever showed up here. So: `:tags` joins the index's eager loads, and the comment on tag_names now says which of its two costs a caller is paying, so the next reader doesn't re-derive this the way I just did. The spec is the location one's twin — tag-table query count for thirty plans equals the count for three, plus the assertion that each plan still gets its own tags rather than a neighbour's, since a preload can be cheap and wrong. Both now share the counter as a helper. It fails without the includes: thirty queries against three. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
The last N+1 on
GET /api/v1/plans, and a correction to what #196 said about it.That PR's location preload had to count only the location tables, because counting every query would have failed on
tag_names— which was doing a query a row — and the number would then have been bumped to whatever made it pass. The note it left called thepluckthe cause. Thepluckisn't the cause.What's actually going on
pluckon a relation that's already loaded reads the loaded records instead of querying — it has since Rails 6. Sotag_namesis free on a plan whose tags are loaded, and one query on a plan whose tags aren't. The whole story is that the API index never eager-loaded:tags.Measured directly: five plans produce five
CoPlan::Tag Pluckqueries without the eager load, and none with it. Rewritingtag_namesastags.loaded? ? tags.map(&:name) : tags.pluck(:name)— the obvious-looking fix — changes nothing, because that branch is whatpluckalready does internally.The change
:tagsjoins the index's eager loads inApi::V1::PlansController#index. That's the fix.Plan#tag_namesnow names which of its two costs a caller is paying, so the next reader doesn't re-derive this.index_queries_touching(pattern).The sweep
Every other list endpoint that reads tags already preloads them — the libraries organization API (both the filed and unfiled paths), the workspace index, home's feed, and search. Which is why this only ever showed up on the API index. The remaining
.pluckcalls in the engine are on scoped relations rather than associations, or deliberate (AssignSlug's.lock.pluckis taking row locks).Verification
The new spec fails without the includes — thirty tag queries against three — and passes with it. It also asserts each plan still gets its own tags rather than a neighbour's, since a preload can be cheap and wrong. Full suite green: 1803 examples, 0 failures. Rubocop clean.
🤖 Generated with Claude Code