Faragiri

Learn Indexes SQL

A lookup measured with and without an index, why the second column of a composite index cannot be used alone, and the three ways to make an index useless.

4Sections
3Min read
4Depth

Recommended Before This

0 / 2
  • SQL
  • SELECT

Two hundred thousand loans, one lookup by borrower. Measured on MySQL 8.4 on
2026-09-02: without an index the query took 0.0239 s and
EXPLAIN reported type=ALL key=NULL rows=200000, which
is the engine reading every row. Building the index took 0.125 s. After it, the
same query took 0.000033 s and the plan reported
type=ref key=idx_borrower rows=1.

The numbers are large and the plan is the part to learn.
rows=200000 against rows=1 is the engine telling you
what it expects to examine, and it says so before the query is run at scale. A
query with a row estimate near the table's size is doing a full scan whatever
the timing says on a small development database.

Column order in a composite index is the whole of it

With an index on (town, due), three queries and three plans:

One index on (town, due)

Which queries this composite index can narrow

An index is usable from the left, so column order determines which filter narrows rows and which falls back to a scan.

  1. YesWHERE town = 'T042'Leading column matches. Examines 400 rowsYes
  2. YesWHERE town = 'T042' AND due = '2026-03-15'Both columns, left to right. Examines 19 rowsYes
  3. NoWHERE due = '2026-03-15'Second column alone. Scans the whole index, 19968 rowsNo
  4. NoAdd a new column before townChanges the left prefix and breaks existing queries on townNo

Column order is a decision about which queries the index serves, and adding a column to the front changes what an existing index is for.

Interactive Practice Lab

Edit and run this snippet directly in your browser (zero setup)

SQL
CREATE TABLE workshop_orders (id INTEGER PRIMARY KEY, supplier TEXT, qty INTEGER, city TEXT);INSERT INTO workshop_orders VALUES (1, 'Sepehr', 15, 'Ardabil'), (2, 'Aria', 47, 'Tabriz'), (3, 'Sepehr', 22, 'Ardabil'), (4, 'Nima', 31, 'Tehran');SELECT supplier, COUNT(*) AS deliveries, SUM(qty) AS total_qtyFROM workshop_ordersGROUP BY supplierORDER BY total_qty DESC;

Live Big-O Complexity & Binary Search Race Lab

Compare complexity curves at scale and watch Linear vs Binary Search race live.

  • WHERE town = 'T042' uses it, examining 400 rows
  • WHERE town = 'T042' AND due = '2026-03-15' uses it better,
    examining 19
  • WHERE due = '2026-03-15' alone falls back to scanning the whole
    index, 19968 rows, because due is the second column

An index is usable from the left. A query filtering only on a later column
cannot use it to narrow, which is why the order of the columns is a decision
about which queries the index serves and why adding a column to the front of an
existing index changes what it is for.

Three ways to make an index useless

Wrap the column in a function.
WHERE UPPER(borrower) = 'B142857' reported
rows=199680: the index holds the values, not the uppercased values,
so the engine cannot use it to narrow. The fix is either to store the value in
the form you search on or to create an index on the expression.

Rows examined by EXPLAIN

Three queries that defeat the borrower index

Wrapping a column in a function or putting a wildcard at the start removes the ordered starting point the engine needs.

rows examined in EXPLAIN plan

  1. UPPER(borrower) = 'B142857'Index stores raw values, not uppercased ones199680
  2. LIKE '%857' (leading wildcard)No leading characters means no starting point199680
  3. LIKE 'B14%' (prefix range)Ordered prefix gives the engine a starting range18242

Implicit type conversion causes the same full scan: comparing a string column to a number forces the engine to convert every row.

Put a wildcard at the front. LIKE 'B14%' is a range and examines
18242 rows; LIKE '%857' examines 199680, because an index is
ordered by the start of the value and a leading wildcard has no starting point.
And implicit type conversion does the same thing: comparing a string column to a
number makes the engine convert every row.

What to run in 2026

EXPLAIN is the tool, free with every engine, and the three
columns to read are the access type, the key chosen and the row estimate. It
replaced timing queries on a development database, which has too few rows for
the difference to appear at all.

What to run in 2026

Moving from small dev timings to plan-first indexing

Small development tables hide full scans, so two engine-level checks replace guesswork before queries reach scale.

Small dev timingsToo few rows to expose full scans or extra table lookups
  1. 01Run EXPLAIN firstCheck access type, key chosen, and row estimate
  2. 02Cover the needed columnsInclude SELECT town, due in the index so the table is never touched
  3. 03Measure without guessingConfirm why an index sped up a query or barely moved it
Plan-verified index readsPredictable access paths checked before production load

Both EXPLAIN and covering indexes are free in every SQL engine and cost nothing extra to inspect.

A covering index is the second idea worth knowing, free with any engine.
When every column a query needs is in the index, the engine answers from the
index and never touches the table, which is why
SELECT town, due against the (town, due) index is
faster than selecting a column that is not in it. It replaced adding an index
and wondering why the improvement was smaller than expected.

Where this stalls people

The stall is indexing everything. An index is a second copy of the column
kept in order, so every insert, update and delete has to maintain it, and a
table with eight indexes is a table where writes cost eight times the
bookkeeping. Storage grows too, and the optimiser has more choices to get
wrong.

Where indexing stalls people

What eight indexes actually cost beneath the reads

Indexing every column looks safe on reads while quietly multiplying write work, storage, and plan choices.

  • Single-column read lookupsAd-hoc filters feel faster in isolation
Visible query speedups
  • Write bookkeeping on every row changeEvery insert, update, and delete must maintain each index copy
  • Growing index storageEach index keeps another ordered copy of its columns
  • More optimiser choices to get wrongExtra indexes widen the search space for query planning
  • Missed composite pairsTwo single-column indexes lose to one composite index for common pairs

Use the slow query log to name the queries you actually run, then index only for those patterns.

Index for the queries you actually run, and find them rather than guessing:
the slow query log names them, and a composite index for a common pair beats two
single column ones for the same pair. Where those queries come from is
choosing rows and columns, the
joins that most need an index are
combining tables, and the four
kinds
, and reading a plan properly is
reading a
plan, and fixing the query it describes
, inside
the language for asking a database
questions
.

Alongside this

Above this