At Acme Retail, a simple reporting query has become slow as the sales table has grown. Interviewers use this kind of question to test whether you understand practical SQL performance tuning, not just syntax.
Explain how you would optimize a slow SQL query in PostgreSQL to improve performance. Your answer should cover:
You may reference common patterns such as unnecessary SELECT *, filtering late instead of early, functions on indexed columns, avoidable sorting, and inefficient aggregations.
Keep the discussion at an easy interview level: focus on a clear, structured optimization process, practical examples, and the most common performance mistakes candidates should recognize in PostgreSQL.
Optimization should start with measurement. In PostgreSQL, EXPLAIN ANALYZE shows the execution plan, actual row counts, timing, and whether the database is scanning far more data than expected.
EXPLAIN ANALYZE
SELECT customer_id, SUM(amount)
FROM sales
WHERE sale_date >= DATE '2024-01-01'
GROUP BY customer_id;
Queries often slow down because they read more rows and columns than needed. Restricting rows in the WHERE clause and selecting only required columns reduces I/O, memory use, and sort work.
SELECT customer_id, amount
FROM sales
WHERE status = 'completed';
Indexes are most useful for selective filters, joins, and some sorts. They are less helpful when a query needs most rows in a table, and they can be bypassed if the query applies functions to indexed columns.
CREATE INDEX idx_sales_sale_date ON sales (sale_date);
Wrapping indexed columns in functions can make PostgreSQL ignore an otherwise useful index. Rewriting predicates into index-friendly ranges often improves performance significantly.
SELECT *
FROM sales
WHERE sale_date >= DATE '2024-01-01'
AND sale_date < DATE '2024-02-01';
A query is only optimized if the new version is measurably better. Compare execution plans, runtime, rows scanned, and whether the result set remains correct after changes.
EXPLAIN ANALYZE
SELECT region, SUM(amount)
FROM sales
WHERE status = 'completed'
GROUP BY region;