In SQLite's EXPLAIN QUERY PLAN, what do USING COVERING INDEX and USE TEMP B-TREE FOR ORDER BY mean, and how do you avoid the sort?
answer
- sorter versus index order
- equality columns first, then sort columns
- the index alone answers it
- rowid rides in every index
basics
~20 sUSING COVERING INDEX means SQLite read only the index, never the table rows. USE TEMP B-TREE FOR ORDER BY means it sorted the result in a temporary B-tree. An index on the equality-filter columns followed by the ORDER BY columns removes that sort.
solid answer
~40 sFor `SELECT surname, given_name, phone FROM contacts WHERE city = ? ORDER BY surname` with an index on `city` alone, SQLite prints `SEARCH contacts USING INDEX contacts_city (city=?)` plus `USE TEMP B-TREE FOR ORDER BY`: it finds the city's rows, then sorts them. An index on `(city, surname)` stores each city's entries already in surname order, so the sort line disappears. Adding `given_name` and `phone` to that index turns the line into `USING COVERING INDEX`, meaning SQLite never visits the table. The rowid is the right-most part of every index entry, so an `INTEGER PRIMARY KEY` column is covered for free. `USE TEMP B-TREE FOR LAST TERM OF ORDER BY` means the index gave part of the order and SQLite sorts only within groups. Every extra index and column costs writes and file size.
code
console · 9 linessqlite> CREATE INDEX contacts_city ON contacts(city);
sqlite> EXPLAIN QUERY PLAN SELECT surname, given_name, phone FROM contacts WHERE city = 'Brno' ORDER BY surname;
QUERY PLAN
|--SEARCH contacts USING INDEX contacts_city (city=?)
`--USE TEMP B-TREE FOR ORDER BY
sqlite> CREATE INDEX contacts_city_surname_cov ON contacts(city, surname, given_name, phone);
sqlite> EXPLAIN QUERY PLAN SELECT surname, given_name, phone FROM contacts WHERE city = 'Brno' ORDER BY surname;
QUERY PLAN
`--SEARCH contacts USING COVERING INDEX contacts_city_surname_cov (city=?)go deeper
Recognise the two phrases: a temp B-tree line means a sort step, and COVERING INDEX means the table rows were never read.
Explain why an index on the equality columns followed by the sort columns removes the sort, why the rowid makes the primary key free to cover, and what LAST TERM OF ORDER BY means.
Choose index shapes from real screens: confirm with plans which query each index serves, avoid near-duplicate indexes, and weigh write and file-size cost on bulk syncs.
Set the team's rule for when a hot query earns a wider covering index, and how unused indexes get found and dropped before they slow every write.
## Two lines that describe extra work When SQLite plans a query it reports, per table, how it reads rows. Two parts of that report point at avoidable work: - **`USE TEMP B-TREE FOR ORDER BY`** is a separate line. SQLite collects the qualifying rows into a **temporary B-tree** (a throwaway sorted structure) and reads them back in order. The same line appears with `GROUP BY` or `DISTINCT` when those clauses need sorting. - **`USING COVERING INDEX`** sits on the `SCAN` or `SEARCH` line. A **covering index** contains every column the query touches, so SQLite answers from the index B-tree and skips the second lookup into the table for each row. Plain `USING INDEX` means each index entry still leads to a table-row fetch by rowid. What a covering index is in general, and when its write cost pays off, belongs to the general material on covering indexes. This answer is about how SQLite reports it and how its own index layout behaves. ## The contacts screen, step by step A contacts app lists one city's people sorted by surname: ```sql SELECT surname, given_name, phone FROM contacts WHERE city = ? ORDER BY surname; ``` | Index on contacts | Plan SQLite 3.53 prints | |---|---| | `(city)` | `SEARCH contacts USING INDEX contacts_city (city=?)` then `USE TEMP B-TREE FOR ORDER BY` | | `(city, surname)` | `SEARCH contacts USING INDEX contacts_city_surname (city=?)` | | `(city, surname, given_name, phone)` | `SEARCH contacts USING COVERING INDEX contacts_city_surname_cov (city=?)` | | `(surname, city)` | `SCAN contacts USING INDEX contacts_surname_city` | The second row is the key idea. In an index on `(city, surname)` all entries for one city sit next to each other and are already ordered by surname, so SQLite can read that range top to bottom and emit rows in the requested order. For `ORDER BY surname DESC` it walks the same range bottom to top; no descending index is needed. The last row is a trap: putting the sort column first avoids the sort but turns the city filter into a full pass over every row in index order, because the city values are scattered through it. ## What SQLite stores in every index - Every entry in an ordinary SQLite index ends with the table's **rowid**. A column declared `INTEGER PRIMARY KEY` is an alias for the rowid, so `SELECT id, surname FROM contacts WHERE city = ?` is covered by an index on `(city, surname)` with nothing added. - SQLite's `CREATE INDEX` has no `INCLUDE` clause for non-key payload columns, as some other engines do. To cover more columns you append them to the index key. - In a `WITHOUT ROWID` table the primary key takes the rowid's place at the end of every index entry, so the primary-key columns are the ones covered for free. - Expression indexes can cover too: with an index on `lower(surname)`, `SELECT id FROM contacts WHERE lower(surname) = ?` reports `USING COVERING INDEX`, because the expression's value and the rowid are all it needs. ## Partial sorts and the other temp B-tree lines - **`USE TEMP B-TREE FOR LAST TERM OF ORDER BY`** (or `FOR LAST 2 TERMS OF ORDER BY`) means the index already delivers rows ordered by the leading ORDER BY terms. SQLite then runs many small sorts, one per group of equal leading values, instead of one big sort. That uses less memory, and rows can stream out before the scan ends, which helps with a `LIMIT`. Extending the index with the remaining sort terms removes the line. - **`USE TEMP B-TREE FOR GROUP BY`** and **`FOR DISTINCT`** appear when no index provides the grouping or distinct order; an index whose leading columns match the GROUP BY list removes them. - In compound queries, `UNION USING TEMP B-TREE` means SQLite implements the UNION, which drops duplicate rows, with a temporary B-tree. ## Choosing what to change 1. Put the columns compared with `=` first, in any order among themselves. 2. Follow them with the ORDER BY columns, in the ORDER BY's order. 3. Append the selected columns only when the query is hot enough to justify a wider index. 4. Re-run `EXPLAIN QUERY PLAN` and confirm the temp B-tree line is gone and the `SEARCH` term list is what you expect. Each index is another B-tree SQLite must update on every insert and delete, and on every update that changes its columns, and it adds pages to the database file. On a phone that means slower syncs of bulk changes and a larger file, so a covering index is worth it for the screen users open constantly, not for every query.
- In SQLite, what does `USE TEMP B-TREE FOR LAST TERM OF ORDER BY` tell you about the index?The index already returns rows ordered by every ORDER BY term except the last, so SQLite sorts only within each group of equal leading values. Those small sorts need less temporary storage and let rows stream out early, which helps a LIMIT. Adding the last ORDER BY column to the end of the index removes the line.
- In SQLite, does selecting the `id INTEGER PRIMARY KEY` column stop an index on (city, surname) from covering the query?No. Every entry in an ordinary SQLite index ends with the rowid, and an INTEGER PRIMARY KEY column is an alias for the rowid. `SELECT id, surname FROM contacts WHERE city = ?` therefore reports `USING COVERING INDEX` on that two-column index.
- In SQLite, does `WHERE city = ? ORDER BY surname DESC` need a separately declared descending index?No. With an index on (city, surname), SQLite finds the city's range and reads it from bottom to top, so the rows come out in descending surname order and no temporary B-tree appears. A DESC index column earns its place when one ORDER BY mixes directions, such as `ORDER BY surname, given_name DESC`.
- USE TEMP B-TREE means SQLite saved a new permanent index to the database file.
- SQLite covers extra columns through an INCLUDE clause on CREATE INDEX.
- Selecting the INTEGER PRIMARY KEY column always forces a table lookup.
- Any index holding both columns, in any order, avoids the sort and the scan.
- A descending ORDER BY always needs a separate descending index.