Skip to content

Conversation

@lancy
Copy link
Contributor

@lancy lancy commented Jan 16, 2026

Summary

  • Add 4 new database indexes to optimize query performance for high-frequency operations
  • Add schema-defined indexes in Drizzle following existing patterns (runner_job_queue, agent_schedules)

Indexes Added

Index Table Type Purpose
idx_agent_runs_user_created agent_runs Composite User run listing with time-based sorting
idx_agent_runs_running_heartbeat agent_runs Partial Cron cleanup job (only running status)
idx_agent_runs_schedule_created agent_runs Partial Schedule run history (only scheduled runs)
idx_agent_sessions_user_compose_artifact agent_sessions Composite findOrCreate pattern optimization

Query Impact

  • GET /v1/runs: Now uses composite index instead of full table scan
  • /api/cron/cleanup-sandboxes: Uses partial index on running status
  • schedule-service.ts getRecentRuns: Uses partial index for scheduled runs
  • agent-session-service.ts findOrCreate: Uses composite index

What's NOT Included

  • cli_tokens.expiresAt: Not needed - unique index on token already covers auth queries (returns 0-1 rows)

Test plan

  • Type check passes (pnpm check-types)
  • Lint passes (pnpm turbo run lint)
  • All tests pass (pnpm vitest --run)
  • Run pnpm db:migrate after deployment to apply indexes

Closes #1284

🤖 Generated with Claude Code

Add 4 new indexes to optimize query performance:

- idx_agent_runs_user_created: Composite index for user run listing
- idx_agent_runs_running_heartbeat: Partial index for cron cleanup
- idx_agent_runs_schedule_created: Partial index for schedule history
- idx_agent_sessions_user_compose_artifact: Composite for findOrCreate

Closes #1284

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@lancy lancy force-pushed the feat/add-performance-indexes branch from 2a0a443 to c6a2bb1 Compare January 16, 2026 16:28
@lancy
Copy link
Contributor Author

lancy commented Jan 16, 2026

Code Review - Claude Code

Commit: c6a2bb1 - perf(db): add missing indexes for high-frequency queries

Summary

This PR adds 4 database indexes to optimize high-frequency queries. The changes are well-designed and follow best practices.

Files Reviewed

File Status
migrations/0051_add_performance_indexes.sql Added
schema/agent-run.ts Modified
schema/agent-session.ts Modified

Index Analysis

Index Type Purpose Verdict
idx_agent_runs_user_created Composite User run listing with time sort Well-designed
idx_agent_runs_running_heartbeat Partial Cron cleanup for stale sandboxes Excellent
idx_agent_runs_schedule_created Partial Schedule run history Well-designed
idx_agent_sessions_user_compose_artifact Composite findOrCreate pattern Appropriate

Bad Code Smell Checklist

  • No new mocks introduced
  • No unnecessary try/catch blocks
  • No dynamic imports
  • No any types (uses as never for Drizzle partial index syntax - established pattern)
  • No lint/type suppressions
  • No artificial delays or fake timers
  • No hardcoded URLs

Issues Found

None

Verdict

APPROVE - Clean performance optimization with well-documented migrations and appropriate use of partial indexes.


Detailed review: codereviews/20260116/review-c6a2bb1.md

@lancy lancy added this pull request to the merge queue Jan 17, 2026
Merged via the queue into main with commit c0ae99c Jan 17, 2026
15 checks passed
@github-actions github-actions bot mentioned this pull request Jan 17, 2026
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.

perf: add missing database indexes for high-frequency query columns

2 participants