Skip to content
This repository was archived by the owner on Apr 2, 2026. It is now read-only.
This repository was archived by the owner on Apr 2, 2026. It is now read-only.

perf: parallelize sequential DB queries in adminStats and getCompletionPercentage #268

Description

@claude

Two tRPC procedures execute independent database queries sequentially instead of in parallel with Promise.all. Each pair has no data dependency and can run concurrently to reduce latency.

Affected Locations

1. challenge.adminStatsserver/api/routers/challenge.ts lines 256–289

// Currently sequential (two round-trips):
const [submissionStats] = await ctx.db.select(...).from(userSubmission);
const [progressStats]   = await ctx.db.select(...).from(userProgress);

// Fix — parallel:
const [[submissionStats], [progressStats]] = await Promise.all([
  ctx.db.select(...).from(userSubmission),
  ctx.db.select(...).from(userProgress),
]);

2. userProgress.getCompletionPercentageserver/api/routers/userProgress.ts lines 94–113

// Currently sequential (two round-trips):
const [totalResult]     = await ctx.db.select({ count: count() }).from(challenge).where(themeFilter);
const [completedResult] = await completedQuery;

// Fix — parallel:
const [[totalResult], [completedResult]] = await Promise.all([
  ctx.db.select({ count: count() }).from(challenge).where(themeFilter),
  completedQuery,
]);

Impact

Each sequential pair adds one extra database round-trip per request. With Neon serverless, individual query latency is 5–30ms, so parallelizing saves that overhead on every call to these endpoints.

Notes

The codebase already applies this pattern in other procedures — getStatus in onboarding was parallelized in e364ea7. This is a continuation of that work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions