Asking the database how
After this: Read the plan of a lookup by key, a range, and a three-table join.
On this page · 7 sections · 3 checks
You now know two plan lines: SCAN reads every row, and SEARCH ... USING INDEX halves a sorted copy. But some searches are fast before you create anything. Finding a student by id in the earlier tutorials never needed an index. This lesson reads the plans of four everyday queries and shows where their speed comes from.
Finding a row by its key
Ask for the plan of a search by id on the members table. No index has been created.
The plan says SEARCH members USING INTEGER PRIMARY KEY (rowid=?). SQLite stores the rows of a table in order of each row's number, which it calls the rowid. When a table has a column id INTEGER PRIMARY KEY, id is the rowid. So the table itself is sorted by id, and it can be halved like an index. A search by this key is fast without any CREATE INDEX.
A key of two columns
The key of enrolments is two columns: PRIMARY KEY (student_id, course_id). A key must be different in every row, and to check that quickly on every insert, SQLite builds an index for the key by itself. It names it sqlite_autoindex_enrolments_1: the first index SQLite made for enrolments on its own.
The plan is SEARCH enrolments USING INDEX sqlite_autoindex_enrolments_1 (student_id=?). This index is sorted by student_id first, and by course_id within each student, so all of Ana's rows sit together in it. Here they are:
Lesson 4 shows what an index of two columns can and cannot do.
A range
An index helps with more than an equals test. Because the marks are sorted, every mark of 85 or more sits together at one end of the index. SQLite halves to find the first such mark, then reads the entries after it until the end: 85, 90, 91.
The plan is SEARCH enrolments USING INDEX idx_enrolments_mark (mark>?). SQLite writes mark>? for the start of the range, even though the test is >=. There is only one line: the index hands over the rows already in mark order, so the ORDER BY needs no sorting of its own. Remove the EXPLAIN QUERY PLAN line to see the three rows.
The plan of a join
Here is the three-table join from the Joining tables tutorial, without its ORDER BY. It puts each enrolment next to its student and its course.
This plan has three lines. Read them from the top, as loops inside each other:
SCAN e: read every enrolment, one by one.eis the short name the query gaveenrolments.SEARCH s USING INTEGER PRIMARY KEY (rowid=?): for each enrolment, go straight to its student, by the student's key.SEARCH c USING INTEGER PRIMARY KEY (rowid=?): and go straight to its course, by the course's key.
So the join reads the 10 enrolments once, and makes one halving lookup in students and one in courses for each of them. This is why joins on keys are fast: each partner row is found by halving, not by reading its whole table again.
How to read any plan
- Each line here is one table, in the order SQLite works through them. (Some plans add other lines, such as one for a sort.)
SCANmeans every row is read. On a big table, that is the line to look at first.SEARCHmeans SQLite goes straight to the rows it needs. The words afterUSINGsay how: by the key, or by a named index.- The brackets say which columns are looked up, and how:
=?for an equals test,>?for the start of a range.
Check yourself
Nobody has run CREATE INDEX on the members table. What does the detail column of this plan say?
EXPLAIN QUERY PLANSELECT * FROM membersWHERE id = 4242;Nobody wrote CREATE INDEX on enrolments, yet a plan says USING INDEX sqlite_autoindex_enrolments_1. Where did that index come from?
The plan of the three-table join has three lines: SCAN e, SEARCH s USING INTEGER PRIMARY KEY (rowid=?) and SEARCH c USING INTEGER PRIMARY KEY (rowid=?). What does SQLite do?
Honesty notes
- Say why a search by an INTEGER PRIMARY KEY needs no index: the table is stored sorted by that key, the rowid.
- Say where sqlite_autoindex_enrolments_1 comes from: SQLite indexes a key of two columns by itself.
- Read a range in a plan: (mark>?) is the start of the range, and the index hands over the rows already in order.
- Read a join's plan from the top: one SCAN of enrolments, then a SEARCH by key in students and courses for each row.