Indexing
Definition
Index
- In plain English
- An index is an extra sorted structure that lets a database find rows without reading the whole table.
- Think of it like
- It is like the index at the back of a textbook that points straight to page 214.
Full table scan
- In plain English
- A full table scan is when the database reads every row to answer a query.
- Think of it like
- It is like flipping every page of a book to find one word.
Why it exists
Your receptionist searches two million pet records by owner phone. Reading every row makes each lookup slow.
The database needs a shorter path to matching rows. An extra lookup structure can provide that path.
How it works
B-tree
- In plain English
- A B-tree is the sorted, branching structure most databases use for indexes, so a lookup takes a few steps even across millions of rows.
- Think of it like
- Like a library directory: floor, then aisle, then shelf.
Composite index
- In plain English
- A composite index sorts by several columns in a fixed order, such as tournament then score.
- Think of it like
- Like a phone book sorted by surname, then first name.
A phone-range root branches to two ranges; one range leads to matching entries and then the pet rows.
CREATE INDEX idx_owner_phone ON pets (owner_phone);
SELECT id, name
FROM pets
WHERE owner_phone = '5550100';| Work | Without this index | With this index |
|---|---|---|
| Selective lookup | Read up to 2,000,000 rows | Traverse branches and fetch matches |
| Illustrative time | About 1.5 seconds | A few milliseconds |
| Write cost | No extra structure here | Maintain index entries too |
- This lesson describes sorted B-tree indexes; other index types also exist.
- Composite indexes are usually most efficient when filters include their leading columns.
- Measure on your data: row count, stored layout, and memory affect speed.
Examples
Search by owner phone
You look up pets by owner phone among two million records.
SELECT id, name FROM pets WHERE owner_phone = '5550100';A few tree levels can narrow the search, but fetching rows takes more work. Treat timing figures as illustrations, then measure.
The insight
Index the columns your useful queries search by.
Find tournament 7’s top ten
Your scoreboard needs the ten highest scores in tournament 7.
CREATE INDEX idx_tournament_score
ON scores (tournament_id, score DESC);
SELECT player_id, score
FROM scores
WHERE tournament_id = 7
ORDER BY score DESC
FETCH FIRST 10 ROWS ONLY;Putting score first mixes tournaments together. That index can still help some plans, but may inspect many unrelated scores.
The insight
Column order in a composite index matters.
Remove unused index work
Your team indexes all fourteen pickup-event columns. Peak evening inserts slow down.
- Measure use
Find which indexes real queries use, including infrequent important queries.
- Keep protections
Preserve indexes required for keys and declared uniqueness rules.
- Remove carefully
After review, keep three useful indexes and compare write times.
Removing unused indexes reduces storage and write work. Check that important searches still meet their response-time goals.
The insight
Keep indexes whose read benefits justify their write and storage costs.
Trade-offs
Pros
- Selective reads can avoid full scans.
- Suitable indexes can avoid extra sorting.
Cons
- Uses additional storage.
- Writes maintain affected index entries.
- Unused indexes still cost resources.
Use when
- Large-table queries filter specific rows.
- Frequent joins and sorting need faster access.
Avoid when
- Tables are tiny.
- Rarely read columns add no useful search path.
Quick check
Takeaways
- Indexes trade storage and write work for read speed.
- Index useful search, join, and sort paths.
- Column order matters in composite indexes.