Large PostgreSQL tables are common in analytics systems, especially when querying high-volume products such as card payments, transfers, or ledger events. A slow query is often caused by a mix of poor filtering, missing indexes, expensive joins, and unnecessary scans.
You are asked to explain how you would optimize a SQL query that performs poorly on a table with millions of rows in PostgreSQL. Walk through how you would diagnose the bottleneck, what you would inspect in the execution plan, when you would add or change indexes, and when you would consider partitioning or query rewrites. Use examples relevant to Revolut-style transaction or ledger data.
The interviewer expects a practical, structured answer rather than generic advice. You should cover both SQL-level changes and table-design choices, and explain the trade-offs of each approach.