Skip to content

perf(core): drive two-taxonomy pivot queries from the smallest filter - #4643

Closed
swissky wants to merge 2 commits into
emdash-cms:mainfrom
swissky:perf/taxonomy-pivot-selective
Closed

swissky wants to merge 2 commits into
emdash-cms:mainfrom
swissky:perf/taxonomy-pivot-selective

Conversation

@swissky

@swissky swissky commented Nov 4, 2026

Copy link
Copy Markdown
Member

What does this PR do?

On SQLite and D1, a collection listing filtered by two or more taxonomies seeks the first filter on the content_taxonomies pivot and checks every other filter with a per-candidate EXISTS. The first filter was whichever key the caller wrote first in where, so { topics, places } and { places, topics } read very different row counts for the same result.

The term lookup that already runs for each taxonomy filter now also counts that filter's pivot rows in the collection, capped at 200 index entries, inside the same query. The filters are then sorted by that count, so the smallest one drives the seek. No query is added; the per-listing query count is unchanged.

  • The cap is shared across a filter's slugs, so a long slug list reads about as much as one slug.
  • Each later filter is counted only up to one past the smallest count so far.
  • A group that exceeds its share of the cap counts as the whole cap. A multi-slug filter with one large term is therefore never mistaken for a small filter.
  • Single-taxonomy listings keep their existing probe and walk/seek choice. Postgres is unchanged and keeps the where order.

This covers part 1 of the issue. Part 2 (a content-field filter drops the pivot path) is not in this PR.

Refs #3259

Type of change

  • Bug fix
  • Feature (requires an accepted feature plan or RFC)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Your connection to this change

  • I use EmDash and ran into the problem this PR addresses
  • I maintain a tool, dependency, or service that EmDash uses, and this change concerns that integration
  • A Maintainer approved this specific change before I opened the PR

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main.
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to their merged design PR: https://github.com/emdash-cms/emdash/pull/...
  • I have included screenshots below if this PR changes the UI

i18n, design PR and screenshots do not apply: no admin UI change, performance change only.

AI assistance disclosure

  • This PR includes AI-generated code or text — model/tool: Claude Opus 5.5

Screenshots / test output

Not applicable (no UI change).

Measured with D1 meta.rows_read in the new tests/workerd/loader-taxonomy-pivot-order-d1.test.ts (local D1 through @cloudflare/vitest-pool-workers). The fixture has 1,500 published entries: category: broad tags 750, category: rare 5 and tag: narrow 30. Each listing has limit: 10, returns the same 10 ids in both orders and runs 3 queries in both orders, before and after.

Listing where order Rows read before Rows read after
published_at sort { category: "broad", tag: "narrow" } 995 505
{ tag: "narrow", category: "broad" } 274 336
updated_at sort (temp-sort path) { category: "broad", tag: "narrow" } 1,200 710
{ tag: "narrow", category: "broad" } 479 541
published_at sort, two slugs { category: ["rare", "broad"], tag: "narrow" } 1,056 445
{ tag: "narrow", category: ["rare", "broad"] } 308 361

The worst order reads about half as many rows as before. The best order now pays for the size probe: up to about 200 index entries for the first filter and one past the smallest count for each later one. The gap between the orders is now bounded by that probe instead of by the size of the broadest term. When every taxonomy filter has 200 or more assignments, the filters tie and the where order still decides, as it did before.

The test fails on main in all three cases (gap of 721, 721 and 748 rows against a bound of 300). With the cap guard removed, it fails in the two-slug case (664).

  • packages/core: 858 test files, 10,379 tests passed; workerd suite 45 files, 206 tests passed, including the existing pivot plan tests (loader-taxonomy-pivot-plan), which are unchanged.
  • pnpm build, pnpm typecheck, pnpm format clean; pnpm lint:json reports 0 diagnostics.

@github-actions github-actions Bot added the review/needs-review No maintainer or bot review yet label Nov 4, 2026
@changeset-bot

changeset-bot Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b51ba37

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/plugin-test Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
create-emdash Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The approach fits the performance problem. Capped size counts piggyback on the existing resolveTermGroups query, so no extra round trips are added. Sorting groupSets lets the smallest filter drive the pivot seek. Both reviewers found no result-correctness regression. Postgres and single-taxonomy behavior are unchanged, and the new D1 test measures rows_read for both key orders. One finding remains: the changeset overstates how exact the smallest-filter choice is for multi-slug filters, since the estimates are conservative.


Reviewed by anthropic/claude-opus-5-5 and openai/gpt-6.1-sol.

"emdash": patch
---

Speeds up `getEmDashCollection()` listings on SQLite and D1 that filter by two or more taxonomies and no content fields. When one of the taxonomy filters has fewer than 200 assigned entries in the collection, the listing now starts from the filter with the fewest entries instead of the taxonomy the `where` object names first, so a broad taxonomy listed first no longer makes the listing read every entry assigned to it. To pick the starting filter, the term lookup reads up to about 200 extra index entries per taxonomy filter. Postgres listings are unchanged.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[needs fixing] The claim that any filter below 200 assignments makes the listing start from the filter with the fewest entries is not guaranteed by the implementation. For example, a first filter with two slugs having 150 and 5 assignments gets a per-group cap of 100 and is therefore estimated as 200 (loader.ts:1282,1313–1317). A second, single-slug filter with 180 assignments is then chosen over that smaller 155-assignment filter. This is a reasonable conservative heuristic, but the release note currently describes exact selection. Describe selection using bounded estimates and qualify the multi-slug case rather than promising the smallest filter whenever one falls below 200. (Only the openai reviewer raised this; the anthropic reviewer checked the cap logic and found it correct without noting the wording issue.)

@pkg-pr-new

pkg-pr-new Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@4643

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@4643

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@4643

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@4643

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@4643

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@4643

emdash

npm i https://pkg.pr.new/emdash@4643

create-emdash

npm i https://pkg.pr.new/create-emdash@4643

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@4643

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@4643

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@4643

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@4643

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@4643

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@4643

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@4643

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@4643

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@4643

upgrade-emdash

npm i https://pkg.pr.new/upgrade-emdash@4643

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@4643

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@4643

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@4643

@emdash-cms/plugin-ai-search

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-search@4643

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@4643

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@4643

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@4643

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@4643

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@4643

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@4643

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@4643

commit: b51ba37

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The approach remains appropriate for the taxonomy-query performance problem. This follow-up only revises the changeset, and it addresses the prior finding: selection is now described in terms of capped estimates, with an explicit caveat for multi-slug filters rather than a guarantee of choosing the exact smallest filter.

I checked the revised wording against the probe and sorting implementation and the changeset documentation standards. No outstanding findings or new issues in this follow-up. Review was static; no checks were run.


Follow-up review by openai/gpt-6.1-sol, scoped to the changes since the last review.

@danielmlr danielmlr 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.

Two small things, neither blocking:

  1. The new D1 test only compares the two where orders with each other. If the sort ever picked the bigger filter, both orders would start from broad and read about the same, so the test would still pass. Also checking that each order reads fewer rows than the 750 entries tagged broad would catch that.
  2. The changeset says the lookup reads up to about 200 extra index entries per filter. With a long slug list, each slug's count reads its own entries, so the extra reads grow with the list instead of staying near 200. The docstring line "a long slug list reads about as much as a single slug" is off in the same way, so I'd fix or drop it. I'd keep the changeset to what a site owner notices, something like:
Speeds up `getEmDashCollection()` listings on SQLite and D1 that filter by two or more taxonomies and no content fields. The listing now usually starts from the smallest taxonomy filter instead of the one written first in `where`, so a broad taxonomy listed first no longer slows it down.

@emdashbot emdashbot Bot added review/needs-review No maintainer or bot review yet and removed review/needs-review No maintainer or bot review yet labels Nov 4, 2026
@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Nov 4, 2026
@swissky

swissky commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

Closing in favor of #3266 by @eisenbruch, which takes the same approach (capped count inside the term lookup, no extra round trip) and was opened six weeks earlier. Sorry for the overlap, I missed it when I picked up #3259. The field-filter case of #3259 is not covered by either PR and will follow separately.

~ 🤖 Claude Opus 5.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants