Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(nuq): add indexes

Gergő Móricze3b7f522486e
diff --git a/apps/nuq-postgres/nuq.sql b/apps/nuq-postgres/nuq.sql
index 834390d0d..287dc5d67 100644
--- a/apps/nuq-postgres/nuq.sql
+++ b/apps/nuq-postgres/nuq.sql
@@ -47,6 +47,17 @@ CREATE INDEX IF NOT EXISTS nuq_queue_scrape_completed_created_at_idx ON nuq.queu
CREATE INDEX IF NOT EXISTS nuq_queue_scrape_queued_owner_idx ON nuq.queue_scrape (owner_id, priority ASC, created_at ASC) WHERE (status = 'queued'::nuq.job_status);
CREATE INDEX IF NOT EXISTS nuq_queue_scrape_queued_owner_group_idx ON nuq.queue_scrape (owner_id, group_id, priority ASC, created_at ASC) WHERE (status = 'queued'::nuq.job_status);
+-- Performance indexes for prefetchJobs query optimization
+-- This index dramatically speeds up the "SELECT DISTINCT owner_id, group_id" query in the queued_combinations CTE
+-- by allowing an index-only scan instead of a sequential scan of all queued jobs
+CREATE INDEX IF NOT EXISTS nuq_queue_scrape_queued_owner_group_distinct_idx ON nuq.queue_scrape (owner_id, group_id) WHERE (status = 'queued'::nuq.job_status);
+
+-- This index helps with group-specific queries and provides better coverage for per-group capacity lookups
+CREATE INDEX IF NOT EXISTS nuq_queue_scrape_queued_group_priority_idx ON nuq.queue_scrape (group_id, priority ASC, created_at ASC) WHERE (status = 'queued'::nuq.job_status AND group_id IS NOT NULL);
+
+-- This index helps with owner-specific queries when group_id is NULL
+CREATE INDEX IF NOT EXISTS nuq_queue_scrape_queued_owner_no_group_idx ON nuq.queue_scrape (owner_id, priority ASC, created_at ASC) WHERE (status = 'queued'::nuq.job_status AND group_id IS NULL);
+
CREATE TABLE IF NOT EXISTS nuq.queue_scrape_owner_concurrency (
id uuid NOT NULL,
current_concurrency int8 NOT NULL,