Skip to content

fix(core): stop taxonomy listings walking the collection for small terms - #3402

Merged
khoinguyenpham04 merged 6 commits into
emdash-cms:mainfrom
danielmlr:fix/taxonomy-pivot-sparse-terms
Sep 30, 2026
Merged

khoinguyenpham04 merged 6 commits into
emdash-cms:mainfrom
danielmlr:fix/taxonomy-pivot-sparse-terms

Conversation

@danielmlr

@danielmlr danielmlr commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Stops taxonomy-filtered listings sorted by published_at or created_at from reading up to the whole collection for a small term. Since #3300 these sorts join the pivot with a plain JOIN, so without ANALYZE statistics, which EmDash never collects, the planner walks the ec_* sort index for every term, as #1834 reported. #3409 put the pin back for updated_at and field sorts only.

The term lookup now also reads, capped at 200 pivot rows in the same query, whether the term is large. Only a single large term with a LIMIT keeps the walk, stopping at a full page. Smaller terms, several slugs or taxonomies, and listings without a LIMIT seek the pivot again as before #3300. The query count, Postgres and the sorts #3409 pinned are unchanged.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

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. (n/a: no admin UI change)
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to an approved Discussion: https://github.com/emdash-cms/emdash/discussions/... (n/a: bug fix)
  • I have included screenshots below if this PR changes the UI (n/a: no UI change)

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 5.5 (Claude Code)

Screenshots / test output

Not applicable for screenshots. On current main, 8 of the 14 plan tests in loader-taxonomy-pivot-plan.test.ts fail: every small-term shape under published_at or created_at, and a large term with a second slug, a second taxonomy, no LIMIT, or one slug that names terms in two locales. A small term under published_at no longer gets the plain JOIN, so #3409's join test now runs against a large term.

Rows read on local D1 (miniflare) with 20,000 published posts, published_at descending, limit 30, listing plus term lookup:

Posts on the term before #3300 main this PR
19,000 76,222 285 486
1,000 4,220 852 1,053
2 37 20,030 40
1 (the oldest post) 18 20,015 20

The walk overtakes the seek at about 90 posts in a 1,000-post collection, 200 in 5,000 and 400 in 20,000.

Deeper pages and byline filters on a large term keep the walk, which reads at most about the collection plus the term's entries, while the seek grows with the term: the last page of an 800-post term reads 21,503 rows against 3,904 with the seek, and of a 12,800-post term 33,103 against 51,503.

The 847,162 rows in #3299 reproduce only without idx_ec_posts_tg_locale: with the reporter's data shape and 0.38.0 indexes, the pivot seek reads 4,954 rows with it and 846,628 without it.

The plain JOIN in the pivot listing lets SQLite drive every taxonomy
listing from the ec_* sort index. That fills a page quickly for a term
with many entries, but for a small term it reads up to the whole
collection, and the same happens for updated_at sorts and for listings
that combine several terms or taxonomies.

The term lookup now carries a capped count of the term's pivot rows.
Only a single term with at least 200 of them, under a published_at or
created_at sort with a LIMIT, walks ec_*. Every other listing seeks the
pivot again, as it did before the plain JOIN. The count rides in the
existing lookup query, so the query count is unchanged.

Measured on local D1 with 20,000 posts, listing plus term lookup: a
2-post term reads 40 rows instead of 20,030, a 19,000-post term 486
instead of 285.
@changeset-bot

changeset-bot Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0970e7e

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

@pkg-pr-new

pkg-pr-new Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/auth-atproto

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

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

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

emdash

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

create-emdash

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

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

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

@emdash-cms/plugin-cli

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

@emdash-cms/plugin-test

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

@emdash-cms/plugin-types

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

@emdash-cms/registry-client

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

@emdash-cms/registry-lexicons

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

@emdash-cms/registry-loader

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

@emdash-cms/registry-moderation

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

@emdash-cms/registry-verification

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

@emdash-cms/sandbox-workerd

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-field-kit

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: 0970e7e

@github-actions

Copy link
Copy Markdown
Contributor

Overlapping PRs

This PR modifies files that are also changed by other open PRs:

This may cause merge conflicts or duplicated work. A maintainer will coordinate.

@danielmlr
danielmlr marked this pull request as ready for review September 25, 2026 00:56
@github-actions github-actions Bot added the review/needs-review No maintainer or bot review yet label Sep 25, 2026

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

This is the right change for the reported regression. Since #3300 switched the taxonomy pivot CTE to a plain JOIN, D1/SQLite's stats-blind planner has been free to drive from the ec_* sort index and probe the pivot per row, turning small-term listings into full-collection walks. The PR restores the pre-#3300 pivot-seek behavior for the cases where seeking is cheaper (small terms, updated_at/non-indexed sorts, multi-slug/multi-taxonomy filters) while keeping the content-index walk only for the single case where it actually wins: one large term (≥200 assignments), indexed published_at/created_at sort, with a LIMIT.

What I checked:

  • Read the full diff, AGENTS.md, and .changeset/README.md.
  • Traced resolveTermGroups, isIndexedPivotSort, and buildTaxonomyPivotQuery in packages/core/src/loader.ts.
  • Verified that the size probe is folded into the existing term-lookup query (no additional logged-out round trip), capped at 200 pivot index rows via LIMIT 1 OFFSET 199.
  • Confirmed driveFromContent is only set when the strict single-large-term/indexed-sort/LIMIT conditions hold; every other shape falls back to the pivot-seeking CROSS JOIN.
  • Checked that the CROSS JOIN path is SQLite-only (isPostgres(db) forces JOIN); Postgres behavior is unchanged.
  • Reviewed the updated plan tests in packages/core/tests/integration/loader-taxonomy-pivot-plan.test.ts — they cover the boundary (199 vs 200 assignments), all the small-term shapes, and the large-term exceptions.
  • Confirmed the query-count snapshots do not add queries, only reflect the JOIN→CROSS JOIN SQL change on the same taxonomy-listing query.

The implementation is careful: no SQL interpolation, locale filtering on ec_* is preserved, the term lookup is unchanged for multi-slug cases ( avoiding a wasted probe), and the LARGE_TERM_ASSIGNMENTS constant is exported only for the regression test, not re-exported from the package index. I found no logic bugs, regressions, AGENTS.md violations, or missing tests. The changeset is user-facing and accurately describes the new behavior.

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Sep 25, 2026
@danielmlr danielmlr added the ci:run Approve and re-run CI on the PR's latest commit label Sep 25, 2026
@github-actions github-actions Bot removed the ci:run Approve and re-run CI on the PR's latest commit label Sep 25, 2026
emdash-cms#3409 pinned the pivot join for temp sorts and freed it for every indexed
sort. This branch frees it only for a single large term under an indexed
sort with a LIMIT, which already keeps temp sorts pinned, so its join
condition stays. The comment keeps emdash-cms#3409's reason for the temp-sort pin
and drops the claim that D1 keeps no planner statistics, which emdash-cms#3409's
production statistics contradict. emdash-cms#3409's join test moves into the
large-term block, the only shape whose indexed sort still frees the join.

emdash-cms#3510 replaced better-sqlite3, so the plan test uses the node:sqlite
helper.
emdash-cms#3409 pinned the pivot join for updated_at and field sorts on main, so
those listings no longer walk the collection and this change no longer
fixes them. The entry now names only the indexed sorts it changes, and
created_at as the default, so it does not repeat emdash-cms#3409's entry in the
same release.
The size probe counts a term's pivot rows in the listed collection only.
Without that condition, a term with 200 or more assignments in another
collection would count as large and walk the whole collection for a
listing where it tags a single entry. No plan test failed when the
condition was removed; this one does, and it fails on main as well.
…tatistics claim

The join comment explained the SQLite choice only, leaving `isPostgres(db)`
in the condition unexplained; Postgres rejects `CROSS JOIN ... ON`, as the
byline fold in the same file already notes.

The plan tests claimed D1 never keeps sqlite_stat1 and that their plans
match D1 exactly. A D1 database has statistics once its operator runs
ANALYZE or PRAGMA optimize (emdash-cms#3408 measured one). EmDash never runs either,
which is why the tests run without them.
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/approved Approved; no new commits since labels Sep 27, 2026

@khoinguyenpham04 khoinguyenpham04 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

"emdash": patch
---

Fixes taxonomy-filtered `getEmDashCollection()` listings sorted by `published_at` or `created_at` (the default) on D1 and SQLite reading up to the whole collection when the term has fewer than 200 entries or the listing filters by several terms. These listings now read only the term's entries. A single term with 200 or more entries is still read in date order and stops once the page is full.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

in the future, could you please ensure all changeset are simpler and a bit easier to understand? as it's a public facing changesets.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fair point. That entry packed every condition into the first sentence and described the query instead of what a site owner sees. I'll lead with the visible effect and keep the case analysis in the PR body from now on.

@emdashbot emdashbot Bot added the review/approved Approved; no new commits since label Sep 30, 2026
@khoinguyenpham04
khoinguyenpham04 merged commit f925a89 into emdash-cms:main Sep 30, 2026
44 checks passed
@emdashbot emdashbot Bot removed the review/needs-rereview New commits since the last review label Sep 30, 2026
@emdashbot emdashbot Bot mentioned this pull request Sep 30, 2026
danielmlr added a commit to danielmlr/emdash that referenced this pull request Sep 30, 2026
… small terms

The taxonomy cases in loader-cursor-plan.test.ts tag 40 posts with one
term and assert that every page walks the ec_post published_at index.
Since the small-term change (emdash-cms#3402), the loader walks ec_post only for a
term with at least LARGE_TERM_ASSIGNMENTS (200) pivot rows and seeks a
smaller term on the pivot, sorting just its entries. Both changes were
green on their own bases; together they fail both taxonomy cases on
main.

The fixture now creates LARGE_TERM_ASSIGNMENTS posts, so the taxonomy
cases keep exercising the walk this test guards, including the seek on
descending pages that continue from an undated entry. Small terms stay
covered by loader-taxonomy-pivot-plan.test.ts.
khoinguyenpham04 pushed a commit that referenced this pull request Sep 30, 2026
… small terms (#3703)

* test(core): stop the cursor-plan test expecting a collection walk for small terms

The taxonomy cases in loader-cursor-plan.test.ts tag 40 posts with one
term and assert that every page walks the ec_post published_at index.
Since the small-term change (#3402), the loader walks ec_post only for a
term with at least LARGE_TERM_ASSIGNMENTS (200) pivot rows and seeks a
smaller term on the pivot, sorting just its entries. Both changes were
green on their own bases; together they fail both taxonomy cases on
main.

The fixture now creates LARGE_TERM_ASSIGNMENTS posts, so the taxonomy
cases keep exercising the walk this test guards, including the seek on
descending pages that continue from an undated entry. Small terms stay
covered by loader-taxonomy-pivot-plan.test.ts.

* test(core): count cursor-plan pages for any large-term threshold

The fixture follows LARGE_TERM_ASSIGNMENTS, but the expected page count
divided it by the page size, which is exact only while the page size
divides the threshold. The last page can be partial, so the count now
rounds up and a retuned threshold keeps the test on what it guards.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core review/approved Approved; no new commits since size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants