perf: optimize API queries and add database indexes #347

Merged
review.bot merged 1 commit from fix/338-ui-performance into develop 2026-03-02 14:15:53 +00:00
Owner

Summary

  • getProjectStats(): Replace list() with count() for session/observation/summary counts, use small sample (100) for token estimation instead of loading all records
  • getAnalyticsTypes(): Reduce limit from 100K to 5K — only needed for type distribution counts
  • getAnalyticsProjects(): Remove 1K observation load per project for token sums (was causing N+1-like overhead)
  • getProjectFiles(): Reduce limit from 10K to 500 — only top 50 files are returned anyway
  • Add composite indexes: Task(status, created_at), Session(project, status), Observation(project, type) for faster filtered queries

Closes #338

## Summary - **`getProjectStats()`**: Replace `list()` with `count()` for session/observation/summary counts, use small sample (100) for token estimation instead of loading all records - **`getAnalyticsTypes()`**: Reduce limit from 100K to 5K — only needed for type distribution counts - **`getAnalyticsProjects()`**: Remove 1K observation load per project for token sums (was causing N+1-like overhead) - **`getProjectFiles()`**: Reduce limit from 10K to 500 — only top 50 files are returned anyway - **Add composite indexes**: `Task(status, created_at)`, `Session(project, status)`, `Observation(project, type)` for faster filtered queries Closes #338
- Replace list() with count() in getProjectStats() (was loading 10K+ records)
- Reduce getAnalyticsTypes() limit from 100K to 5K (cached anyway)
- Remove N+1 token loading in getAnalyticsProjects() (3 queries per project)
- Reduce getProjectFiles() limit from 10K to 500 (only returns top 50)
- Add composite indexes: Task(status, created_at), Session(project, status),
  Observation(project, type)
review.bot left a comment
Owner

AI Code Review (Devstral)

The PR implements performance optimizations for API queries and adds database indexes. The changes align well with the linked issue #338, addressing excessive load times by reducing query limits and using count() instead of list() where appropriate. The added indexes will improve query performance on filtered data.


Automated review by pr-reviewer

## AI Code Review (Devstral) The PR implements performance optimizations for API queries and adds database indexes. The changes align well with the linked issue #338, addressing excessive load times by reducing query limits and using count() instead of list() where appropriate. The added indexes will improve query performance on filtered data. --- *Automated review by [pr-reviewer](https://github.com/customable/pr-reviewer)*
review.bot merged commit 55c7f6974d into develop 2026-03-02 14:15:53 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
customable/claude-mem!347
No description provided.