Skip to content

Conversation

@dimitri-yatsenko
Copy link
Member

Summary

Add a composite index to the jobs table to optimize the critical job fetching query used during populate(reserve_jobs=True).

Problem

When populating with job reservation, the following query runs frequently:

SELECT ... FROM jobs
WHERE status='pending' AND scheduled_time <= NOW()
ORDER BY priority ASC, scheduled_time ASC
LIMIT N

Without an index, this requires a full table scan and filesort, which becomes slow with large job queues (thousands of jobs).

Solution

Add a composite index that covers both the WHERE clause and ORDER BY:

INDEX (status, priority, scheduled_time)

This enables:

  • Index range scan on status='pending'
  • Filtering on scheduled_time within the index
  • Index-ordered retrieval by priority, scheduled_time (no filesort needed)

Testing

  • All test_jobs.py tests pass
  • Verified index creation in MySQL with SHOW INDEX

🤖 Generated with Claude Code

Add INDEX (status, priority, scheduled_time) to optimize the critical
populate query:

  SELECT ... FROM jobs
  WHERE status='pending' AND scheduled_time <= NOW()
  ORDER BY priority ASC, scheduled_time ASC

This index covers the WHERE clause and ORDER BY, eliminating table scans
and filesorts for large job queues.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@github-actions github-actions bot added the enhancement Indicates new improvements label Jan 30, 2026
@MilagrosMarin MilagrosMarin merged commit 71b63db into master Jan 30, 2026
8 checks passed
@MilagrosMarin MilagrosMarin deleted the perf/jobs-table-index branch January 30, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Indicates new improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants