<- All articles
SQL

SQL Query Optimization: Five Fixes That Make Slow Queries Fast

A practical guide to finding slow queries with EXPLAIN ANALYZE, adding the right indexes, and rewriting queries so PostgreSQL does less work.

A query that takes 12 seconds and a query that takes 0.2 seconds often return the same rows. The difference is not the data — it is how the database is asked to find it. In this walkthrough, we fix a slow reporting query on a sales database step by step, using PostgreSQL.

The problem

A monthly sales report joins orders, customers and products, filters by date, and groups by product. On 50,000+ rows it takes over 10 seconds, and it gets slower every month as the tables grow. The report is run every morning, so the slowness is felt daily.

Step 1 — See what the database is doing

Before changing anything, run the query with EXPLAIN ANALYZE. It shows the real plan: which scans, joins and sorts PostgreSQL chose, and how long each step took.

EXPLAIN ANALYZE
SELECT p.name, SUM(o.amount) AS total
FROM orders o
JOIN products p ON p.id = o.product_id
WHERE o.order_date >= '2026-01-01'
GROUP BY p.name
ORDER BY total DESC;

Look for two things in the output. First, Seq Scan on a large table means PostgreSQL read every row instead of using an index. Second, the step with the biggest time is where the fix belongs. Never guess — the plan tells you.

Step 2 — Index the columns you filter and join on

The query filters on order_date and joins on product_id. Without indexes on those columns, every run scans the whole table. One index each is usually enough to start.

CREATE INDEX idx_orders_date ON orders (order_date);
CREATE INDEX idx_orders_product ON orders (product_id);

Re-run EXPLAIN ANALYZE after adding them. A Seq Scan should turn into an Index Scan, and the time should drop sharply. An index is not free — it costs a little on every insert — so index the columns your real queries actually use, not every column.

Step 3 — Select only the columns you need

SELECT * forces the database to read and move every column, including large text fields the report never shows. Listing the columns you need is a small change with a real effect on wide tables.

-- Slow: reads every column
SELECT * FROM orders WHERE order_date >= '2026-01-01';

-- Fast: reads only what the report uses
SELECT id, product_id, amount, order_date
FROM orders
WHERE order_date >= '2026-01-01';

Step 4 — Filter before you join

Filtering in the WHERE clause after a join means the database joins everything first and throws rows away after. Filtering early — or pre-aggregating — shrinks the work before the expensive step.

-- Filter early with a CTE so the join sees fewer rows
WITH recent AS (
  SELECT product_id, amount
  FROM orders
  WHERE order_date >= '2026-01-01'
)
SELECT p.name, SUM(r.amount) AS total
FROM recent r
JOIN products p ON p.id = r.product_id
GROUP BY p.name
ORDER BY total DESC;

Step 5 — Re-check, then stop

After each change, re-run EXPLAIN ANALYZE and compare the time. Fix the biggest cost first, then stop when the query is fast enough — over-optimizing a report that already runs in 0.2 seconds wastes time better spent elsewhere. The routine is always the same: measure with EXPLAIN ANALYZE, fix the biggest cost, measure again.

Result

The same monthly report that took over 10 seconds now runs in under half a second — two indexes, a column list instead of SELECT *, and a filter applied before the join. No hardware changed, no data was lost, and the report is ready before the working day starts.