Stop 11 · Indexes & Query PerformanceTopics · 4/9

Covering and Unique Indexes

Part of the SQL Roadmap

In short

Some indexes do more than speed things up. A unique index protects your data from duplicates, and a covering index can answer a query without touching the table at all.

A unique index guarantees no two rows share a value and also speeds up lookups. A covering index includes every column a query needs, so the database can answer from the index alone without reading the table.

The jaadu stands at the open door of an exam hall studying a seating chart pinned to the wall, while inside rows of single desks each hold exactly one student.

Think of it like this

The seating chart outside the exam hall

Outside the hall, a chart lists every roll number with its seat. Each roll number appears exactly once, because two students can't share a seat. That's a unique index. And if the chart also shows the student's name, a teacher asking 'who sits in seat 14?' gets the answer at the door without walking in. That's a covering index.

Watch it happen

Index Scan vs Index Only Scan

Normal index

  • Index on (customer_id) only
  • Finds matching entries in the index
  • Then visits the table for order_date and amount
  • One extra trip per matching row

Covering index

  • Index on (customer_id, order_date) INCLUDE (amount)
  • Finds matching entries in the index
  • Every needed column is already there
  • Plan shows Index Only Scan

Make an index covering when one important query runs very often and needs only a few columns. Don't try to cover SELECT *.

The big ideas

What you need to know

Unique means one only

A unique index rejects any INSERT or UPDATE that would create a second row with the same value.

Unique on an expression

UNIQUE on LOWER(email) stops 'Riya@x.in' and 'riya@x.in' from both being saved.

Covering means no table trip

If the index holds every column the query reads, the database answers from the index alone.

NULLs are not equal

A unique index allows many NULLs, because NULL never equals NULL. PostgreSQL 15 adds NULLS NOT DISTINCT if you need only one.

See it in code

Walk through the code

sql
1-- Unique: no two customers share an email, ignoring case2CREATE UNIQUE INDEX uq_customers_email3  ON customers (LOWER(email));4 5-- Covering: search columns plus a payload column6CREATE INDEX idx_orders_cust_date_cover7  ON orders (customer_id, order_date)8  INCLUDE (amount);9 10-- This query can be answered from the index alone11EXPLAIN12SELECT order_date, amount13FROM orders14WHERE customer_id = 452115ORDER BY order_date DESC;16-- Look for: Index Only Scan using idx_orders_cust_date_cover

1/4An expression index that is also unique. A second signup with the same email in different case fails with an error.

INCLUDE works in PostgreSQL 11+ and SQL Server. MySQL has no INCLUDE, so add amount as a last key column instead. In PostgreSQL, index-only scans work best on recently vacuumed tables.

Save yourself the pain

Common mistakes

Oops

Using a unique index only for speed and forgetting it also blocks inserts.

Do this instead

Add UNIQUE only where duplicates are truly wrong, like emails or PAN-style codes, and handle the error in the app.

Oops

Adding every column to INCLUDE so 'all queries are covered'.

Do this instead

That makes a second copy of the table. Cover one or two hot queries with just the columns they need.

Your mission

Get an Index Only Scan

~25 min

Quick check

What makes an index 'covering' for a query?

Go deeper

  1. 1Add a unique index on email and try inserting a duplicate.
  2. 2Build a covering index for a frequent query and look for an index-only scan in EXPLAIN.
  3. 3Learn PostgreSQL's INCLUDE clause for adding extra columns to an index.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge