veda.ng
Back to Glossary

Database Index

Database Index infographic

A database index is a data structure that accelerates query performance by maintaining sorted pointers to table rows, enabling the database to locate data without scanning every row. Without an index on a column, finding all users named 'Alice' requires examining every row in the table (a full table scan).

With a B-tree index on the name column, the database traverses a balanced tree structure directly to 'Alice' entries, typically examining only log(n) entries instead of n. The speedup on large tables is dramatic: milliseconds instead of minutes. Indexes have costs. They consume storage space, often 10-20% of the table size per index.

They slow write operations because every INSERT, UPDATE, or DELETE must update not just the table but also all relevant indexes. Over-indexing a heavily written table can significantly degrade write performance.

Index types serve different purposes: B-tree indexes handle equality and range queries; hash indexes excel at exact matches; GiST and GIN indexes support full-text search and geometric data; partial indexes cover only rows matching a condition. Composite indexes span multiple columns, supporting queries filtering on combinations.

Covering indexes include all columns a query needs, eliminating table lookups entirely.

Interactive Visualizer

Database Index Visualization

Compare how databases find data with and without indexes. A full table scan checks every row, while an index provides direct lookup.

Users Table

IDNameAge
1Alice25
2Bob30
3Charlie35
4Diana28
5Eve32
6Frank27
7Grace29
8Henry31
9Alice26
10Jack33

Name Index (B-Tree)

NameRow IDs
Alice1, 9
Bob2
Charlie3
Diana4
Eve5
Frank6
Grace7
Henry8
Jack10
2
Results Found
10
Rows Examined
1x
Speed Improvement
Currently Scanning
Match Found
No Match