Describe your process for optimizing a slow-running SQL query that processes billions of rows at AWS.
Using the provided PostgreSQL tables, produce the optimized query and explain the validation process, including execution plans, filtering, joins, aggregation, and indexing considerations.
Output
- One row per active account, service, and request date in the requested date range.
- Return
account_name, service_name, request_date, successful_requests, p95_latency_ms, successful_bytes, and daily_change.
- Sort by
account_name, service_name, and request_date ascending.
Output details
- Restrict
request_ts to 2026-01-01 00:00:00+00 inclusive through 2026-01-04 00:00:00+00 exclusive.
- Treat status codes from
200 through 299, inclusive, as successful.
- Return
successful_bytes as 0 when no successful requests exist.
- Return
p95_latency_ms as NULL when no successful requests exist.
- Return
daily_change as NULL for the first date per account and service, otherwise subtract the preceding date's count.