Repository navigation
fix(core): stop taxonomy listings walking the collection for small terms - #3402
Conversation
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 detectedLatest commit: 0970e7e The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
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 |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
Overlapping PRsThis PR modifies files that are also changed by other open PRs: This may cause merge conflicts or duplicated work. A maintainer will coordinate. |
There was a problem hiding this comment.
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, andbuildTaxonomyPivotQueryinpackages/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
driveFromContentis only set when the strict single-large-term/indexed-sort/LIMIT conditions hold; every other shape falls back to the pivot-seekingCROSS JOIN. - Checked that the
CROSS JOINpath is SQLite-only (isPostgres(db)forcesJOIN); 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 JOINSQL 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.
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.
| "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. |
There was a problem hiding this comment.
in the future, could you please ensure all changeset are simpler and a bit easier to understand? as it's a public facing changesets.
There was a problem hiding this comment.
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.
… 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.
… 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.
What does this PR do?
Stops taxonomy-filtered listings sorted by
published_atorcreated_atfrom reading up to the whole collection for a small term. Since #3300 these sorts join the pivot with a plainJOIN, so withoutANALYZEstatistics, which EmDash never collects, the planner walks theec_*sort index for every term, as #1834 reported. #3409 put the pin back forupdated_atand 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
LIMITkeeps the walk, stopping at a full page. Smaller terms, several slugs or taxonomies, and listings without aLIMITseek the pivot again as before #3300. The query count, Postgres and the sorts #3409 pinned are unchanged.Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain. (n/a: no admin UI change)AI-generated code disclosure
Screenshots / test output
Not applicable for screenshots. On current
main, 8 of the 14 plan tests inloader-taxonomy-pivot-plan.test.tsfail: every small-term shape underpublished_atorcreated_at, and a large term with a second slug, a second taxonomy, noLIMIT, or one slug that names terms in two locales. A small term underpublished_atno longer gets the plainJOIN, so #3409's join test now runs against a large term.Rows read on local D1 (miniflare) with 20,000 published posts,
published_atdescending, limit 30, listing plus term lookup:mainThe 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.