Skip to content

perf: optimize dashboard refresh from >500ms to ~140ms#8

Merged
sleipi merged 1 commit into
mainfrom
perf/dashboard-query-optimization
May 11, 2026
Merged

perf: optimize dashboard refresh from >500ms to ~140ms#8
sleipi merged 1 commit into
mainfrom
perf/dashboard-query-optimization

Conversation

@sleipi
Copy link
Copy Markdown
Owner

@sleipi sleipi commented May 11, 2026

Summary

  • Rewrite getToolUsageSummary() from N×5 dynamic queries with string interpolation to a single prepared CTE query (P0, ~60-80% of latency)
  • Collapse getTokenSummary() from 4 separate full-table scans to 1 query with conditional aggregation (P1)
  • Remove date() function wrapping in WHERE clauses to enable index usage on idx_messages_timestamp / idx_tool_calls_timestamp (P1)
  • Add 2s TTL cache on /api/stats route to skip redundant DB queries within refresh cycles (P2)
  • Move maybeAggregate() / maybeGC() from request path to background setInterval timers (P2)
  • Add mmap_size (256MB) and cache_size (8MB) SQLite pragmas for read performance (P2)
  • Add idx_sessions_last_seen index via new migration v2 (P3)
  • Prepare getTodayTokens statement in constructor for consistency

Test coverage

  • 3 new tests: cache hit within TTL, cache refresh after TTL, per-directory cache isolation
  • 138 tests pass, 0 failures

Measured result

Dashboard auto-refresh latency reduced from >500ms (yellow) to ~140ms (green).

@sleipi sleipi added the enhancement New feature or request label May 11, 2026
Rewrite getToolUsageSummary() from N×5 dynamic queries to single prepared
CTE query. Collapse getTokenSummary() from 4 queries to 1. Remove date()
function wrapping in WHERE clauses to enable index usage. Add 2s TTL cache
on /api/stats route. Move maintenance off request path to background timers.
Add mmap_size/cache_size pragmas and idx_sessions_last_seen index.
@sleipi sleipi force-pushed the perf/dashboard-query-optimization branch from bf10290 to a78506f Compare May 11, 2026 14:00
@sleipi sleipi merged commit 0c10f21 into main May 11, 2026
3 checks passed
@sleipi sleipi deleted the perf/dashboard-query-optimization branch May 11, 2026 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant