Files
LWLT-AIBOT/control-plane/migrations/010_task_query_optimization.sql
2026-08-25 10:33:29 +08:00

18 lines
876 B
SQL

-- Keep the operator list and active-task scans bounded by organization and
-- lifecycle status. The list API still applies LIMIT/OFFSET, while these
-- indexes avoid sorting/reading large unrelated task sets as data grows.
CREATE INDEX IF NOT EXISTS tasks_visible_created_idx
ON tasks (organization_id, created_at DESC, id DESC)
WHERE status <> 'cancelled';
CREATE INDEX IF NOT EXISTS tasks_active_status_created_idx
ON tasks (organization_id, status, created_at DESC, id DESC)
WHERE status IN ('queued', 'accepted', 'running', 'reconciliation_pending');
CREATE INDEX IF NOT EXISTS tasks_active_handoff_created_idx
ON tasks (organization_id, handoff_status, created_at DESC, id DESC)
WHERE handoff_status IN ('accepted', 'running', 'reconciliation_pending');
CREATE INDEX IF NOT EXISTS task_events_org_task_idx
ON task_events (organization_id, task_id, id);