seegongsik
Saved words
Data

Indexes: making lookups fast

Imagine finding one word in a thick book. Reading from the first page to the last takes far too long. That is why a book has an index at the back. It lists words with page numbers, so you can jump straight to that page. A database is the same. When data grows large, scanning from the start is too slow, so it builds an index, a kind of lookup map, ahead of time.

01

A book has an index at the back

Earlier we saw how a database
finds the row it wants.
But when there is a lot of data,
scanning from start to end is slow.
It is just like hunting for a word
in a thick book from the first page.
So a book has an index at the back.
A page number sits beside each word,
so you can jump straight to that page.

Find one word in a thick book
page 1
page 2
page 3
page 4
page 5word to find
page 6
index: word -> page 5
Tap both ways to compare the number of steps

Compare scanning the text from the start with jumping via the index. How many steps does each take?

Jumping with the index
found it in far fewer steps.
A database's index
is exactly this lookup.
It is a map built ahead of time
so data is found fast.
But indexes come in kinds.
Finding one exact value
and finding a range are different.

02

For an exact value, a hash index

Sometimes you want to find
just one exact value, like "Kim".
For that, a hash index is good.
A hash takes the name
and computes a slot number at once.
So without scanning row by row
it jumps to that slot in one step.
It is like knowing a locker number
and opening exactly that one.

names (exact value)
slots
0
1
2
3
4
Tap a name to find its slot via the hash

Tap a name and the hash computes a slot number, jumping to that box in one step.

With just one name
it found the spot at once.
A hash index is this fast
at finding an exact value.
But it has a weak point.
A hash scatters the values,
so finding by a range,
like "from age 20 to 30",
it does poorly. Then another index is needed.

03

For a range, a tree index

Sometimes you want to find
by a range, like "from age 20 to 30".
For that, a tree index is good.
A tree index keeps the values
sorted from smallest to largest.
So once you find the start value
you can run right along beside it,
scanning the values in the range in order.
It is like a dictionary in alphabetical order.

sorted tree index (age)
Tap the start value of the range

Tap to set the range to find. The sorted tree index finds the start value and sweeps along the range.

Because it is sorted,
the range ran on smoothly to the side.
A hash pinpoints one spot,
a tree sweeps along a line.
So the right index depends
on what you look up often.
Lots of exact values, use a hash;
lots of ranges, use a tree.
But an index is not free.

04

An index is not free

An index has downsides too.
First, it takes extra space.
Since the lookup is written separately,
it uses that much more room.
Second, when the data changes
the index must change with it.
If you add or delete a row,
the lookup has to be updated to stay correct.
So making too many blindly is a loss.

data table
Ann
Ben
Cho
index (lookup)
Ann#0
Ben#1
Cho#2
space the index uses: 3 cells
Add or delete a row and the index follows

Add or delete a row. Each time the data changes, the index updates too, and the space it uses grows.

Every time the data changed
the index moved along with it.
And it took up more and more space.
So we make an index
only for values we look up often.
We weigh the gain of speed
against the cost of space and updates
on a balance.
Chosen well, lookups get far faster.

05

Let's wrap up

Gathered on one line, it is this.
An index, like the back-of-book lookup,
is a map that lets you jump fast.
A hash index pinpoints one exact value
in a single step,
and a tree index, being sorted,
is good for sweeping a range.
But it uses extra space
and must be updated when the data changes.

1. back-of-book lookup = a map to jump fast
2. hash index for an exact value
3. sorted tree index for a range
4. not free (space + updates)
Press the button to review the key points in order

Tap the key points in order to review. (back-of-book lookup -> hash for exact -> tree for range -> not free)

Now you know how an index
makes lookups fast.
We have seen how to find data quickly,
so next we turn to handling
data safely.
What happens when many people
touch the same data at once?
Let's work through that tricky problem
together in the next lesson.

In one lineWhen there is a lot of data, scanning from the start is slow. An index is like the back-of-book lookup, a map built in advance so the database can jump straight to the right spot. There are two kinds. A hash index pinpoints one exact value in a single step. A tree index keeps values sorted, so it is good for scanning a range. But it is not free. An index takes extra space, and when the data changes the index must be updated too. Even so, used well, it makes lookups far faster.
Data
Was this helpful? Support seegongsik