CodeBlanks Logo Icon CodeBlanks

Covering index

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

query.sql
-- covering index
CREATE INDEX idx_orders_cust ON orders(customer_id)

Sample solution

query.sql
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

Question
CREATE INDEX idx_orders_cust ON orders(customer_id) ____ (amount)
Answer
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

All lessons in Expert