3.7 An Index That Skips Table Pages: Covering Index
Finding a record through a secondary index usually takes two steps. First, the database descends the index to learn the record's address (or primary key), then uses that address to read the table page again and fetch the remaining column values (Key Lookup or RID Lookup). For one or two rows this is fine, but if thousands of rows match the condition, the Lookup that reads table pages also happens thousands of times, and most of the query cost comes from these Lookups. But what if all the values the query needs are already in the index from the start? Then there is no reason to go back to the table.
Figure 3-8. Comparing lookups with a regular index and a covering index
flowchart LR
subgraph N ["Regular index (name)"]
direction TB
N1["① Index search<br/>Root → Branch → Leaf"] --> N2["② Values obtained from the leaf<br/>name + row location"]
N2 --> N3["③ Key Lookup / RID Lookup<br/>Read the table page"]
N3 --> N4["④ Return the phone value"]
end
subgraph C ["Covering index (name) INCLUDE (phone)"]
direction TB
C1["① Index search<br/>Root → Branch → Leaf"] --> C2["② Values obtained from the leaf<br/>name + phone"]
C2 --> C3["③ Return the phone value<br/>No table page is read"]
end
Both paths process SELECT name, phone FROM customer WHERE name = '홍길동'. The regular index on the left holds only name, so the database ① descends the index, ② obtains the row location from the leaf, and ③ must read the table page at that location once more before it can ④ return the phone value. If many rows match the condition, ③ repeats as many times as there are rows.
The covering index on the right has phone in the leaf as well, so all the values the query needs are gathered at step ②. At ③ it returns the result directly, and there is no step that reads a table page at all. The difference between the two paths is a single Lookup step, and the more result rows there are, the larger that difference becomes.
If all the columns needed for a lookup are in the index, the database reads only the index leaves and not the table pages. For example, if the leaf of the name index stores both the name and the phone number, the question "What is Hong Gildong's phone number?" can be answered from the index alone. Such an index is called a covering index, and because it does not read table pages, it is faster.
Two ways to carry columns in an index
The first is to put the needed columns into the index key as well. If you build a multi-column index over name and phone number, both values go into the leaf, so it becomes a covering index. However, since the phone number also becomes a sort key, it is copied into the branch pages too, and the index grows accordingly.
The second is to use included columns. Only the name is used for search and sorting, and the phone number is stored together only in the leaf. Branches stay small with just the name, and the phone number can be taken directly from the leaf. It is created with the INCLUDE clause in SQL Server (Azure SQL) and PostgreSQL 11 and later.
-- Look up by name; store the phone number together only in the leaf
CREATE INDEX idx_customer_name ON customer (name)
INCLUDE (phone);
-- This query finishes with the index alone, without reading table pages
SELECT name, phone FROM customer WHERE name = '홍길동';
Differences between DBMSs
- SQL Server (Azure SQL): Supports
INCLUDE. When covering is achieved, the Key Lookup (RID Lookup for a heap) step that goes back to the table disappears from the execution plan. - PostgreSQL: Supports
INCLUDE, and it appears in the execution plan as an Index Only Scan. However, PostgreSQL consults the Visibility Map to check whether the row is a version that is currently visible. If VACUUM has not run for a while and the visibility map is not up to date, it ends up reading the table page again (the Heap Fetches value in the execution plan grows). - MySQL (InnoDB): There is no
INCLUDE, so columns must be put into the key. In exchange, the secondary index leaf contains the primary key, so a query that fetches only the primary key column in addition is covered without any extra effort. It is shown as Using index in the Extra column ofEXPLAIN. - Oracle: There is no
INCLUDE, so columns are put into the key. When covering is achieved, the TABLE ACCESS BY INDEX ROWID step disappears from the execution plan.
Cautions when using it
A covering index greatly reduces lookups but is not free. The more columns carried in the index, the larger the index grows, and whenever the value of a carried column changes, the index must be modified too. Also, a query that asks for all columns, such as SELECT *, can never be covered in the first place. So a covering index is best applied only to a few frequently executed key queries, carrying only the columns those queries actually use. This cost is covered in detail in Section 3.9.