Use it to design indexes for queries.
B-tree Indexes
A B-tree index keeps keys sorted in a balanced tree so lookups, range scans and ordered reads are fast.
1. Query predicates and sort.
2. Existing indexes: \di / SHOW INDEX FROM t.
3. Create: CREATE INDEX … ON t (a, b); (CONCURRENTLY in PG).
4. EXPLAIN uses it?
5. Remove unused indexes.
key → tree descent → leaf → row pointers
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Compare execution time and buffers read before and after the index. Confirm the index is used for the real query parameters, not only test values.
Step 2 Common failures Narrow the fault
Indexing low-selectivity columns (for example a boolean) rarely helps. Wrong column order in a composite index makes it useless for the query. Functions on the column in WHERE bypass the index.
Composite index (a,b) helps filters on a or a+b, not b alone. Functions on columns prevent index use unless an expression index exists. Low-selectivity columns rarely benefit.
\di— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
The index stores sorted keys with pointers to rows. The planner uses it when it expects few rows to match; for many matches a sequential scan is cheaper. Covering indexes (INCLUDE columns) can answer queries without visiting the table.
ReferenceImportant terms & boundaries3 sections · depth on demand
Indexing tips
- Index columns used in WHERE, JOIN and ORDER BY on large tables.
- Composite index order matters:
(country, created_at)servesWHERE country=? ORDER BY created_at, notWHERE created_at > ?alone. - Covering indexes (
INCLUDEin PostgreSQL/SQL Server) avoid table lookups. - Every index slows writes and uses space — remove unused ones (
pg_stat_user_indexes.idx_scan = 0). - Low-selectivity columns (booleans) rarely benefit alone.
In depth
Composite index column order
An index on (a, b, c) is sorted by a, then b, then c. It can serve:
| Query | Uses the index? |
|---|---|
WHERE a = ? | Yes |
WHERE a = ? AND b = ? | Yes |
WHERE a = ? ORDER BY b | Yes, including the sort |
WHERE b = ? | Not efficiently (leading column missing) |
WHERE a > ? AND b = ? | Range on a stops b from narrowing the scan |
Rule of thumb: equality columns first, then the range or sort column.
Covering (index-only) scans
If every column the query needs is in the index, the table need not be read:
CREATE INDEX orders_cust_created ON orders (customer_id, created_at) INCLUDE (total);
SELECT created_at, total FROM orders WHERE customer_id = 42 ORDER BY created_at DESC LIMIT 10;
-- Index Only Scan … Heap Fetches: 0 (PostgreSQL; requires a recently vacuumed table)
In MySQL InnoDB every secondary index already contains the primary key, so (customer_id, created_at, total) covers the same query.
Cost of indexes
Every index is updated on every insert and on updates of its columns, takes space and competes for cache. Find unused ones before adding more:
SELECT relname, indexrelname, idx_scan, pg_size_pretty(pg_relation_size(indexrelid))
FROM pg_stat_user_indexes WHERE idx_scan = 0 ORDER BY pg_relation_size(indexrelid) DESC;
Check the counter has been accumulating long enough (statistics reset, replicas use their own counters) before dropping an index.
Common mistakes
- Indexing every column.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.