When an index cannot help, and what it costs
After this: Spot the WHERE tests an index cannot help, use a two-column index, and count the pages an index adds.
On this page · 8 sections · 3 checks
With an index on email, the search for user4242@mail.test became a SEARCH. Does every query that mentions email now get faster? No. An index is a sorted copy of the column's values, and it helps only when SQLite can use that order. This lesson shows five tests where it cannot, and then what every index costs.
An ending
A dictionary is sorted from the first letter, so you can find every word that starts with "sun". You cannot find every word that ends in "ing" without reading the whole book: those words are spread from A to Z. An index is the same.
The % in LIKE stands for any text, so this asks for emails that end in 4242@mail.test. The plan is SCAN members, even with the index.
A function of the column
lower(email) gives the email in small letters. People type their email with capitals sometimes, so a login search might compare lower(email).
The plan is SCAN members. The index holds the values of email, sorted. It does not hold lower(email), and to compare those SQLite would have to work out lower(email) for every row. The same happens with almost any function wrapped around the column.
Arithmetic on the column
The same idea, with the marks index:
mark + 5 > 90 gives SCAN enrolments. The index holds mark, not mark + 5. But this test is the same as mark > 85, and written that way, with mark alone on one side, it can use the index:
-- the index cannot help: mark is inside a sumWHERE mark + 5 > 90
-- the same test, with mark alone on one sideWHERE mark > 85The plan is SEARCH enrolments USING INDEX idx_enrolments_mark (mark>?), and the rows are the same two: Ben's Art mark (90) and Femi's Maths mark (91).
OR with a column that has no index
On the school's students, create an index on city and ask for students in Leeds or in year 1:
SCAN students. The city index finds the Leeds students, but the year 1 students could be anywhere, and nothing is sorted by year. SQLite has to read every row to find them, and once it reads every row anyway, the index adds nothing.
Two columns in one index
An index can hold two columns. Sorted by city first, and by joined within each city, it is like a phone book sorted by surname, then first name.
SEARCH members USING INDEX idx_members_city_joined (city=? AND joined=?). All the Leeds members of 2010 sit together in the index. This block counts them:
870 of the 4,348 members who joined in 2010 are in Leeds. A test on city alone still searches: SEARCH members USING INDEX idx_members_city_joined (city=?). But a test on joined alone cannot. The 2010 members sit in five separate places, one inside each city, as everyone called Sam is spread through a phone book.
SCAN members. A two-column index helps a test on its first column, or on both. On its second column alone it usually cannot help; the honesty note below gives the one exception.
What an index costs
SQLite stores a database in pieces of 4,096 bytes called . PRAGMA page_count is an SQLite command that reports how many pages the database uses.
- Without any index, the members database uses 890 pages. (Delete the first line and run it to check.)
- With the email index it uses 1,568: 678 more pages, about 2.8 MB.
- Add the index on city and joined as well, and it uses 1,959.
Space is the smaller cost. Every INSERT and DELETE must also change every index on the table, and every UPDATE must change each index that holds a column it changes, so that the sorted copies stay sorted and correct. A new member means one new row and one new entry in each index. So index the columns your searches use, not every column.
Check yourself
The members table has an index on email, and nothing else. Which of these four WHERE tests lets SQLite search the index instead of scanning?
WHERE email = 'user7@mail.test'WHERE email LIKE '%@mail.test'WHERE lower(email) = 'user7@mail.test'WHERE substr(email, 1, 8) = 'user4242'The only index on members is idx_members_city_joined ON members(city, joined). What is the plan for WHERE joined = 2010?
A colleague suggests creating an index on every column of every table, so that every search is fast. What is the problem?
Honesty notes
- Spot the tests an index cannot help: an ending with LIKE '%...', a function such as lower(email), arithmetic such as mark + 5, and OR with a column that has no index.
- Rewrite arithmetic so the column stands alone, as mark > 85 instead of mark + 5 > 90, and the index can be used.
- Say what a two-column index helps: a test on its first column, or on both, and usually not a test on the second column alone.
- Say what an index costs: pages of space (890 to 1,568 for the email index) and extra work on every INSERT and DELETE, and on each UPDATE of an indexed column.