How do you handle and optimize queries that involve massive datasets with highly skewed distribution keys at Twilio?
Write a PostgreSQL query that returns monthly delivered-message metrics for eligible Twilio Messaging tenants during Q1 2026. The query should remain correct when one tenant owns a disproportionately large share of events.
tenant_id, account_name, plan_name, month_start, delivered_messages, and average_latency_ms.month_start, then tenant_id.| Column | Type | Description |
|---|---|---|
| event_idPK | BIGINT | Unique messaging event identifier |
| tenant_id | VARCHAR(40) | Tenant that generated the event |
| occurred_at | TIMESTAMPTZ | Event timestamp |
| event_type | VARCHAR(30) | Type of messaging event |
| delivery_status | VARCHAR(20) | Delivery outcome |
| latency_ms | INTEGER | Delivery latency in milliseconds |
| Column | Type | Description |
|---|---|---|
| tenant_idPK | VARCHAR(40) | Twilio customer tenant identifier |
| account_name | VARCHAR(100) | Customer account name |
| region | VARCHAR(20) | Primary customer region |
| plan_id | VARCHAR(30) | Messaging plan reference |
| status | VARCHAR(20) | Tenant lifecycle status |
| is_test | BOOLEAN | Whether the tenant is a test account |
| Column | Type | Description |
|---|---|---|
| plan_idPK | VARCHAR(30) | Messaging plan identifier |
| plan_name | VARCHAR(60) | Human-readable plan name |