How do you optimize a SQL query that is running slowly on a very large database?
Use the provided PostgreSQL schema as the working example. Produce the result for completed orders from the first quarter of 2025, and explain how you would investigate and improve performance at scale.
Output
- One row per customer with qualifying orders.
- Columns:
customer_id, customer_name, region, order_count, and total_revenue.
- Include only completed orders dated from 2025-01-01 through 2025-03-31, with revenue above 500.
- Sort by
total_revenue descending, then customer_id ascending.