Skip to the page
Tecyfy

AWS interview question · hard

How do you debug and optimize slow-running queries in Amazon Redshift?

The short answerFind the slow queries in the system views, read their plans and alerts to see why they are slow, then fix the cause, which is usually table design, stale statistics, data movement between nodes, or competition for resources.

The full answer

Find the slow queries in the system views, read their plans and alerts to see why they are slow, then fix the cause, which is usually table design, stale statistics, data movement between nodes, or competition for resources.

1. Find the slow queries

SELECT query_id, status, elapsed_time / 1000000.0 AS seconds,
       queue_time / 1000000.0 AS queued_seconds, TRIM(query_text) AS sql
FROM sys_query_history
WHERE query_type = 'SELECT' AND start_time > dateadd(day, -1, getdate())
ORDER BY elapsed_time DESC
LIMIT 10;

SYS_QUERY_HISTORY works on provisioned clusters and Serverless; times are in microseconds. A long queue time points to workload management, not the query itself. SYS_QUERY_DETAIL shows the steps of each query.

2. See why

  • EXPLAIN shows the plan. Look for DS_BCAST_INNER or DS_DIST_BOTH (large data movement between nodes), nested loop joins, and large row estimates.
  • STL_ALERT_EVENT_LOG (provisioned clusters) records the optimizer's own warnings with a suggested fix: missing statistics, nested loops, very selective filters scanning many rows, ghost rows waiting for vacuum, and large distribution or broadcast steps.
  • SVV_TABLE_INFO shows each table's distribution style, sort key, skew_rows, unsorted percentage and stats_off.
  • The Redshift console's query monitoring shows the same in charts, and Advisor gives recommendations.

3. Fix table design

  • Distribution: use DISTSTYLE AUTO or a KEY on the column large tables are joined on, so joins happen on the same node; ALL for small dimension tables.
  • Sort keys on columns used in range filters (often dates), so zone maps skip blocks.
  • Automatic table optimisation chooses these from real queries when tables are set to AUTO; check what it chose before overriding.
  • Compression: ENCODE AUTO.

4. Keep tables healthy

  • Redshift runs VACUUM and ANALYZE automatically in the background; after large loads or deletes, run ANALYZE (and VACUUM if unsorted is high) yourself.

5. Fix the query

  • Select only needed columns; filter early, on sort-key columns, without wrapping them in functions.
  • Join on columns of the same type; avoid cross joins.
  • Pre-compute repeated heavy logic in materialized views with auto refresh.

6. Manage concurrency

  • Automatic WLM with query priorities, configured in the cluster's parameter group (not in SQL), plus query monitoring rules to log or stop runaway queries.
  • Short query acceleration and concurrency scaling for peaks; on Serverless, raise base capacity.

The order matters: measure queue versus execution time first, then read the plan and alerts, and only then change tables or capacity.

Last updated 16 October 2026