Skip to content
Quantoros

What is a covering index?

SQLLevel: IntermediateDatabase Design

What is a covering index?

Hints
  • How is lookup performed without the structure and what changes with it?
  • Check selectivity, key order and the cost of data updates.

Answer

Покрывающий индекс содержит все данные, необходимые определённому запросу, включая возвращаемые столбцы. В PostgreSQL дополнительные поля можно добавить через INCLUDE:

CREATE INDEX orders_customer_cover
ON orders(customer_id) INCLUDE (status, total);

Это делает index-only scan возможным, но не гарантирует отсутствие обращений к таблице. PostgreSQL также проверяет видимость строк: если страница не отмечена all-visible в visibility map, потребуется heap fetch. Частота обновлений и VACUUM поэтому влияют на практический выигрыш.

Source and licence: SQL Interview Questions — Arda Kocadoru · MIT · answer: Quantoros editorial

Related questions