A database index is a lookup structure that helps the database find rows without scanning the entire table. It works like the index at the back of a book. You look up a term, then go straight to the pages where it appears.

Suppose you have a million users and often look them up by email. You could create an index and run this query:

```sql
CREATE INDEX users_email_idx ON users (email);

SELECT * FROM users WHERE email = 'sam@example.com';
```

Without a suitable index, the database may need to check every row for a matching email. With a typical **B-tree index**, it starts at the root and follows branches based on alphabetical order. It reaches a leaf containing the matching email and a reference it can use to fetch the full row.

```mermaid
flowchart LR
    A["Root page"] --> B["Branch for S…"]
    B --> C["Leaf entry for sam@example.com"]
    C --> D["Matching row in users"]
```

The tree stays shallow even when the table grows because each branch can point to many pages. The leaf entries are sorted, so the same structure also helps with range queries. An index on `created_at`, for example, lets the database jump to the start of a date range and read forward until it reaches the end.

**The trade-off is storage and write work.** The index takes up space, and the database maintains it as rows are inserted, deleted, or their indexed values change. The query planner also decides whether to use it. If a query returns most of the table, reading the table directly can be cheaper than following lots of index entries to fetch rows.