Add partial indexes for has_pending() and dashboard throughput queries

has_pending() scans jobs WHERE source_id=? AND status IN
('queued','claimed') on every poller sweep — without an index this
is a full table scan as the jobs table grows. Similarly,
count_succeeded_since() scans by completed_at for the dashboard's
rolling-throughput display.

Add two partial indexes:
- idx_jobs_pending_by_source: covers has_pending() lookups
- idx_jobs_succeeded_completed: covers count_succeeded_since()

Both use CREATE INDEX IF NOT EXISTS so they're idempotent on
existing databases.
This commit is contained in:
Chris McDonough 2026-06-01 06:28:55 -04:00
parent d5e5733f67
commit 48826031ac

View file

@ -40,6 +40,18 @@ ON jobs(scheduled_at)
WHERE status = 'queued'
"""
JOBS_PENDING_BY_SOURCE_INDEX = """
CREATE INDEX IF NOT EXISTS idx_jobs_pending_by_source
ON jobs(source_id)
WHERE status IN ('queued', 'claimed')
"""
JOBS_SUCCEEDED_COMPLETED_INDEX = """
CREATE INDEX IF NOT EXISTS idx_jobs_succeeded_completed
ON jobs(completed_at)
WHERE status = 'succeeded'
"""
SYNC_STATE_DDL = """
CREATE TABLE IF NOT EXISTS sync_state (
source_id TEXT NOT NULL,
@ -67,6 +79,8 @@ ALL_DDL: tuple[str, ...] = (
JOBS_DDL,
JOBS_LIVE_INDEX,
JOBS_CLAIMABLE_INDEX,
JOBS_PENDING_BY_SOURCE_INDEX,
JOBS_SUCCEEDED_COMPLETED_INDEX,
SYNC_STATE_DDL,
SCHEMA_VERSION_DDL,
DLQ_VIEW,