perf: fix N+1 query in NamespaceService::groupedForUser #14

Open
opened 2026-02-20 16:37:28 +00:00 by Clotho · 0 comments
Member

Problem

Services/NamespaceService.php::groupedForUser() (lines 245-271) has an N+1 query pattern:

foreach ($user->workspaces()->active()->get() as $workspace) {
    $namespaces = Namespace_::ownedByWorkspace($workspace)
        ->active()->ordered()->get(); // 1 query per workspace!
}

Impact

For a user with N workspaces, this executes N+1 database queries (1 for workspaces, then 1 per workspace for its namespaces). This method is called on every page load where namespace switching is available.

Acceptance Criteria

  • Rewrite to use a single query with whereIn or eager loading
  • Load all relevant namespaces in one query, then group by workspace_id in PHP
  • Add test or comment documenting expected query count

Discovered during automated scan (issue #3)

## Problem `Services/NamespaceService.php::groupedForUser()` (lines 245-271) has an N+1 query pattern: ```php foreach ($user->workspaces()->active()->get() as $workspace) { $namespaces = Namespace_::ownedByWorkspace($workspace) ->active()->ordered()->get(); // 1 query per workspace! } ``` ## Impact For a user with N workspaces, this executes N+1 database queries (1 for workspaces, then 1 per workspace for its namespaces). This method is called on every page load where namespace switching is available. ## Acceptance Criteria - Rewrite to use a single query with `whereIn` or eager loading - Load all relevant namespaces in one query, then group by workspace_id in PHP - Add test or comment documenting expected query count _Discovered during automated scan (issue #3)_
Clotho added the
discovery
P3
labels 2026-02-20 16:37:28 +00:00
Clotho was assigned by Charon 2026-02-20 23:46:41 +00:00
Charon added the
agent-ready
label 2026-02-21 01:31:59 +00:00
Sign in to join this conversation.
No description provided.