Fix bd-666: Replace N+1 query pattern in auto-import with batch fetch

- Batch fetch all existing issues with SearchIssues() upfront
- Use O(1) map lookup instead of O(n) GetIssue() calls
- Improves performance dramatically with 1000+ issues
- All tests pass
This commit is contained in:
Steve Yegge
2025-10-16 19:14:05 -07:00
parent 37d60d624d
commit ef31d98b43
6 changed files with 1413 additions and 164 deletions

View File

@@ -342,12 +342,22 @@ func autoImportIfNewer() {
allIssues = filteredIssues
}
// Batch fetch all existing issues to avoid N+1 query pattern (bd-666)
allExistingIssues, err := store.SearchIssues(ctx, "", types.IssueFilter{})
if err != nil {
fmt.Fprintf(os.Stderr, "Auto-import failed: error fetching existing issues: %v\n", err)
return
}
// Build map for O(1) lookup
existingByID := make(map[string]*types.Issue)
for _, issue := range allExistingIssues {
existingByID[issue.ID] = issue
}
// Import non-colliding issues (exact matches + new issues)
for _, issue := range allIssues {
existing, err := store.GetIssue(ctx, issue.ID)
if err != nil {
continue
}
existing := existingByID[issue.ID]
if existing != nil {
// Update existing issue