A database index is an extra, ordered structure that lets the database find the rows it needs without reading the whole table. It works like the index at the back of a thick book: instead of leafing through 800 pages looking for a word, you look up the page number and open the right page straight away.
In short
- Without an index, the database reads every row in turn. With one, it jumps straight to the right rows.
- In my test on a million rows, finding a customer by email went from 209 ms to 0.09 ms.
- An index takes space and slows down writes, so you create one for specific queries, not "for everything".
The phone book
Remember phone books? Names were sorted alphabetically, so finding Smith's number took seconds: open roughly in the middle, check whether S is before or after, and so on. Now try finding whose number is 0117 496 0123. The book is sorted by name, not by number, so you are left reading it cover to cover.
A database index is exactly that kind of sorting, only extra: the table stays as it was, and next to it sits an ordered list of one column's values (emails, say) with a pointer to each row. You can index several columns, as if separate lists sat next to the phone book: by name, by number, by street.
How much does it help?
I measured it on an orders table with a million rows in MySQL 5.7 (email, city, amount, date):
- finding one customer by email - 209 ms without an index, 0.09 ms with one. Over 2,000 times faster.
- the orders from one day - 243 ms without an index, 1.5 ms with an index on the date.
209 milliseconds sounds harmless. But a page that runs a few such queries every time it loads, with a few dozen visitors at once, starts to choke. With an index, the same database will not even notice. Putting EXPLAIN before a query shows how many rows the database plans to read: a million without the index, one with it.
CREATE UNIQUE INDEX email ON orders (email);
EXPLAIN SELECT * FROM orders WHERE email = '[email protected]';
Composite indexes: order matters
"How many orders from one city since the start of the year" (Gdańsk, in my test data) filters on two columns. This is where it gets interesting:
- an index on the date alone - 185 ms, the same as no index. Almost every order in the table matched the date, so the database decided the index was useless and read everything.
- a composite index (city, date) - 5 ms.
- a composite index (date, city) - 30 ms. The same columns in the opposite order, six times slower.
Why? A composite index sorts by the first column, then by the second - like a phone book sorted by surname, then by first name within each surname. "Gdańsk, from 1 January" is one compact stretch of a (city, date) index: all of Gdańsk, with its dates in order. In a (date, city) index, the Gdańsk orders are scattered across the whole date range. The rule: the column compared with "equals" first, the one with a range second.
When an index does not help
- Searching from the middle -
WHERE email LIKE '%@gmail.com'cannot use an ordinary index, because there is nowhere to start. Like finding everyone in the phone book whose surname ends in "-son". - A function on the column -
WHERE YEAR(created) = 2026instead ofcreated >= '2026-01-01'. The database has to compute the function for every row. - A column with few values, such as yes/no. An index on it rarely helps, as half the table matches anyway.
- A small table - with a few hundred rows, reading everything is faster than using an index.
What an index costs
Indexes are not free. In my test the table took 68 MB and its indexes 108 MB - more than the data itself. Every INSERT and UPDATE also has to update all of the table's indexes, so writes get slower (although in my bulk INSERT test the difference was smaller than I expected). So you create indexes for the queries you actually run, not for every column.
And partitions?
Partitioning splits one table into pieces, by year for example. A query about 2026 then reads only the 2026 piece, and old data goes by dropping a whole partition instead of deleting millions of rows. It sounds like an index, but it is a tool for another scale: tens or hundreds of millions of rows, with data that splits naturally over time. For an online shop's or a blog's tables, good indexes will do more.
When it matters to you
- Your site slows down as posts, orders or customers pile up - often a single query without an index is to blame. Diagnostic plugins (in WordPress, Query Monitor for one) show the slowest queries.
- You are writing your own application - index the columns you search, join and sort on, and make
EXPLAINa habit for every new query. - Searching the text of articles - an ordinary index is not enough here. You need a full-text index or a separate search engine. A relational database is great at finding specific values, less so at "find me something similar".
Related terms
primary key, EXPLAIN, full-text index, partitioning, cache, server.