SQL B-tree, Composite, and Covering Indexes

Practical SQL indexing patterns for equality, range filters, ordering, include columns, and covering scans.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Create index patterns

Common B-tree patterns that support app queries.

Create a single-column index

Index a common lookup column.

sqlANYsqlcreate-index
sql
CREATE INDEX idx_users_email ON users (email);
Notes

A classic lookup index for logins, unique checks, or profile fetches.

Create a composite index

Index fields that are commonly used together.

sqlANYcompositesql
sql
CREATE INDEX idx_orders_status_created_at ON orders (status, created_at DESC);
Notes

Useful for queries that filter by `status` and then sort or range on `created_at`.

PostgreSQL covering index with INCLUDE

Store extra columns for index-only scans.

sqlANYpostgresqlincludecovering
sql
CREATE INDEX idx_orders_status_created_at_inc ON orders (status, created_at DESC) INCLUDE (total_amount, user_id);
Notes

`INCLUDE` can help PostgreSQL serve some queries directly from the index when conditions are right.

Design for equality then range

Put equality columns first in many B-tree cases.

sqlANYbtreeordering
sql
-- Often effective
INDEX (tenant_id, status, created_at)

-- Query
WHERE tenant_id = ? AND status = ? AND created_at >= ?
Notes

A strong general pattern is equality predicates first, then range or ordering columns.

Covering and ordering

Use indexes to avoid extra sorting or table lookups.

Support ORDER BY with an index

Avoid expensive sorts when possible.

sqlANYorder-bysql
sql
SELECT id, created_at
FROM orders
WHERE status = 'paid'
ORDER BY created_at DESC
LIMIT 50;
Notes

An index on `(status, created_at DESC)` may let the planner filter and return rows in the needed order.

Example covering query

Query only indexed columns when possible.

sqlANYcoveringindex-only
sql
SELECT status, created_at, total_amount
FROM orders
WHERE status = 'paid'
ORDER BY created_at DESC
LIMIT 50;
Notes

Covering or index-only strategies are strongest when the query reads only columns present in the index.

Recommended next

No recommendations yet.