vibe-kanban icon indicating copy to clipboard operation
vibe-kanban copied to clipboard

perf: fix database query performance bottlenecks

Open wreed4 opened this issue 1 month ago • 0 comments

Summary

This PR addresses significant database performance bottlenecks identified in production logs where queries were taking 2-5 seconds to execute.

Changes Made

  1. 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 = $2 was appearing repeatedly in logs with 1.1s-5.0s execution times
    • With this composite index, the database can jump directly to the matching row
  2. 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
  3. 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
  4. Fixed N+1 query in get_task_attempt_branch_status route
    • Changed from querying Merge::find_by_workspace_and_repo_id in a loop (once per repo)
    • Now batch fetches all merges with Merge::find_by_workspace_id upfront and groups by repo_id
    • Eliminates repeated database queries for the same workspace

Performance Impact

Based on the production logs analyzed:

  • Queries on workspace_repos were taking 1.1s-5.0s → should be <10ms with the index
  • Queries on merges were 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.

wreed4 avatar Jan 12 '26 21:01 wreed4