Skip to content

N+1 query pattern in 6+ functions — loads all sessions then loops KV lookups #18

@rohitg00

Description

@rohitg00

Problem

Multiple functions list ALL sessions, then loop and call kv.list(KV.observations(session.id)) for each session. For 100 sessions, that's 101+ KV calls. This will degrade linearly as data grows.

Affected Functions

  • src/functions/timeline.ts:50-65
  • src/functions/profile.ts:52-78
  • src/functions/patterns.ts:34-67
  • src/functions/auto-forget.ts:88-105
  • src/functions/evict.ts:80-108
  • src/functions/consolidate.ts:80-90

Suggested Fix

Use batch operations, pagination, or maintain pre-computed aggregate indices (e.g., a global observations index keyed by timestamp).

Metadata

Metadata

Assignees

No one assigned

    Labels

    highHigh priorityperformancePerformance optimization

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions