perf: Optimize dashboard queries for faster load times

- Use pg_stat for approximate product count (instant vs full scan)
- LIMIT on DISTINCT queries for brand/category counts
- Single combined query (reduces round trips)
- Add index on store_product_snapshots.captured_at
- Add index on worker_tasks.worker_id and created_at

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Kelly
2025-12-13 01:09:02 -07:00
parent 291a8279bd
commit 271faf0f00
3 changed files with 33 additions and 54 deletions

View File

@@ -0,0 +1,11 @@
-- Migration: Add indexes for dashboard performance
-- Speeds up the tasks listing query with ORDER BY and JOIN
-- Index for JOIN with worker_registry
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_worker_tasks_worker_id
ON worker_tasks(worker_id)
WHERE worker_id IS NOT NULL;
-- Index for ORDER BY created_at DESC (dashboard listing)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_worker_tasks_created_at_desc
ON worker_tasks(created_at DESC);