Optimize a slow SQL query involving complex joins and window functions, and explain how you would improve performance.
Use the provided PostgreSQL schema to produce the requested result and demonstrate an optimized query structure. Explain how you would validate the improvement with execution plans, indexing, filtering, and join reduction.
Output
- One row per customer, product category, and reporting month for qualifying orders.
- Columns:
customer_id, customer_name, category, reporting_month, revenue, prior_period_revenue, and revenue_change.
- Include completed orders from January through March 2025, exclude rows without a customer or category, and order by customer, category, and month.