How would you optimize a slow-running query that scans millions of rows across multiple partitioned tables?
Provide a PostgreSQL query that returns the required aggregates for the supplied date range. Keep the result logically correct while demonstrating choices appropriate for large partitioned datasets.
month_start, region_name, total_revenue, order_count, and distinct_customers.month_start ascending, then region_name ascending.| Column | Type | Description |
|---|---|---|
| event_idPK | INT | Unique sales event identifier |
| order_id | VARCHAR(20) | Business order identifier |
| customer_id | INT | Customer associated with the event |
| event_date | DATE | Partition key and sales date |
| amount | NUMERIC(12,2) | Value of the sales event |
| status | VARCHAR(20) | Sales event status |
| Column | Type | Description |
|---|---|---|
| customer_idPK | INT | Unique customer identifier |
| customer_name | VARCHAR(100) | Customer display name |
| segment | VARCHAR(30) | Customer market segment |
| region_id | INT | Customer region |
| is_active | BOOLEAN | Whether the customer is currently active |
| Column | Type | Description |
|---|---|---|
| region_idPK | INT | Unique region identifier |
| region_name | VARCHAR(60) | Region display name |
| country_code | VARCHAR(3) | Country or territory code |