Skip to content

Commit 39b5242

Browse files
committed
perf(webapp): avoid unindexed fileId scan in get-background-worker-by-version
The endpoint loaded each file's tasks via the files.tasks relation, which queries BackgroundWorkerTask by the unindexed fileId column and sequential-scans a very large table for every request. Group task slugs by fileId in memory from the tasks that are already loaded via the indexed workerId relation, and drop the files.tasks include. The response shape is unchanged.
1 parent a6bd370 commit 39b5242

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
area: webapp
3+
type: improvement
4+
---
5+
6+
Speed up retrieving a background worker by version. The endpoint no longer runs a slow lookup that scanned the full task table for large deployments; it now reuses data it already loads, so the response is the same but returns much faster.

apps/webapp/app/routes/api.v1.projects.$projectRef.background-workers.$envSlug.$version.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,27 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
4545
},
4646
include: {
4747
tasks: true,
48-
files: {
49-
include: {
50-
tasks: {
51-
select: {
52-
slug: true,
53-
},
54-
},
55-
},
56-
},
48+
files: true,
5749
},
5850
});
5951

6052
if (!backgroundWorker) {
6153
return json({ error: "Background worker not found" }, { status: 404 });
6254
}
6355

56+
// Group task slugs by fileId from the already-loaded tasks (which are fetched
57+
// via the indexed workerId relation) instead of loading files.tasks, which
58+
// queries BackgroundWorkerTask by the unindexed fileId column.
59+
const taskSlugsByFileId = new Map<string, Set<string>>();
60+
for (const task of backgroundWorker.tasks) {
61+
if (!task.fileId) {
62+
continue;
63+
}
64+
const slugs = taskSlugsByFileId.get(task.fileId) ?? new Set<string>();
65+
slugs.add(task.slug);
66+
taskSlugsByFileId.set(task.fileId, slugs);
67+
}
68+
6469
return json({
6570
id: backgroundWorker.friendlyId,
6671
version: backgroundWorker.version,
@@ -82,7 +87,7 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
8287
filePath: file.filePath,
8388
contentHash: file.contentHash,
8489
contents: decompressContent(file.contents),
85-
tasks: Array.from(new Set(file.tasks.map((task) => task.slug))),
90+
tasks: Array.from(taskSlugsByFileId.get(file.id) ?? []),
8691
})),
8792
});
8893
} catch (error) {

0 commit comments

Comments
 (0)