How do you optimize slow-running SQL queries in a relational database containing millions of patient records?
Using the provided PostgreSQL tables, write a query that returns active patients whose latest appointment was on or after 2025-01-01, along with their active prescription count. Consider how the query would perform at scale.
Output
- One row per qualifying active patient with
patient_id, full_name, latest_appointment_date, and active_prescription_count.
- Include only active patients with a qualifying appointment, ordered by appointment date descending, then
patient_id ascending.