How do you optimize a complex SQL query that is performing poorly?
Using the provided PostgreSQL tables, write an optimized query that returns broker performance for active policies starting in 2025. The result should preserve the required filters, aggregations, and inclusion of brokers without matching claims.
Output
- One row per qualifying broker, with
broker_id, broker_name, active_policy_count, total_premium, claim_count, and total_claim_amount.
- Include only brokers whose 2025 active-policy premium exceeds 1,000.
- Sort by
total_premium descending, then broker_id ascending.