CREATE INDEX idx_users_email ON users (email);A classic lookup index for logins, unique checks, or profile fetches.
Practical SQL indexing patterns for equality, range filters, ordering, include columns, and covering scans.
Common B-tree patterns that support app queries.
CREATE INDEX idx_users_email ON users (email);A classic lookup index for logins, unique checks, or profile fetches.
CREATE INDEX idx_orders_status_created_at ON orders (status, created_at DESC);Useful for queries that filter by `status` and then sort or range on `created_at`.
Store extra columns for index-only scans.
CREATE INDEX idx_orders_status_created_at_inc ON orders (status, created_at DESC) INCLUDE (total_amount, user_id);`INCLUDE` can help PostgreSQL serve some queries directly from the index when conditions are right.
-- Often effective
INDEX (tenant_id, status, created_at)
-- Query
WHERE tenant_id = ? AND status = ? AND created_at >= ?A strong general pattern is equality predicates first, then range or ordering columns.
Use indexes to avoid extra sorting or table lookups.
SELECT id, created_at
FROM orders
WHERE status = 'paid'
ORDER BY created_at DESC
LIMIT 50;An index on `(status, created_at DESC)` may let the planner filter and return rows in the needed order.
SELECT status, created_at, total_amount
FROM orders
WHERE status = 'paid'
ORDER BY created_at DESC
LIMIT 50;Covering or index-only strategies are strongest when the query reads only columns present in the index.