← All posts

Read a query plan: five rules for fixing slow SQL

November 25, 2026

“The query is slow” is not a diagnosis. The database already knows why it is slow, and it will tell you if you ask. The tool is called a query plan, and reading one is the most useful SQL skill you can pick up in an afternoon.

All examples below ran on SQLite, but the ideas carry over to Postgres and MySQL. Only the wording of the plan changes (SCAN becomes Seq Scan in Postgres, SEARCH ... USING INDEX becomes Index Scan).

Setup

An orders table with 20,000 rows and 500 customers:

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INT,
  status TEXT,
  created_at TEXT,
  total INT
);

To see the plan, put EXPLAIN QUERY PLAN in front of any query (EXPLAIN alone in Postgres, and EXPLAIN ANALYZE if you also want real timings).

Rule 1: Know the two words that matter

EXPLAIN QUERY PLAN SELECT * FROM orders WHERE customer_id = 42;
SCAN orders

SCAN means the database reads every row and checks each one. With 20,000 rows that is fine. With 20 million it is the reason your page times out.

Add an index and ask again:

CREATE INDEX idx_orders_customer ON orders(customer_id);
SEARCH orders USING INDEX idx_orders_customer (customer_id=?)

SEARCH means it jumped straight to the matching rows, the way you’d use a book’s index instead of reading every page. When you see SCAN on a big table behind a filter, that is your first suspect.

Rule 2: Make the index match the whole query

Now add sorting: the 10 latest orders for a customer.

SELECT * FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC LIMIT 10;
SEARCH orders USING INDEX idx_orders_customer (customer_id=?)
USE TEMP B-TREE FOR ORDER BY

The index finds the customer’s rows, but then the database has to collect them all and sort them. That second line is the cost. A composite index fixes both steps at once:

CREATE INDEX idx_orders_cust_created ON orders(customer_id, created_at);
SEARCH orders USING INDEX idx_orders_cust_created (customer_id=?)

The temp sort is gone. Inside each customer, the index is already ordered by date, so the database reads the first 10 entries and stops.

Rule 3: Column order is not optional

A composite index works like a phone book sorted by last name, then first name. You can look up “Smith”, or “Smith, Anna”. You can’t use it to find everyone called “Anna”.

With the index on (customer_id, created_at):

-- both columns, in order: uses the index fully
WHERE created_at > '2026-06-01' AND customer_id = 42
-- SEARCH ... (customer_id=? AND created_at>?)

-- only the second column: the index can't help
WHERE created_at = '2026-06-01'
-- SCAN orders

The rule of thumb: equality columns first, then the range or sort column. And a query can only use the leading part of an index.

Rule 4: Don’t hide the column from the index

Indexes store the column’s raw values. Wrap the column in an expression and the database can’t use them:

WHERE customer_id + 0 = 42
-- SCAN orders

WHERE lower(status) = 'paid'
-- SCAN orders

Move the work to the other side of the comparison (customer_id = 42) or, when you really need the function, create an index on the expression itself (supported by Postgres and SQLite, and by MySQL 8 as a functional index). Typical offenders: lower(col), date(col), col + 1, and in many databases comparing a column to a value of a different type.

Rule 5: Cover the query and skip the table

If an index contains every column the query needs, the database never touches the table at all:

CREATE INDEX idx_orders_cust_total ON orders(customer_id, total);

SELECT total FROM orders WHERE customer_id = 42;
SEARCH orders USING COVERING INDEX idx_orders_cust_total (customer_id=?)

COVERING INDEX is the good news in that line. It is the fastest way to answer a query, and the reason SELECT * can quietly cost you: asking for every column rules out covering.

When an index will not help

Indexes aren’t free. Every one slows down each INSERT, UPDATE and DELETE on the table, and takes disk space. Skip them when:

  • The table is small. Scanning 500 rows is faster than any clever lookup.
  • The column has few distinct values. In our table, 90% of orders have status = 'paid'. An index on status can’t narrow much, and reading most of a table through an index is slower than a plain scan. Real databases track statistics and often ignore such an index on purpose.
  • The query returns most of the table anyway. The index only helps when it cuts the data down.
  • You wrote it for a query that no longer runs. Unused indexes are pure cost. Find them and drop them.

A habit worth building

  1. Find the slow query (your database’s slow query log, or your APM).
  2. Run it under EXPLAIN. Look for SCAN on big tables, sorts, and large row estimates.
  3. Change one thing: an index, a rewrite of the condition, a narrower SELECT.
  4. Run EXPLAIN again and check that the plan changed the way you expected.

Guessing at indexes is how tables end up with fifteen of them. Reading the plan first is how you end up with the two that matter.

Practise on a real database

Paren's SQL Performance course has you read plans, build indexes and watch the plan change, with every query running in your browser.

Start free