Skip to content

Comments

perf: batch user queries in schedules output service#25901

Merged
keithwillcode merged 6 commits intomainfrom
perf/batch-schedules-queries
Jan 13, 2026
Merged

perf: batch user queries in schedules output service#25901
keithwillcode merged 6 commits intomainfrom
perf/batch-schedules-queries

Conversation

@pedroccastro
Copy link
Contributor

What does this PR do?

Fixes N+1 query pattern in organization and team schedule endpoints

Problem:

Both services were calling getResponseSchedule in a loop, which internally fetched user's default schedule ID one by one:

for (const schedule of schedules) {
  responseSchedules.push(await this.outputSchedulesService.getResponseSchedule(schedule));
  // ↳ internally calls: await this.usersRepository.getUserScheduleDefaultId(userId)
}

For an organization with 100 users, this means 100+ database round-trips.

Solution:

  • Add getUsersScheduleDefaultIds batch method to UsersRepository
  • Add getResponseSchedules batch method to OutputSchedulesService
  • Extract transformScheduleToOutput for reuse and testability

Changes:

File Change
users.repository.ts Add getUsersScheduleDefaultIds batch method
output-schedules.service.ts Add getResponseSchedules + extract transformScheduleToOutput
organizations-schedules.service.ts Use batch method
teams-schedules.service.ts Use batch method

Also removes dead code (formatInput method)

How should this be tested?

  1. GET /v2/organizations/{orgId}/schedules — verify returns schedules correctly
  2. GET /v2/teams/{teamId}/schedules — verify returns schedules correctly

No functional change, same response format, fewer queries.

Mandatory Tasks

  • I have self-reviewed the code
  • N/A - I have updated the developer docs in /docs if this PR makes changes that would require a documentation change
  • N/A - Performance improvement, existing e2e tests cover functionality

Replace N sequential queries with single batch query for fetching
user default schedule IDs

Changes:
- Add getUsersScheduleDefaultIds batch method to UsersRepository
- Add getResponseSchedules batch method to OutputSchedulesService
- Extract transformScheduleToOutput for reuse
- Simplify OrganizationsSchedulesService and TeamsSchedulesService
- Remove dead code (formatInput no-op method)

Reduces database round-trips from O(n) to O(1) for schedule lookups
@vercel
Copy link

vercel bot commented Dec 22, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Review Updated (UTC)
cal Ignored Ignored Dec 22, 2025 5:00pm
cal-companion Ignored Ignored Preview Dec 22, 2025 5:00pm
cal-eu Ignored Ignored Dec 22, 2025 5:00pm

@pedroccastro pedroccastro marked this pull request as ready for review December 22, 2025 16:59
@pedroccastro pedroccastro requested a review from a team as a code owner December 22, 2025 16:59
@graphite-app graphite-app bot added core area: core, team members only foundation labels Dec 22, 2025
@graphite-app graphite-app bot requested a review from a team December 22, 2025 17:00
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

@github-actions
Copy link
Contributor

This PR has been marked as stale due to inactivity. If you're still working on it or need any help, please let us know or update the PR to keep it active.

Copy link
Contributor

@ThyMinimalDev ThyMinimalDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@github-actions github-actions bot removed the Stale label Jan 13, 2026
@vercel vercel bot temporarily deployed to Preview – cal-companion January 13, 2026 09:06 Inactive
@vercel vercel bot temporarily deployed to Preview – dev January 13, 2026 09:07 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants