Índices de Base de Datos
B-tree · LSM-tree
Escrito por Darjus Kazakevic · revisado el
Build each index for one real query, and pay for it on every write.
Without an index, a query like where email = 'a@b.com' reads every row in the table. On 50 million rows that takes seconds. An index keeps the email values sorted, each with a pointer to its row, so the database finds the match in three or four page reads.
Indexes are not free. Every insert, update and delete must also change each index on the table, and each index uses disk and memory. So build indexes for the queries you actually run, check the plan with EXPLAIN, and drop the ones nothing uses.
En esencia — An index is a sorted copy of some columns that finds rows in a few page reads instead of a full scan. Shape it to a query, then check it with EXPLAIN.
Cuándo recurrir a él
Recurre a él cuando
- A frequent query filters or sorts on columns with no index
- EXPLAIN shows a sequential scan over a large table
- A foreign key column is used in joins or cascading deletes
Reconsidéralo cuando
- Small tables of a few thousand rows, where a scan is already fast
- Columns with few distinct values, like a boolean flag
- Write-heavy tables where no important read needs the index
Conceptos clave
- B-tree (The default index)
- A balanced tree of pages kept in sorted order, used by default in Postgres, MySQL and most relational databases. Three or four levels cover hundreds of millions of rows, so a lookup reads only a few pages.
- LSM-tree (Log-structured merge tree)
- Writes go to memory first, then flush to sorted files on disk that are merged in the background. Used by Cassandra, RocksDB and ScyllaDB. Writes are fast and sequential; a read may check several files.
- Composite index (Multi-column index)
- One index over several columns, sorted by the first, then by the second. It helps queries that filter on the leading columns. Put the columns you compare with = first, and the range or sort column last.
- Covering index (Index-only scan)
- An index that holds every column a query needs, so the database never reads the table itself. In Postgres you add the extra columns with INCLUDE.
- Selectivity (What the planner checks)
- The share of rows a filter keeps. The planner uses an index only when that share is small. If one status value covers 30% of rows, a full scan is often faster, so the index is ignored.
Reglas de diseño
- Index the queries you run, not the columns
- Start from the slow query log. Build the index that matches one query's filter and sort order. Then confirm with EXPLAIN ANALYZE that the plan uses it and the time really drops.
- Equality columns first, range column last
- In a composite index, columns compared with = go first and the range or order by column goes last. With the range column first, the index cannot narrow the next column and reads many more rows.
- Count the write cost
- A table with eight indexes updates nine structures on every insert. Indexes also take disk and memory. In Postgres, drop the ones that pg_stat_user_indexes shows are never scanned.
- Build indexes without blocking writes
- On a live Postgres table, use CREATE INDEX CONCURRENTLY. A plain CREATE INDEX blocks writes for the whole build, which can take many minutes on a large table.
Errores habituales
- An index on (created_at, customer_id) for a query that filters on customer_id: the plan scans far more of the index than needed.
- Wrapping the column in a function, like where lower(email) = ..., so the plain index on email is ignored. Index the expression instead.
- Adding an index on every column "just in case", then wondering why inserts got slow.
- Forgetting that Postgres does not index foreign keys automatically, so deleting one parent row scans the whole child table.