perf(scheduler): harden jobs for scalable execution

This commit is contained in:
2026-05-20 12:19:27 +08:00
parent 4b09a34748
commit 5fb9d0b0dd
23 changed files with 887 additions and 147 deletions
@@ -0,0 +1,6 @@
DROP INDEX IF EXISTS idx_tenant_plan_active_window;
DROP INDEX IF EXISTS idx_brands_monitoring_daily_candidate;
DROP INDEX IF EXISTS idx_platform_accounts_monitoring_client_platform;
DROP INDEX IF EXISTS idx_desktop_clients_active_plan_candidate;
DROP INDEX IF EXISTS idx_schedule_task_due_dispatch;
DROP INDEX IF EXISTS idx_schedule_task_missing_next_run;
@@ -0,0 +1,30 @@
CREATE INDEX IF NOT EXISTS idx_schedule_task_missing_next_run
ON schedule_tasks (created_at, id)
WHERE deleted_at IS NULL
AND status = 'enabled'
AND next_run_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_schedule_task_due_dispatch
ON schedule_tasks (next_run_at, id)
WHERE deleted_at IS NULL
AND status = 'enabled'
AND next_run_at IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_desktop_clients_active_plan_candidate
ON desktop_clients (tenant_id, workspace_id, created_at, id)
INCLUDE (last_seen_at)
WHERE revoked_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_platform_accounts_monitoring_client_platform
ON platform_accounts (tenant_id, workspace_id, client_id, platform_id)
WHERE deleted_at IS NULL
AND client_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_brands_monitoring_daily_candidate
ON brands (tenant_id, created_at, id)
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_tenant_plan_active_window
ON tenant_plan_subscriptions (tenant_id, start_at, end_at, plan_id)
WHERE status = 'active'
AND deleted_at IS NULL;