How would you optimize a SQL query for performance?
Using the supplied PostgreSQL schema, write an optimized query that calculates billed totals for qualifying active accounts. The answer should be suitable for explaining how you would validate query performance and reduce unnecessary work.
Output
- One row per active account with completed invoices in the requested date range.
- Columns:
region, account_id, account_name, total_billed, and account_rank.
- Sort by
region ascending with NULL regions last, then account_rank ascending and account_id ascending.