-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(core): drive taxonomy term counts from the pivot #2238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MA2153
wants to merge
4
commits into
emdash-cms:main
Choose a base branch
from
MA2153:fix/2237-term-count-join-order
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
189ed89
fix(core): drive taxonomy term counts from the pivot
MA2153 427aa82
ci: update query-count snapshots
emdashbot[bot] b10d84a
fix(core): trim narrative/issue-referencing comments per review
MA2153 baafe3c
fix(core): tighten changeset and comment per review
MA2153 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "emdash": patch | ||
| --- | ||
|
|
||
| Fixes taxonomy term counts reading a near-quadratic number of rows on sites with many entries and terms, causing multi-second delays on pages that render term counts or taxonomy filters. Counts are unchanged. |
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
111 changes: 111 additions & 0 deletions
111
packages/core/tests/integration/taxonomy-term-counts-plan.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /** | ||
| * SQLite query-plan regression guard for the consolidated term-count query. | ||
| * Output correctness is covered by unit/taxonomies/term-counts; this asserts | ||
| * the planner drives from content_taxonomies, not from ec_*. | ||
| */ | ||
|
|
||
| import Database from "better-sqlite3"; | ||
| import { Kysely, SqliteDialect } from "kysely"; | ||
| import { afterEach, beforeEach, expect, it } from "vitest"; | ||
|
|
||
| import { runMigrations } from "../../src/database/migrations/runner.js"; | ||
| import { ContentRepository } from "../../src/database/repositories/content.js"; | ||
| import { TaxonomyRepository } from "../../src/database/repositories/taxonomy.js"; | ||
| import type { Database as DatabaseSchema } from "../../src/database/types.js"; | ||
| import { SchemaRegistry } from "../../src/schema/registry.js"; | ||
| import { fetchVisibleTermCounts } from "../../src/taxonomies/term-counts.js"; | ||
|
|
||
| interface CapturedQuery { | ||
| sql: string; | ||
| parameters: readonly unknown[]; | ||
| } | ||
|
|
||
| let sqlite: Database.Database; | ||
| let db: Kysely<DatabaseSchema>; | ||
| let captured: CapturedQuery[]; | ||
|
|
||
| beforeEach(async () => { | ||
| captured = []; | ||
| sqlite = new Database(":memory:"); | ||
| db = new Kysely<DatabaseSchema>({ | ||
| dialect: new SqliteDialect({ database: sqlite }), | ||
| log(event) { | ||
| if (event.level === "query") { | ||
| captured.push({ sql: event.query.sql, parameters: event.query.parameters }); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| // No ANALYZE: D1 never maintains sqlite_stat1. | ||
| await runMigrations(db); | ||
| const registry = new SchemaRegistry(db); | ||
| await registry.createCollection({ slug: "post", label: "Posts", labelSingular: "Post" }); | ||
| await registry.createField("post", { slug: "title", label: "Title", type: "string" }); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any -- schema vs Database type | ||
| const anyDb = db as any; | ||
| const content = new ContentRepository(anyDb); | ||
| const tax = new TaxonomyRepository(anyDb); | ||
|
|
||
| // Enough rows to make the two access paths visually distinct. | ||
| const terms = []; | ||
| for (let i = 0; i < 5; i++) { | ||
| terms.push( | ||
| await tax.create({ name: "category", slug: `term-${i}`, label: `Term ${i}`, locale: "en" }), | ||
| ); | ||
| } | ||
| for (let i = 0; i < 20; i++) { | ||
| const post = await content.create({ | ||
| type: "post", | ||
| slug: `post-${i}`, | ||
| data: { title: `Post ${i}` }, | ||
| status: "published", | ||
| locale: "en", | ||
| }); | ||
| await tax.attachToEntry("post", post.id, terms[i % terms.length]!.id); | ||
| } | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await db.destroy(); | ||
| }); | ||
|
|
||
| /** better-sqlite3 only binds primitives; coerce the JS values Kysely captured. */ | ||
| function bindable(p: unknown): unknown { | ||
| if (typeof p === "boolean") return p ? 1 : 0; | ||
| if (p instanceof Date) return p.toISOString(); | ||
| if (p === undefined) return null; | ||
| return p; | ||
| } | ||
|
|
||
| function explain(query: CapturedQuery): string { | ||
| const rows = sqlite | ||
| .prepare(`EXPLAIN QUERY PLAN ${query.sql}`) | ||
| .all(...query.parameters.map(bindable)) as { detail: string }[]; | ||
| return rows.map((r) => r.detail).join("\n"); | ||
| } | ||
|
|
||
| async function countQueryPlan(): Promise<string> { | ||
| captured = []; | ||
| await fetchVisibleTermCounts(db, "category", ["post"]); | ||
| const query = captured.find((q) => q.sql.includes("content_taxonomies")); | ||
| expect(query, "expected a term-count query against the pivot").toBeDefined(); | ||
| return explain(query!); | ||
| } | ||
|
|
||
| it("seeks the terms on a content_taxonomies index rather than probing the pivot per entry", async () => { | ||
| const plan = await countQueryPlan(); | ||
|
|
||
| // The pivot must be entered on a taxonomy_id-leading index. | ||
| expect(plan).toMatch(/SEARCH ct USING (COVERING )?INDEX idx_content_taxonomies/); | ||
| expect(plan).not.toContain("sqlite_autoindex_content_taxonomies_1"); | ||
| expect(plan).not.toContain("SCAN ct"); | ||
| }); | ||
|
|
||
| it("touches the content table only by primary key", async () => { | ||
| const plan = await countQueryPlan(); | ||
|
|
||
| expect(plan).toContain("SEARCH e USING"); | ||
| expect(plan).toMatch(/SEARCH e USING (COVERING )?INDEX sqlite_autoindex_ec_post_1 \(id=\?\)/); | ||
| expect(plan).not.toContain("SCAN e"); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.