What strategies would you use to optimize a SQL query for performance?
Explain your approach and provide a PostgreSQL query that returns customer totals for completed orders from January 1 through March 31, 2025. Include the reasoning behind query structure, filtering, indexing, and plan inspection.
Output
- One row per qualifying customer with
customer_id, customer_name, completed_orders, and total_spend.
- Include customers with at least two completed orders in the date range.
- Sort by
total_spend descending, then customer_id ascending.