Reading every row
After this: Search a 100,000-row table, ask SQLite how it did it, and read SCAN as 'read every row'.
On this page · 7 sections · 3 checks
A club keeps its 100,000 members in one table. When someone logs in with their email, the program asks the database for that member's row. How does the database find one row among 100,000? And why can the same kind of question be slow on one table and fast on another? This tutorial answers both, and shows how to make a slow search fast.
The members table
The lessons use two tables. One is the school from the earlier tutorials. The other is new: a table of 100,000 club members.
CREATE TABLE members ( id INTEGER PRIMARY KEY, email TEXT NOT NULL, city TEXT NOT NULL, joined INTEGER NOT NULL);Each member has a number (id, from 1 to 100,000), an email, a city and the year they joined. The rows were made by a query, not typed in, so they follow a pattern. Member 4242 has the email user4242@mail.test. The five cities take turns, so each city has 20,000 members. The years run from 2000 to 2022.
Run the two queries below. The first shows the first three members. The second counts the members in each city.
Each press of Run builds all 100,000 rows again in your browser before your query runs, so it takes a moment.
Finding one member
Now find the member who logs in as user4242@mail.test.
One row comes back: member 4242, from Hull, who joined in 2010. The answer is right. The question for this tutorial is how SQLite found it.
Reading every row
The rows are stored in the order they were added, not in email order. So nothing tells SQLite where user4242@mail.test is. The only safe way to answer is this:
- Take the first row and compare its email with
user4242@mail.test. - Take the next row and compare again.
- Keep going until the last row has been compared.
SQLite cannot stop at the first match, because nothing in the table says an email appears only once. Another member further down might have the same email. So it compares all 100,000 rows. Reading every row of a table to answer a question is called a .
Here is a small version. The visualizer holds nine marks from the school's enrolments, in the order the rows are stored. (Chloe's History row has no mark yet, so it is left out.) The task looks for 80, then for 60, which nobody scored.
- Search for 80These are the nine marks in the order the rows are stored. Look for 80 the only way possible here: start at the first row and compare each one.
- Search for 60
- 072
- 165
- 280
- 358
- 490
- 585
- 647
- 776
- 891
Run an operation to begin.
The visualizer stops when it finds 80, after 3 comparisons. A database answering a WHERE test cannot stop there, for the reason above: another row might also hold 80. And for 60, even the visualizer had to compare all 9 marks before it could say "not here".
Asking SQLite how it searched
You can ask SQLite how it will run a query. Put EXPLAIN QUERY PLAN in front of the query. SQLite then does not run the query. Instead it returns its : the way it will read the tables.
The result has four columns. The first three are SQLite's own numbering; read the last one, detail. It says SCAN members: read every row of members. That is the scan you did by hand.
The work grows with the table
A scan reads every row, so its work grows with the size of the table. Twice as many members means twice as much reading. In the words of the arrays tutorial (Counting steps), a scan is O(n), which means the work grows with the number of rows, n.
On your computer, 100,000 rows are read in a blink. But a real login table can hold millions of rows, and thousands of people log in every minute. Reading the whole table for every login is the reason a query can be slow. The next lesson shows how to avoid it.
Check yourself
The members table has no index of any kind on email. How does SQLite find the rows WHERE email = 'user9@mail.test'?
The club grows from 100,000 members to 1,000,000, and still has no index on email. What happens to the work of finding one member by email?
The members table has no index on city. What does the detail column of this plan say?
EXPLAIN QUERY PLANSELECT * FROM membersWHERE city = 'Kent';Honesty notes
- Say what a scan is: reading every row of a table, one after another, to find the rows that pass a test.
- Say why a database scans when nothing is sorted by the column it searches, and why it reads to the end even after a match.
- Ask SQLite how it will run a query with EXPLAIN QUERY PLAN, and read the detail column: SCAN members.
- Say that the work of a scan grows with the size of the table: ten times the rows, ten times the reading.