SQL B-tree, Composite, and Covering Indexes/Support ORDER BY with an index

Avoid expensive sorts when possible.

Section: Covering and ordering

Support ORDER BY with an index

sql
sql
SELECT id, created_at
FROM orders
WHERE status = 'paid'
ORDER BY created_at DESC
LIMIT 50;
Explanation

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

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Covering and ordering
Example covering query
Query only indexed columns when possible.
OpenIn sheetsqlsame section
Create a single-column index
Index a common lookup column.
OpenIn sheetsql1 tag match
Create a composite index
Index fields that are commonly used together.
OpenIn sheetsql1 tag match
PostgreSQL covering index with INCLUDE
Store extra columns for index-only scans.
Design for equality then range
Put equality columns first in many B-tree cases.