diff --git a/backend/src/routes/consumer-favorites.ts b/backend/src/routes/consumer-favorites.ts index e9eccb2d..2b2b0a84 100644 --- a/backend/src/routes/consumer-favorites.ts +++ b/backend/src/routes/consumer-favorites.ts @@ -75,6 +75,7 @@ router.get('/', async (req: Request, res: Response) => { }); } else if (domain === 'findadispo.com') { // Dispensary favorites + // Use denormalized product_count column (faster than correlated subquery) const result = await pool.query( `SELECT f.*, d.name as current_name, @@ -88,7 +89,7 @@ router.get('/', async (req: Request, res: Response) => { d.hours, d.latitude, d.longitude, - (SELECT COUNT(*) FROM store_products WHERE dispensary_id = d.id AND stock_status = 'in_stock') as product_count + COALESCE(d.product_count, 0) as product_count FROM findadispo_favorites f LEFT JOIN dispensaries d ON f.dispensary_id = d.id WHERE f.user_id = $1 diff --git a/backend/src/routes/pipeline.ts b/backend/src/routes/pipeline.ts index 7d045259..6e53e473 100644 --- a/backend/src/routes/pipeline.ts +++ b/backend/src/routes/pipeline.ts @@ -679,11 +679,11 @@ router.post('/stores/:id/approve', async (req: Request, res: Response) => { const dispensaryId = parseInt(id, 10); try { - // Get the dispensary with product count + // Get the dispensary with product count (use denormalized column) const { rows } = await pool.query(` SELECT d.*, dcp.id as profile_id, - (SELECT COUNT(*) FROM store_products sp WHERE sp.dispensary_id = d.id) as product_count + COALESCE(d.product_count, 0) as product_count FROM dispensaries d LEFT JOIN dispensary_crawler_profiles dcp ON dcp.dispensary_id = d.id AND dcp.enabled = true WHERE d.id = $1 @@ -760,7 +760,7 @@ router.post('/stores/approve-batch', async (req: Request, res: Response) => { try { let query = ` SELECT d.id, d.name, - (SELECT COUNT(*) FROM store_products sp WHERE sp.dispensary_id = d.id) as product_count + COALESCE(d.product_count, 0) as product_count FROM dispensaries d WHERE d.stage = 'sandbox' `;