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-10-22 - Optimizing Distinct Counts in Prisma
**Learning:** Prisma's `.count()` API does not support the `distinct` argument in this project's version, which prevents us from doing `prisma.model.count({ distinct: ['field'] })`. We were working around this by fetching all rows to Node.js and deduplicating them with a `Set`.
**Action:** To compute a distinct count at the database level and avoid O(N) memory/network usage, use `.findMany({ distinct: ['field'], select: { field: true } })` followed by calling `.length` on the results. This pushes the deduplication to the database engine.
6 changes: 4 additions & 2 deletions src/server/api/routers/judging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2126,9 +2126,11 @@ export const judgingRouter = createTRPCRouter({
});
if (duplicate)
throw new TRPCError({ code: "CONFLICT", message: "This judge already has that scoring scope." });
// ⚡ Bolt: Pushing deduplication to the database to reduce memory and network overhead.
// Expected impact: Faster database query and reduced Node.js memory footprint.
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