
You are asked to explain how you would optimize a slow PostgreSQL query that builds a monthly customer summary from QVC order, customer, and item-level tables. Focus on how you would diagnose the issue, rewrite the SQL, and preserve correctness.
Reading execution plansOptimizing joins and aggregationsChoosing between CTEs and subqueriesWriting index-friendly date filtersA common pattern is a query that joins qvc_orders, qvc_order_items, and qvc_customers, groups results by customer and month, and runs slowly because too many rows are scanned or joined before filtering.