perf(projects): bulk SQL replaces N+1 stats loop on /api/projects (26s → 3s cold)#65
Merged
Merged
Conversation
The dashboard loads /api/projects?include_stats=true on first paint. On a 188-project / 228K-message store this took 26s cold because the route ran the per-project aggregator pipeline once per project. This commit replaces that loop with three bulk passes: - bulk_session_counts(): one GROUP BY for file_count - bulk_project_lite_stats(): one GROUP BY for tokens / dates / message counts, sufficient for the project-list cards - bulk_project_cost(): per-(project_id, model, speed) rollup folded through compute_cost(); honours fast-mode multipliers Also memoises _dir_size_mb by (path, mtime) so repeat calls don't re-glob 188 directories. Result on the user's real store: cold: 26152 ms → 3175 ms (8.2x) warm: 504 ms → 443 ms (small; bounded by SQL pass) The aggregator-only fields (avg_steps_per_command, compact_summary_count, avg_tokens_per_command) default to 0 in the list view — they're only meaningful in the per-project detail view, which still runs the full aggregator on demand via /api/dashboard-data. 1333 backend tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Numbers (real local data)
8.2× cold-load speedup. Warm is bounded by SQL (3 GROUP BYs on 228K messages); could be cached with TieredCache as a follow-up.
Test plan
🤖 Generated with Claude Code