Skip to content

Conversation

@smoreinis
Copy link
Collaborator

Summary

  • Add indexes to SpanORM: trace_id, (trace_id, start_time), parent_id
  • Add index to AgentORM: status
  • Add index to TaskORM: status

Problem

SpanORM had no indexes at all, causing full table scans on every query. AgentORM and TaskORM status columns were also unindexed despite being used in WHERE clauses.

Test Results

Metric Without Index With Index Improvement
Query Plan Seq Scan Bitmap Index Scan Direct lookup
Execution Time 0.185 ms 0.064 ms ~3x faster
Rows Scanned 894 (all) 2 (matches only) 99.8% reduction

Note: With larger datasets (100K+ rows), improvement would be 100x or more due to O(log N) vs O(N) complexity.

How to Verify

-- Check indexes exist
\di ix_spans_*
\di ix_agents_status
\di ix_tasks_status

-- Test query plan
EXPLAIN ANALYZE SELECT * FROM spans WHERE trace_id = 'xxx' ORDER BY start_time;
-- Should show "Bitmap Index Scan on ix_spans_trace_id_start_time"

Files Changed

  • src/adapters/orm.py - Added __table_args__ with indexes
  • database/migrations/alembic/versions/2025_12_24_*_add_performance_indexes.py - Migration

- Add indexes to SpanORM: trace_id, (trace_id, start_time), parent_id
- Add index to AgentORM: status
- Add index to TaskORM: status

These indexes improve query performance for:
- Filtering spans by trace_id (common in observability queries)
- Ordering spans by start_time within a trace
- Traversing span hierarchy via parent_id
- Filtering agents/tasks by status (used in list queries)

Test results show ~3x improvement on 894 rows, expected to be
100x+ on larger datasets due to O(log N) vs O(N) complexity.
@smoreinis smoreinis marked this pull request as ready for review December 24, 2025 20:57
@smoreinis smoreinis requested a review from a team as a code owner December 24, 2025 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants