Composite, Covering & Partial Indexes in SQL
Leftmost-prefix, INCLUDE payloads, WHERE-scoped indexes
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.
Composite leftmost-prefix
(a,b,c) serves a, (a,b), (a,b,c) — never b alone. Equality left, range/sort right.
Covering answers from index
(key) INCLUDE (payload) — Index Only Scan skips heap when visibility map is fresh.
Partial scopes rows
WHERE deleted_at IS NULL — smaller, faster, conditional-unique capable.
Expression sargifies functions
(LOWER(email)) serves functional predicates that would otherwise scan.
1CREATE INDEX idx_emp_dept_sal ON employees (department, salary);2EXPLAIN SELECT name FROM employees WHERE department = 'Sales' ORDER BY salary;
| 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.
Equality + range shape
One index serves filter and order.
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';
| plan |
|---|
| Index Scan using idx_o_cust_date |
| Index Cond: customer + date range |
Partial + expression pair
Specialized indexes for proven shapes.
1CREATE UNIQUE INDEX ux_active_email ON users (LOWER(email)) WHERE deleted_at IS NULL;
| tool | job |
|---|---|
| partial | live rows only |
| expression | case-insensitive compare |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.