What Is an Index?
Understand data pages, heaps, full table scans and the B-Tree structure that lets an index find a row in a few hops.
Imagine the IPL database has 1,000,000 rows of player data and you ask for WHERE name = 'Hardik Pandya'. Without help, SQL has no idea where Hardik's row is, so it reads every single row until it finds him. An index is the help: just like the index at the back of a cricket book tells you "Hardik Pandya → page 3", a database index tells SQL exactly where the row lives, so it can jump straight to it in about 3 reads instead of 1,000,000.
Definition
An index is a separate, sorted data structure built on one or more columns of a table that stores the column values together with the location of each matching row, so the database can find rows without scanning the whole table.
- Separate data structure — the index is stored next to the table and takes its own disk space.
- Sorted — the values are kept in order (A→Z, 1→100), which is what makes fast searching possible.
- Location of each row — every index entry points to where the full row is stored (or, for a clustered index, is the row).
- Without scanning — the whole goal: read a few pages instead of all of them.
Note: An index speeds up reads (
SELECT,WHERE,JOIN,ORDER BY) but slows down writes (INSERT,UPDATE,DELETE), because every change to the table must also update every index on it.
Data pages — where rows really live
SQL Server does not store rows "loose". It stores them inside pages.
A data page is a fixed-size 8 KB block inside the database's data file (
.mdf). It is the smallest unit SQL Server reads from or writes to disk.
Every page has three parts:
| Part | What it holds |
|---|---|
| Header | page information — page number (e.g. 1:150 = file 1, page 150), which table it belongs to, free space |
| Rows | the actual data, e.g. 1, Virat Kohli, RCB · 2, Rohit Sharma, MI · 3, Jos Buttler, RR |
| Offset array | a small list at the end of the page saying where each row starts (e.g. 140 | 118 | 96) |
Many pages together (1:150, 1:151, 1:152 …) make up the data file. Because the page is the unit of reading, performance is really about one number: how many pages a query must read. Fewer pages read = faster query.
Heap — a table with no index
A heap is a table that has no clustered index. Its rows are stored in pages in no particular order — usually just the order they were inserted.
sql
SELECT *
FROM players
WHERE name = 'Hardik Pandya';On a heap, Hardik could be on page 1:100, 1:101 or anywhere else, so SQL must do a full table scan: read page 1, page 2, page 3 … until the end. (It can't even stop at the first match — there could be a second Hardik.) With 1,000,000 rows that is thousands of pages.
Heaps are not always bad: inserting into a heap is very fast because SQL just appends the row wherever there is space. That's why heaps are used for staging tables that are loaded once and read in full.
B-Tree — the shape of an index
A B-Tree (Balanced Tree) is a hierarchy of pages — a root page at the top, intermediate pages in the middle and leaf pages at the bottom — where every leaf is the same distance from the root, so any value is reached in the same small number of hops.
| Level | What it stores (ids 1–16 example) |
|---|---|
| Root node | 1–8 → A, 9–16 → B |
| Intermediate | A: 1–4 → Leaf 1, 5–8 → Leaf 2 · B: 9–12 → Leaf 3, 13–16 → Leaf 4 |
| Leaf | Leaf 1 = ids 1–4, Leaf 2 = ids 5–8, Leaf 3 = ids 9–12, Leaf 4 = ids 13–16 |
To find id 11: root says "9–16 → B", B says "9–12 → Leaf 3", Leaf 3 contains id 11. Three hops. Because the tree is balanced, even 1,000,000 rows only need 3–4 levels.
Index types at a glance
Indexes are classified in three different ways — one index can belong to one group from each:
| Classified by | Types | Question it answers |
|---|---|---|
| Structure | Clustered · Non-Clustered | Is the index the table itself, or a separate copy with pointers? |
| Storage | Rowstore · Columnstore | Is data stored row by row or column by column? |
| Function | Unique · Filtered | Does it enforce no duplicates? Does it cover only some rows? |
Each type gets its own topic next.
Example — see a scan become a seek (playground)
The playground runs on SQLite, whose plan command is EXPLAIN QUERY PLAN (SQL Server shows the plan graphically instead — see the Execution Plans topic). The idea is identical.
sql
EXPLAIN QUERY PLAN
SELECT *
FROM players
WHERE name = 'Virat Kohli';| detail |
|---|
| SCAN players |
Now add an index and ask again:
sql
CREATE INDEX IX_players_name ON players (name);
EXPLAIN QUERY PLAN
SELECT *
FROM players
WHERE name = 'Virat Kohli';| detail |
|---|
| SEARCH players USING INDEX IX_players_name (name=?) |
How it works: SCAN means "read every row". After the index exists, the optimizer switches to SEARCH … USING INDEX — it walks the B-Tree straight to "Virat Kohli". (The result also shows id, parent, notused columns; only detail matters.)
Common mistakes
- ❌ "Indexes make everything faster." ✅ They speed up reads on the indexed columns and slow down every write.
- ❌ Indexing a 6-row table and expecting a difference. ✅ Indexes matter on big tables; on tiny tables a scan is already instant.
- ❌ Thinking the index changes the query result. ✅ An index never changes what rows come back — only how fast.
Interview tip
Q: What is the difference between a heap and a table with a clustered index? A: A heap has no clustered index, so rows are stored in no order and every search is a full table scan (inserts are fast, though). A clustered index stores the rows physically sorted by the key inside a B-Tree, so a lookup by that key takes only a few page reads.
Key points to remember
- An index is a separate, sorted structure on one or more columns that points to where each row lives, so SQL can avoid a full table scan.
- SQL Server stores rows in 8 KB data pages; the number of pages a query reads is the real measure of its cost.
- A heap is a table with no clustered index — rows are unordered and every search is a full table scan, but inserts are fast.
- Indexes are stored as B-Trees (root → intermediate → leaf), so any row is reached in a few hops even in millions of rows.
- Indexes are classified by structure (clustered / non-clustered), storage (rowstore / columnstore) and function (unique / filtered).
- Indexes speed up reads but slow down INSERT, UPDATE and DELETE, because every index must be maintained.
- An index never changes the query result — only the speed.
Try it yourself
Without an index: SCAN
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
With an index: SEARCH (index seek)
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: index the team column
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
