Learn SQL
-- performance.index_design

Composite, Covering & Partial Indexes in SQL

Leftmost-prefix, INCLUDE payloads, WHERE-scoped indexes

--concept

Composite (a,b,c) serves a, (a,b), (a,b,c) — never b alone (leftmost-prefix). Order equality-first, then ranges/sorts. Covering indexes hold every needed column (INCLUDE payloads) for index-only scans. Partial indexes cover matching rows only (WHERE deleted_at IS NULL) — smaller, faster, conditional-unique. Expression indexes (LOWER(email)) make functional predicates sargable.

--how it works
1

Composite leftmost-prefix

(a,b,c) serves a, (a,b), (a,b,c) — never b alone. Equality left, range/sort right.

2

Covering answers from index

(key) INCLUDE (payload) — Index Only Scan skips heap when visibility map is fresh.

3

Partial scopes rows

WHERE deleted_at IS NULL — smaller, faster, conditional-unique capable.

4

Expression sargifies functions

(LOWER(email)) serves functional predicates that would otherwise scan.

--visualization
Index Scan using idx_o_cust_dateequality customer seeks, range date scans forward
Stage 1 / 1
Leftmost-prefix: (a,b) serves a and a+b, never b alone. Equality left, range right.
--example
query.sql
1CREATE INDEX idx_emp_dept_sal ON employees (department, salary);
2EXPLAIN SELECT name FROM employees WHERE department = 'Sales' ORDER BY salary;
Result
plan
QUERY PLAN
Index Scan using idx_emp_dept_sal on employees
Index Cond: (department = 'Sales'::text)

The composite serves both the equality filter and the sort order — one index, two jobs. A covering INCLUDE would additionally avoid heap fetches; a partial variant would shrink it to active rows only.

--variants

Equality + range shape

One index serves filter and order.

query.sql
1CREATE INDEX idx_o_cust_date ON orders (customer_id, created_at);
2SELECT * FROM orders WHERE customer_id = 7 AND created_at > '2024-01-01';
Result
plan
Index Scan using idx_o_cust_date
Index Cond: customer + date range
1Equality on customer seeks the slice.
2Range on date scans forward within it.
3Reversed order would miss customer-only filters.

Partial + expression pair

Specialized indexes for proven shapes.

query.sql
1CREATE UNIQUE INDEX ux_active_email ON users (LOWER(email)) WHERE deleted_at IS NULL;
Result
tooljob
partiallive rows only
expressioncase-insensitive compare
1Partial scopes uniqueness to live rows — archived dupes allowed.
2Expression normalizes before comparing — sargable LOWER().
3Both beat triggers for clarity and concurrency.
--key facts
Leftmost-prefix(a,b,c) serves a and a+b, not b alone
Coveringanswer from index, skip heap
Partial+expressionspecialized indexes for proven shapes
--use cases
Hot access paths: WHERE/JOIN/ORDER shapes from EXPLAIN
Soft-delete uniqueness + case-insensitive login
--under the hood
Every index taxes writes (B-tree insert/split + uniqueness checks) — budget deliberately, prune unused via stats.
Bloat from MVCC dead versions slows scans; VACUUM reclaims + refreshes visibility for index-only scans.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.