Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

B-tree Indexes

Direct answer

A B-tree index keeps keys sorted in a balanced tree so lookups, range scans and ordered reads are fast.

When to use this

Use it to design indexes for queries.

First 30 seconds

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.

Operational verification

key → tree descent → leaf → row pointers

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it to design indexes for queries.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

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.

Key checks
  • \di — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -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) serves WHERE country=? ORDER BY created_at, not WHERE created_at > ? alone.
  • Covering indexes (INCLUDE in 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:

QueryUses the index?
WHERE a = ?Yes
WHERE a = ? AND b = ?Yes
WHERE a = ? ORDER BY bYes, 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • Indexing every column.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

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.

Aliases that resolve here
b-tree indexesb-tree indexcomposite index column order
Confirmation

Confirm action

Continue?