How would you optimize a slow-running SQL query that involves multiple joins and aggregations on tables containing millions of rows?
Using the supplied schema, write the query that returns filtered completed-order totals by region, month, and product category. Explain the performance decisions and how you would validate them with PostgreSQL execution plans.
Output
- One row per region, calendar month, and product category.
- Columns:
region, order_month, category, order_count, units_sold, and gross_revenue.
- Include completed orders from 2024-01-01 through 2024-03-31, sort by
order_month, region, and category.