Skip to content

[Server] Batch user lookups in TeamController #12505

Description

@Ash092016

Description

In TeamController::showPage(), the method getUserById() is called inside two separate foreach loops once per team member and once per user seeking a team. Each call executes an individual SELECT query against the database, resulting in an N+1 query pattern that scales linearly with the number of users.

Current Code (N+1 Pattern)

// Loop 1: One query per team member (lines 555-557)
foreach ($team->getMembers() as $teammate) {
    $members[] = $this->core->getQueries()->getUserById($teammate);
}

// Loop 2: One query per seeker (lines 571-573)
foreach ($users_seeking_team as $user_seeking_team) {
    $seekers[] = $this->core->getQueries()->getUserById($user_seeking_team);
}

For a team with 5 members and 20 students seeking teams, this executes 25 individual SELECT queries instead of 1 batch query.

Proposed Fix

Replace both loops with the existing getUsersById() batch method from DatabaseQueries.php to fetch all users in a single query. This method is already used elsewhere in the codebase (e.g., ElectronicGraderController::importTeams()).

Impact

  • Reduces database queries from O(N) to O(1) for this page
  • Improves page load time for courses with many students seeking teams
  • No functional change same data returned, just fetched more efficiently

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions