A sorted copy
After this: Say what an index is, create one, and watch the plan change from SCAN to SEARCH.
On this page · 6 sections · 3 checks
In lesson 1, SQLite read all 100,000 rows to find one email, because the rows are not in email order. Think of a paper dictionary. You never read it from the first page to find a word. You open it near the middle, see whether your word comes before or after, and throw away half the book. That works only because the words are sorted.
A sorted copy of one column
A database can do the same. It keeps a second, sorted list of the values in one column, and next to each value it writes which row the value came from. That list is an .
In the lists tutorial, an index was a position in a list. In a database the word means this sorted copy. The two meanings are different, and this tutorial always means the second one.
Here is an index on the mark column of the school's enrolments. Chloe's History row has no mark yet, so it has no entry.
| mark | row it came from (student_id, course_id) |
|---|---|
| 47 | (4, 2) |
| 58 | (2, 1) |
| 65 | (1, 2) |
| 72 | (1, 1) |
| 76 | (4, 4) |
| 80 | (1, 3) |
| 85 | (3, 1) |
| 90 | (2, 4) |
| 91 | (6, 1) |
The table itself does not change. Its rows stay in the order they were added. The index is a separate list, kept beside the table.
Halving, in three sentences
If you skipped the tutorial "Sorted arrays and binary search", here is the idea. Look at the middle entry of the sorted list: if it is the value, you are done. If the value is smaller, it can only be in the left half; if it is bigger, only in the right half, so the other half is thrown away and you repeat with what is left. Each look is called a .
Find 80 in the marks index by hand:
- Nine entries. The middle one is 76. 80 is bigger, so only 80, 85, 90, 91 are left.
- The middle of those four, rounding down, is 85. 80 is smaller, so only 80 is left.
- Look at 80. Found, after 3 probes. The index says it came from row (1, 3): Ana's History mark.
The task asks you to predict the first probe, then searches for 80 and for 60.
- Binary search for 80The same nine marks, now sorted: this is what an index on mark holds. Predict first: tap the cell the first probe reads. Then watch the search halve the row.
- Binary search for 60
- 047
- 158
- 265
- 372
- 476
- 580
- 685
- 790
- 891
Run an operation to begin.
60 also took 3 probes, and then nothing was left. In lesson 1 the scan needed 9 comparisons to be sure 60 was missing.
17 probes for 100,000 rows
Now the members table. An index on email holds all 100,000 emails in sorted order. Each probe halves what is left: 100,000, then 50,000, then 25,000, and so on. After 16 halvings one entry is left, and one more probe looks at it. So any email is found, or known to be missing, in at most 17 probes. user4242@mail.test takes all 17. A scan reads 100,000 rows.
This kind of growth is written O(log n), which means the work grows by one probe each time the table doubles in size. A table of 200,000 rows needs at most 18 probes.
Creating an index
You create an index with one statement:
CREATE INDEX idx_members_emailON members(email);CREATE INDEXsays what to make.idx_members_emailis a name you choose. Starting withidx_, then the table and the column, is a common habit, and it tells you in a plan which index was used.ON members(email)names the table and the column to copy and sort.
SQLite reads the table once, builds the sorted copy, and from then on keeps it up to date by itself. Your queries do not change. Run the block below. It creates the index, then asks for the plan of the same email search as lesson 1.
The plan now says SEARCH members USING INDEX idx_members_email (email=?). Read it in parts:
SEARCH members: SQLite goes straight to the rows it needs instead of reading them all.USING INDEX idx_members_email: it finds them with the index you created.(email=?): it looks up theemailcolumn with an equals test. The?stands for the value, here'user4242@mail.test'.
Delete the EXPLAIN QUERY PLAN line and run it again: the answer is the same row as before. An index changes how the rows are found, never which rows come back.
The marks index works the same way:
SEARCH enrolments USING INDEX idx_enrolments_mark (mark=?). Without the first line, the plan for the same query is SCAN enrolments.
Check yourself
The index on mark holds 47, 58, 65, 72, 76, 80, 85, 90, 91. A search for 60 halves it until nothing is left. Which marks does it read, in order?
The members table has an index on email: 100,000 emails, sorted. At most how many probes does halving need to find one email, or to be sure it is not there?
These two statements run on the members table. What does the detail column of the plan say?
CREATE INDEX idx_members_email ON members(email);EXPLAIN QUERY PLANSELECT * FROM membersWHERE email = 'user4242@mail.test';Honesty notes
- Say what an index is: a sorted copy of a column, where each entry says which row it came from.
- Search a sorted list by halving: 3 probes for 80 or 60 among nine marks, at most 17 among 100,000 emails.
- Create an index with CREATE INDEX name ON table(column), and say that the answers of your queries do not change.
- Read SEARCH members USING INDEX idx_members_email (email=?): straight to the rows, with that index, by an equals test on email.