Skip to content

Menu

SQL Indexes and EXPLAIN Cheat Sheet: PostgreSQL and MySQL

Checked against SQL indexes PostgreSQL 19 and MySQL 8.4 manuals; plan examples from SQLite 3.51.0 on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for developers who have a slow query and want to know which index to add and how to prove it helped. Index rules and EXPLAIN options come from the PostgreSQL 19 and MySQL 8.4 manuals, and none of those database-specific rows were run here. The plan snippets in the example come from SQLite's EXPLAIN QUERY PLAN on sqlite3 3.51.0, labelled as such, because the column-order ideas carry over.

When to use what

Default to a plain B-tree index on the columns in your WHERE clause and work from the plan, not from intuition. For a composite index, put the columns you compare with = first and the one you range over or sort by last. Both manuals describe the same leftmost rule: an index on (a, b, c) serves a, a, b and a, b, c, but not b alone. PostgreSQL 19 can sometimes skip-scan a leading column with few distinct values; don't design around that.

Add INCLUDE columns in PostgreSQL when a hot query reads one or two extra columns and you want an index-only scan. Use a partial index in PostgreSQL when queries always target a small slice, like unbilled orders. MySQL's CREATE INDEX has no WHERE clause, so no partial indexes, but it does support functional key parts, so lower(email) style lookups work in both.

Then measure. EXPLAIN shows the plan and estimates; EXPLAIN ANALYZE runs the statement and shows real times and row counts. Compare estimated rows with actual rows first: a big gap means stale statistics, not a missing index. Paste plans into the EXPLAIN visualizer, try the index advisor for a first guess, and read the indexing strategies post for the application side.

Partial indexes: https://www.postgresql.org/docs/current/indexes-partial.html. Expression indexes: https://www.postgresql.org/docs/current/indexes-expressional.html. MySQL syntax: https://dev.mysql.com/doc/refman/8.4/en/create-index.html.

CREATE INDEX orders_cust_status ON orders (customer_id, status);

Composite index serving WHERE customer_id = ? and WHERE customer_id = ? AND status = ?

CREATE INDEX orders_cust_created ON orders (customer_id, created_at);

Equality column first, sort column second, so WHERE customer_id = ? ORDER BY created_at needs no sort step

CREATE INDEX tab_x_y ON tab (x) INCLUDE (y);

PostgreSQL: covering index that stores y in the leaf pages so SELECT y ... WHERE x = ? can be index-only

From the docs, not run by us
CREATE INDEX orders_unbilled ON orders (order_nr) WHERE billed IS NOT TRUE;

PostgreSQL: partial index on just the unbilled rows, small and cheap to maintain

From the docs, not run by us
CREATE INDEX users_lower_email ON users (lower(email));

PostgreSQL: expression index for case-insensitive lookups with WHERE lower(email) = ?

From the docs, not run by us
CREATE INDEX idx1 ON t1 ((col1 + col2));

MySQL 8.4: functional key part; note the extra parentheses around the expression

From the docs, not run by us
CREATE INDEX ix ON t1 (col1, col2(10));

MySQL: index only the first 10 characters of a string column (prefix key part)

From the docs, not run by us

The node-by-node walkthrough is at https://www.postgresql.org/docs/current/using-explain.html. Costs are in planner units, not milliseconds.

EXPLAIN SELECT ...;

Show the chosen plan with estimated cost=startup..total, rows and width per node

Try it: sql-explain-visualizerFrom the docs, not run by us
EXPLAIN ANALYZE SELECT ...;

Run the query and add actual time, actual rows and loops to each node

Try it: sql-explain-visualizerFrom the docs, not run by us
BEGIN; EXPLAIN ANALYZE UPDATE ...; ROLLBACK;

Time a write without keeping its effects, since ANALYZE really executes the statement

From the docs, not run by us
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) SELECT ...;

Machine-readable plan with shared buffer hits and reads per node

Try it: sql-explain-visualizerFrom the docs, not run by us
EXPLAIN (ANALYZE, SERIALIZE) SELECT ...;

Also measure the cost of converting the result to text or binary for the client

From the docs, not run by us
Seq Scan / Index Scan / Index Only Scan / Bitmap Heap Scan

Node names to look for: full table read, index then table, index alone, or index bitmap then table pages

From the docs, not run by us
Rows Removed by Filter: 9990

Rows read and thrown away; a large number next to a Seq Scan usually means a missing index

From the docs, not run by us

Statement syntax and EXPLAIN ANALYZE are at https://dev.mysql.com/doc/refman/8.4/en/explain.html. EXPLAIN ANALYZE always uses the TREE format.

EXPLAIN SELECT ...;

Tabular plan; read the type, key, rows and Extra columns first

Try it: sql-explain-visualizerFrom the docs, not run by us
EXPLAIN FORMAT=TREE SELECT ...;

Show the plan as nested iterators, closer to how PostgreSQL prints it

From the docs, not run by us
EXPLAIN ANALYZE SELECT ...;

Run the query and print estimated cost and rows next to actual time, rows and loops for each iterator

From the docs, not run by us
type: ALL

Full table scan for each combination of earlier rows; usually the one to fix

From the docs, not run by us
type: const / eq_ref / ref / range / index

Better access types: single row, one row per join key, rows matching a key, a key range, or a full index scan

From the docs, not run by us
Extra: Using index

Answered from the index alone without reading table rows (a covering index)

From the docs, not run by us
Extra: Using filesort / Using temporary

MySQL needs an extra sort pass or a temporary table; an index matching ORDER BY or GROUP BY can remove it

From the docs, not run by us

These lines are SQLite output, run on 3.51.0. They are here because the column-order rules are easy to see in them, not because Postgres or MySQL print the same thing.

EXPLAIN QUERY PLAN SELECT * FROM orders WHERE customer_id = 7 AND status = 'paid';

With no index, prints SCAN orders; after the composite index, SEARCH orders USING INDEX orders_cust_status (customer_id=? AND status=?)

EXPLAIN QUERY PLAN SELECT * FROM orders WHERE status = 'paid';

Still SCAN orders: status is the second column, so the index can't be used on its own

EXPLAIN QUERY PLAN SELECT status FROM orders WHERE customer_id = 7;

SEARCH ... USING COVERING INDEX: every column needed is in the index

EXPLAIN QUERY PLAN SELECT * FROM orders WHERE customer_id = 7 ORDER BY created_at;

Adds USE TEMP B-TREE FOR ORDER BY until an index on (customer_id, created_at) exists

Worked example: Watch a composite index work, and not work

This is SQLite 3.51.0, not Postgres or MySQL, but it shows the leftmost-column rule in four short plans:

sqlite> CREATE TABLE orders(id INTEGER PRIMARY KEY, customer_id INT, status TEXT, created_at TEXT, total INT, email TEXT);
sqlite> CREATE INDEX orders_cust_status ON orders(customer_id, status);
sqlite> EXPLAIN QUERY PLAN SELECT * FROM orders WHERE customer_id=7 AND status='paid';
`--SEARCH orders USING INDEX orders_cust_status (customer_id=? AND status=?)
sqlite> EXPLAIN QUERY PLAN SELECT * FROM orders WHERE status='paid';
`--SCAN orders
sqlite> EXPLAIN QUERY PLAN SELECT * FROM orders WHERE customer_id=7 ORDER BY created_at;
|--SEARCH orders USING INDEX orders_cust_status (customer_id=?)
`--USE TEMP B-TREE FOR ORDER BY
sqlite> CREATE INDEX orders_cust_created ON orders(customer_id, created_at);
sqlite> EXPLAIN QUERY PLAN SELECT * FROM orders WHERE customer_id=7 ORDER BY created_at;
`--SEARCH orders USING INDEX orders_cust_created (customer_id=?)

The first plan uses both index columns. The second can't use the index at all, because status is not the leading column. The third finds the customer's rows but then sorts them in a temporary B-tree. After adding (customer_id, created_at) the rows come out of the index already in order and the sort step is gone. An expression index on lower(email) also showed up as a covering SEARCH for WHERE lower(email) = ?, but a plain WHERE email = ? went back to SCAN, because the index stores the lowered value. In PostgreSQL you would confirm the same changes with EXPLAIN ANALYZE and check that the sort node disappears; in MySQL, that Using filesort leaves the Extra column.

Common mistakes

  • Creating one single-column index per WHERE column and hoping the database combines them.

    For a query you run constantly, build one composite index with equality columns first and the range or sort column last.

  • Wrapping an indexed column in a function, like WHERE lower(email) = ? or WHERE date(created_at) = ?.

    Either index that exact expression or rewrite the condition as a range on the raw column.

  • Running EXPLAIN ANALYZE on a DELETE or UPDATE to see the plan.

    It really runs the statement. Wrap it in BEGIN and ROLLBACK, or use plain EXPLAIN.

  • Reading the cost numbers as milliseconds.

    Cost is an arbitrary planner unit. Use the actual time figures from EXPLAIN ANALYZE, and compare estimated rows with actual rows.

  • Adding indexes to every column of a write-heavy table.

    Each index has to be updated on every insert and on updates to its columns. Keep the ones your plans actually use and drop the rest.

  • Expecting partial indexes in MySQL.

    MySQL 8.4 has prefix and functional key parts but no WHERE clause on CREATE INDEX. Use a generated column or a different design.