Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-18 - Distinct Counting with Prisma in Node
**Learning:** Found O(N) operation measuring distinct array elements retrieved from DB (`new Set(rows.map(row => row.projectId)).size`) after a `findMany` query. This consumes extra JS memory and compute time as array sizes grow.
**Action:** Replace `findMany` followed by JS `Set` operations with Prisma `findMany({ distinct: ["projectId"], select: { projectId: true } }).length` for better performance at the DB layer, as Prisma `count` doesn`t support distinct in this project version.
7 changes: 5 additions & 2 deletions src/server/api/routers/judging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2126,9 +2126,12 @@ export const judgingRouter = createTRPCRouter({
});
if (duplicate)
throw new TRPCError({ code: "CONFLICT", message: "This judge already has that scoring scope." });
// ⚡ Bolt: Push deduplication to the DB using `distinct` and `.length` instead of fetching everything and making a JS Set.
// Why: `findMany` + mapping to JS arrays and Sets takes more memory and compute time for large sets.
// Impact: Avoids fetching duplicate rows, avoids Set allocations, and lowers latency (O(1) JS side with smaller arrays).
const projectCount = await ctx.prisma.judgingAssignment
.findMany({ where: { judgeId: judge.id }, select: { projectId: true } })
.then(rows => new Set(rows.map(row => row.projectId)).size);
.findMany({ where: { judgeId: judge.id }, distinct: ["projectId"], select: { projectId: true } })
.then(rows => rows.length);
const alreadyVisits = await ctx.prisma.judgingAssignment.count({
where: { judgeId: judge.id, projectId: assignment.projectId },
});
Expand Down
Loading