vibe-kanban
vibe-kanban copied to clipboard
perf: fix database query performance bottlenecks
Summary
This PR addresses significant database performance bottlenecks identified in production logs where queries were taking 2-5 seconds to execute.
Changes Made
-
Added composite index on
workspace_repos(workspace_id, repo_id)- Optimizes lookup queries that were taking up to 5 seconds
- The query
SELECT ... FROM workspace_repos WHERE workspace_id = $1 AND repo_id = $2was appearing repeatedly in logs with 1.1s-5.0s execution times - With this composite index, the database can jump directly to the matching row
-
Added composite index on
merges(merge_type, pr_status)- Optimizes status filtering queries that were taking 2+ seconds
- The query filtering by
WHERE merge_type = 'pr' AND pr_status = 'open'was performing sequential scans - This index allows efficient filtering on both columns
-
Fixed N+1 query in
workspace_repo.rs:create_many- Wrapped individual inserts in a transaction to batch them efficiently
- Reduces round-trips to the database during bulk workspace repo creation
-
Fixed N+1 query in
get_task_attempt_branch_statusroute- Changed from querying
Merge::find_by_workspace_and_repo_idin a loop (once per repo) - Now batch fetches all merges with
Merge::find_by_workspace_idupfront and groups by repo_id - Eliminates repeated database queries for the same workspace
- Changed from querying
Performance Impact
Based on the production logs analyzed:
- Queries on
workspace_reposwere taking 1.1s-5.0s → should be <10ms with the index - Queries on
mergeswere taking 2.43s → should be <10ms with the index - N+1 patterns reduced from O(n) queries to O(1) batch query
Migration
The migration 20260112160045_add_composite_indexes_for_performance.sql adds the indexes and runs PRAGMA optimize to update query planner statistics.