diff --git a/.changeset/poor-points-work.md b/.changeset/poor-points-work.md new file mode 100644 index 00000000..5a4ad9be --- /dev/null +++ b/.changeset/poor-points-work.md @@ -0,0 +1,7 @@ +--- +"@opennextjs/cloudflare": patch +--- + +perf: add an index on the tag column in D1 tag cache + +It only applies to newly created tables diff --git a/packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.ts b/packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.ts index 040032e4..519668ee 100644 --- a/packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.ts +++ b/packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.ts @@ -63,7 +63,7 @@ export class D1NextModeTagCache implements NextModeTagCache { await db.batch( tags.map((tag) => db - .prepare(`INSERT INTO revalidations (tag, revalidatedAt) VALUES (?, ?)`) + .prepare(`INSERT OR REPLACE INTO revalidations (tag, revalidatedAt) VALUES (?, ?)`) .bind(this.getCacheKey(tag), Date.now()) ) ); diff --git a/packages/cloudflare/src/cli/commands/populate-cache.ts b/packages/cloudflare/src/cli/commands/populate-cache.ts index 8cc68707..62fa237c 100644 --- a/packages/cloudflare/src/cli/commands/populate-cache.ts +++ b/packages/cloudflare/src/cli/commands/populate-cache.ts @@ -254,7 +254,7 @@ function populateD1TagCache( [ "d1 execute", D1_TAG_BINDING_NAME, - `--command "CREATE TABLE IF NOT EXISTS revalidations (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);"`, + `--command "CREATE TABLE IF NOT EXISTS revalidations (tag TEXT PRIMARY KEY, revalidatedAt INTEGER);"`, `--preview ${populateCacheOptions.shouldUsePreviewId}`, ], {