How would you optimize a slow-running SQL query that joins multiple large datasets?
Using the provided PostgreSQL schema, write an optimized query that returns completed transfer performance for the first quarter of 2025. The answer should also make clear which query-planning and indexing decisions support performance at scale.
Output
- One row per corridor, calendar month, and customer segment.
- Columns:
corridor_code, month_start, customer_segment, completed_transfer_count, completed_amount, and average_transfer_amount.
- Include only transfers whose latest event is
completed, excluding unmatched customers or corridors.
- Order by
corridor_code, month_start, and customer_segment, with NULL segments last.