Explain how you would optimize a query that is joining a massive claims table with a sparse patient activity table.
Implement a representative PostgreSQL query that returns eligible claims with activity summarized for the same quarter. The result should avoid duplicate claim rows and include only claims whose patients had qualifying activity.
claim_id, patient_id, claim_date, billed_amount, latest_activity_at, and activity_event_count.claim_date ascending, then claim_id ascending.| Column | Type | Description |
|---|---|---|
| claim_idPK | INT | Unique claim identifier |
| patient_id | INT | Patient associated with the claim |
| claim_date | DATE | Date the claim was submitted |
| claim_status | VARCHAR(20) | Current processing status |
| billed_amount | NUMERIC(12,2) | Amount billed for the claim |
| Column | Type | Description |
|---|---|---|
| activity_idPK | INT | Unique activity event identifier |
| patient_id | INT | Patient associated with the activity event |
| activity_at | TIMESTAMP | Timestamp when the activity occurred |
| activity_type | VARCHAR(30) | Type of patient activity |