How do you optimize query performance and manage storage costs in Amazon Redshift?
Using the supplied query history, table usage, and storage metadata, write a SQL query that identifies tables requiring optimization attention. Consider completed queries executed during January 2025 and include tables with no qualifying query activity.
Output
- One row per stored table with
table_name, query_count, total_runtime_ms, avg_runtime_ms, storage_mb, and optimization_priority.
- Classify priority as
HIGH, MEDIUM, or LOW using runtime, storage, dead-space, and unused-table signals.
- Order by total runtime descending, then storage size descending.
Output details
- Treat January as
executed_at >= TIMESTAMP '2025-01-01 00:00:00' and < TIMESTAMP '2025-02-01 00:00:00'.
- Round
avg_runtime_ms and storage_mb to two decimal places, leaving avg_runtime_ms NULL when no qualifying queries exist.
- Use zero for
query_count and total_runtime_ms when no qualifying queries exist, while preserving NULL averages.
- Compute
storage_mb as total_bytes / 1048576.0 before rounding.
- Assign
HIGH for total runtime at least 1000, dead-space ratio at least 0.40, or unused storage at least 10 MiB.
- Otherwise assign
MEDIUM for unused tables, total runtime at least 500, or dead-space ratio at least 0.20; assign LOW otherwise.