Database Indexes Explained: How They Make Queries Fast (With Visuals)
A visual guide to database indexes: full table scans vs index seeks, how an index is structured, what it costs on writes, and when to add one.
Published November 27, 2026
A query that takes four seconds on a million-row table can take two milliseconds after adding one line of SQL. That line creates an index. Indexes are the single most important performance tool in any relational database, yet many developers treat them as magic. This article explains what an index physically is, why it is fast, and what it costs.
The concept
Without an index, the database has only one way to find rows matching WHERE email = 'a@b.com': read every row in the table and check it. That is a full table scan, and its cost grows linearly with table size. On small tables it is fine; on a table with tens of millions of rows it means reading gigabytes from disk for a single lookup.
An index is a separate data structure that keeps a copy of one or more columns in sorted order, each entry pointing back to its row. Because it is sorted, the database can binary-search it, or more precisely walk a B-tree, to jump straight to the matching entries. Finding one row among 100 million takes about four page reads instead of millions.
The cost is paid on writes and storage. Every INSERT must add an entry to every index on the table, every UPDATE to an indexed column must move entries, and every DELETE must remove them. Indexes also take disk space and memory. Good indexing is about adding exactly the indexes your queries need, and no more.
Scan vs seek
What an index looks like inside
The phone book analogy
A phone book sorted by surname is an index on surname. Finding "Sharma" is quick because you can open near S and narrow down. Finding everyone who lives on "MG Road" is slow, because the book is not sorted by street: you must read every page. A second book sorted by street would be another index. Keeping both books up to date whenever someone moves is the write cost.
What an index speeds up (and slows down)
| Operation | Without index | With index on the column |
|---|---|---|
| WHERE col = value | Full scan O(n) | Seek O(log n) |
| WHERE col BETWEEN a AND b | Full scan | Seek to a, scan forward to b |
| ORDER BY col LIMIT 10 | Scan + sort everything | Read first 10 index entries |
| JOIN ON col | Scan for each outer row (or hash join) | Seek per outer row |
| INSERT / UPDATE / DELETE | Write the row | Write the row + maintain each index |
Practical examples
Before and after
Create the index and compare plans.
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'max@example.com';
-- Seq Scan on users (rows=1) (actual time=0.02..412.7 ms) Rows Removed by Filter: 999999
CREATE INDEX idx_users_email ON users (email);
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'max@example.com';
-- Index Scan using idx_users_email on users (actual time=0.03..0.04 ms)Unique indexes enforce rules
An index can also guarantee data integrity.
CREATE UNIQUE INDEX uq_users_email ON users (lower(email)); -- PostgreSQL expression index
-- MySQL 8: ALTER TABLE users ADD UNIQUE KEY uq_users_email ((lower(email)));List indexes on a table
Know what already exists before adding more.
-- PostgreSQL
SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'users';
-- MySQL
SHOW INDEX FROM users;Common mistakes
- Indexing every column "just in case", slowing writes and wasting memory.
- Never checking EXPLAIN to see if an index is actually used.
- Indexing low-selectivity columns alone (a boolean is_active) and expecting big gains.
- Forgetting that primary keys and unique constraints already create indexes.
- Creating indexes on large production tables without a concurrent/online option and locking writes.
Key takeaways
- An index is a sorted copy of column values with pointers to rows.
- It turns full scans into logarithmic seeks and helps ranges, sorting and joins.
- Every index slows writes and uses storage; add only what queries need.
- Always confirm with EXPLAIN.
FAQ
Does the primary key have an index?
Yes. Every major database automatically indexes the primary key, and in InnoDB the table itself is organised by it.
How many indexes is too many?
There is no fixed number. Watch write latency and use index usage statistics to remove indexes nobody uses. OLTP tables commonly have 3-8.
Do indexes help with small tables?
Barely. If a table fits in a few pages, scanning it is as fast as using an index, and the planner will often ignore the index.