Skip to content

Commit cef5e03

Browse files
authored
perf: optimize SQL queries (#515)
1 parent 3165593 commit cef5e03

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

.changeset/many-days-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
perf: optimize SQL queries

packages/cloudflare/src/api/durable-objects/queue.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,16 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
281281
checkSyncTable(msg: QueueMessage) {
282282
try {
283283
if (this.disableSQLite) return false;
284-
const numNewer = this.sql
285-
.exec<{
286-
numNewer: number;
287-
}>(
288-
"SELECT COUNT(*) as numNewer FROM sync WHERE id = ? AND lastSuccess > ? LIMIT 1",
289-
`${msg.MessageBody.host}${msg.MessageBody.url}`,
290-
Math.round(msg.MessageBody.lastModified / 1000)
291-
)
292-
.one().numNewer;
293-
294-
return numNewer > 0;
295-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
296-
} catch (e: unknown) {
284+
return (
285+
this.sql
286+
.exec(
287+
"SELECT 1 FROM sync WHERE id = ? AND lastSuccess > ? LIMIT 1",
288+
`${msg.MessageBody.host}${msg.MessageBody.url}`,
289+
Math.round(msg.MessageBody.lastModified / 1000)
290+
)
291+
.toArray().length > 0
292+
);
293+
} catch {
297294
return false;
298295
}
299296
}

packages/cloudflare/src/api/durable-objects/sharded-tag-cache.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ export class DOShardedTagCache extends DurableObject<CloudflareEnv> {
1212
}
1313

1414
async hasBeenRevalidated(tags: string[], lastModified?: number): Promise<boolean> {
15-
const result = this.sql
16-
.exec<{
17-
cnt: number;
18-
}>(
19-
`SELECT COUNT(*) as cnt FROM revalidations WHERE tag IN (${tags.map(() => "?").join(", ")}) AND revalidatedAt > ?`,
20-
...tags,
21-
lastModified ?? Date.now()
22-
)
23-
.one();
24-
return result.cnt > 0;
15+
return (
16+
this.sql
17+
.exec(
18+
`SELECT 1 FROM revalidations WHERE tag IN (${tags.map(() => "?").join(", ")}) AND revalidatedAt > ? LIMIT 1`,
19+
...tags,
20+
lastModified ?? Date.now()
21+
)
22+
.toArray().length > 0
23+
);
2524
}
2625

2726
async writeTags(tags: string[], lastModified: number): Promise<void> {

packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ export class D1NextModeTagCache implements NextModeTagCache {
1515
try {
1616
const result = await db
1717
.prepare(
18-
`SELECT COUNT(*) as cnt FROM revalidations WHERE tag IN (${tags.map(() => "?").join(", ")}) AND revalidatedAt > ? LIMIT 1`
18+
`SELECT 1 FROM revalidations WHERE tag IN (${tags.map(() => "?").join(", ")}) AND revalidatedAt > ? LIMIT 1`
1919
)
2020
.bind(...tags.map((tag) => this.getCacheKey(tag)), lastModified ?? Date.now())
21-
.first<{ cnt: number }>();
22-
if (!result) throw new RecoverableError(`D1 select failed for ${tags}`);
21+
.raw();
2322

24-
return result.cnt > 0;
23+
return result.length > 0;
2524
} catch (e) {
2625
error(e);
2726
// By default we don't want to crash here, so we return false

0 commit comments

Comments
 (0)