Skip to content

The index reads every plan's tags once, not once per plan - #200

Merged
HamptonMakes merged 1 commit into
mainfrom
hampton/nice-proskuriakova-29a477
Aug 24, 2026
Merged

The index reads every plan's tags once, not once per plan#200
HamptonMakes merged 1 commit into
mainfrom
hampton/nice-proskuriakova-29a477

Conversation

@HamptonMakes

Copy link
Copy Markdown
Collaborator

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 the pluck the cause. The pluck isn't the cause.

What's actually going on

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. The whole story is that the API index never eager-loaded :tags.

Measured directly: five plans produce five CoPlan::Tag Pluck queries without the eager load, and none with it. Rewriting tag_names as tags.loaded? ? tags.map(&:name) : tags.pluck(:name) — the obvious-looking fix — changes nothing, because that branch is what pluck already does internally.

The change

  • :tags joins the index's eager loads in Api::V1::PlansController#index. That's the fix.
  • The comment on Plan#tag_names now names which of its two costs a caller is paying, so the next reader doesn't re-derive this.
  • A spec in the shape of the location one: the tag-table query count for thirty plans has to equal the count for three. Both specs now share the counter as 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 .pluck calls in the engine are on scoped relations rather than associations, or deliberate (AssignSlug's .lock.pluck is 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

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>
@HamptonMakes
HamptonMakes merged commit d6ea21b into main Aug 24, 2026
6 of 7 checks passed
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.

1 participant