Covering index
- SQL course
- Expert
- Lesson 8/45
- 5 min
Cover all columns with one index.
Covering index
A covering index holds every column a query needs. In PostgreSQL the INCLUDE clause adds extra columns to the leaf nodes.
CREATE INDEX idx_orders_cust
ON orders(customer_id)
INCLUDE (amount);A covering index enables index-only scan.
The index idx_orders_cust is created.
Covering index — details
If the query reads only from the index, the table is never touched. This is an index-only scan and it sharply cuts IO cost.
Practice Covering index
Create a customer_id index on orders and add the amount column with INCLUDE.
Starter code
-- covering index
CREATE INDEX idx_orders_cust ON orders(customer_id)Sample solution
CREATE INDEX idx_orders_cust ON orders(customer_id) INCLUDE (amount);Expected output
CREATE INDEX
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 INCLUDE clause
CREATE INDEX idx_orders_cust ON orders(customer_id) ____ (amount)
CREATE INDEX idx_orders_cust ON orders(customer_id) INCLUDE (amount)
Explanation: INCLUDE is the right answer.
2 What does a covering index enable?
- Full table scan
- Index-only scan Correct answer
- Seq scan
- A trigger
Explanation: A covering index enables index-only scan.
3 Covering index: match the terms
| Term | Meaning |
|---|---|
covering |
covering |
INCLUDE |
add |
index-only |
index-only |
leaf |
leaf |