Intermediate 8 min

Database indexes

An index helps the database find matching rows without checking the entire table.

How it works

An index resembles a sorted directory. The database can locate a date range and follow references to its matching rows. But on an insertion, the structure needs to reflect the new date as well. Compare the tradeoff with the reads and writes your application actually performs.

Walk through an example

An index on a booking date lets the database jump to the relevant part of an ordered structure for a date-range search.

What to watch for

Indexes need storage and must be maintained on writes. Add them for measured query patterns and check the query plan.

Check your understanding

Which field is a natural index candidate?

See the example and explanation
Worked example
Given An orders table grows, and one lookup always searches by customer_id.
Answer customer_id

The lookup can use an index keyed by customer_id to find matching rows directly.

Open practice problem
Next lessonAPI resources and status codes