Indexes (B-tree) in SQL
Sorted side-structures that turn scans into lookups
An index is a sorted auxiliary structure — usually a B-tree — pointing into the table. Without one, WHERE salary > 80000 must read every row (a sequential scan). With one, the engine descends the tree in a few steps straight to the first matching row. Indexes make reads fast and writes slightly slower, because every INSERT/UPDATE must maintain them: that trade is the central decision of database performance.
Build the side structure
CREATE INDEX idx_emp_salary ON employees(salary) — a B-tree keyed by salary, each leaf pointing at the matching table rows.
Search becomes descent
WHERE salary > 80000 walks root → branch → leaf — a handful of page reads instead of touching every row.
Ranges ride the leaf chain
Leaves are linked in key order: find the first match, then follow the chain — perfect for BETWEEN and ORDER BY.
Fetch or cover
The engine pulls each matching row from the table — unless the index contains every needed column, in which case it answers from the index alone.
Writes maintain it all
Every INSERT/UPDATE updates every index on the table — the read speed is paid for on every write, forever.
1CREATE INDEX idx_emp_salary ON employees(salary);2EXPLAIN SELECT name FROM employees WHERE salary > 80000;
| plan |
|---|
| QUERY PLAN |
| Index Scan using idx_emp_salary on employees |
| Index Cond: (salary > 80000) |
Before the index this query was a sequential scan reading all six rows; after it, the plan is an index scan that reads only the two rows above 80,000. On six rows the win is tiny — on sixty million it is the difference between seconds and milliseconds.
Scan vs index — the same query
The plan is the proof.
1CREATE INDEX idx_emp_salary ON employees(salary);2EXPLAIN SELECT name FROM employees WHERE salary > 80000;
| plan |
|---|
| QUERY PLAN |
| Index Scan using idx_emp_salary on employees |
| Index Cond: (salary > 80000) |
When the index is ignored
Selectivity decides — and functions on the column.
1EXPLAIN SELECT * FROM employees WHERE UPPER(name) = 'ALICE';2-- Seq Scan: the UPPER(name) expression hides the column from the index3CREATE INDEX idx_emp_name_upper ON employees(UPPER(name));4-- now: Bitmap Index Scan on idx_emp_name_upper
| plan |
|---|
| Seq Scan on employees (cost=0.00..12.00 rows=1) |
| Filter: (UPPER(name) = 'ALICE'::text) |
| -- after functional index: |
| Bitmap Index Scan on idx_emp_name_upper |
Composite index and the leading edge
Column order in the index decides which queries it serves.
1CREATE INDEX idx_dept_sal ON employees(department, salary);2-- serves: WHERE department = ? · WHERE department = ? AND salary > ?3-- ignored for: WHERE salary > ? (salary is not the leading column)
| query shape | plan |
|---|---|
| department + salary filter | Index Scan on idx_dept_sal |
| salary-only filter | Seq Scan — index not usable |
The write-side bill
Every index taxes every write.
1-- one INSERT with 5 indexes:2-- 1 table row write + 5 index maintenance writes3INSERT INTO employees (id, name, department, salary, age)4VALUES (8, 'Hana', 'Sales', 54000, 28);5-- bulk loads often DROP the indexes, load, then REBUILD
| result |
|---|
| 1 row inserted · 6 total writes (row + 5 index entries) |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.