CodeBlanks Logo Icon CodeBlanks

Summary - Index tuning

Speed up a query with a covering index.

Summary - Index tuning

Index tuning: B-tree as base, covering index for index-only scan, partial index for few rows, selectivity should be high, maintenance with REINDEX. The right index speeds the right query.

-- CREATE INDEX ... INCLUDE ... WHERE ...
-- ANALYZE -> REINDEX

Index design and maintenance go together.

Practice Summary - Index tuning

Summary task: create a covering index on orders: customer_id as key, amount in INCLUDE.

Starter code

query.sql
-- summary 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 Index maintenance

Question
REINDEX INDEX ____
Answer
REINDEX INDEX idx_products_name

Explanation: idx_products_name is the right answer.

2 What does a covering index enable?

  • Full scan
  • Index-only scan Correct answer
  • Seq scan
  • Cross join

Explanation: A covering index enables index-only scan.

3 Summary - Index tuning: match the terms

Term Meaning
B-tree balanced
covering covering
partial partial
REINDEX rebuild

All lessons in Expert