Skip to content

Indexes

Introduction

An index is a "table of contents" the database builds for a column, so a lookup jumps straight to the position instead of scanning row by row.

Why use it

  • Faster queries: from “slower as data grows” to “barely affected by data size”.
  • A unique index also prevents duplicates (e.g. email, idempotency keys).
  • Sorting and pagination also rely on indexes.

How to do it

  1. Index columns that often appear in WHERE, JOIN, and ORDER BY.
  2. When several filters are used together, create a composite index. Column order matters: put equality filters first.
  3. Use the database’s EXPLAIN to confirm the query plan actually uses the index.
  4. Do not index every column.

Component comparison

Single-column index (B-Tree)

Built into the frameworkOpsLow

Build a sorted "directory" for one column (B-Tree is the most common directory structure), usable for equality, range, and sort queries.

Best forSingle columns that frequently appear in WHERE, JOIN, and ORDER BY, such as email and foreign key columns.

Avoid whenThe column has very low cardinality, the table is tiny, or the table is written far more than read.

Pros

Almost every database uses it by default, so it is the most general choice

Equality, ranges, sorting, and prefix matches all benefit from it

It is simple to create, and a slow query usually starts here

Cons

Every write has to maintain the index, which slows writes and uses disk

A low-cardinality column, such as gender, gains very little from it

A function applied to the column disables it, so you need a separate expression index

Composite index (multi-column index)

Built into the frameworkOpsLow

Combine multiple columns into one index, suited to "several conditions always queried together", such as "a user's orders sorted by time descending".

Best forFixed multi-condition query patterns, such as list pages filtered and paginated "by user + time".

Avoid whenQuery conditions are all over the place with no fixed pattern; look at the slow query log first and do not pile on indexes blindly.

Pros

One index can handle both filtering and sorting at the same time

It is more efficient than a separate single-column index on every column

It often also covers queries that only use the leading columns

Cons

Column order matters, and the index is unused if the query misses the leftmost prefix

A wider index uses more space, and writes get slower as well

You need EXPLAIN before you know whether it is actually being used

Partial index / covering index

Built into the frameworkOpsLow

A partial index builds a directory only for rows meeting a condition (such as indexing only "unpaid" orders); a covering index puts the queried columns into the directory too, so the query does not need to go back to the table for the original data.

Best forLarge tables where only a small part of the data is frequently queried (such as pending orders), or a hot query needing extreme optimization.

Avoid whenYou have not used EXPLAIN to prove a regular index is insufficient, or you use MySQL (partial indexes unavailable).

Pros

A partial index is smaller, so that small slice of hot data is queried faster

A covering index lets the query return by reading only the index

Writes maintain only the rows that match the condition, so the overhead is smaller

Cons

MySQL does not support partial indexes; the other databases do

If the query condition does not match the index condition, the index will not be used

A covering index has to store extra columns, and a query change means changing the index too

Full-text index

Built into the frameworkOpsLow

An index designed specifically for "searching text by words", able to search for keywords inside article content, which a regular B-Tree cannot do (LIKE '%word%' scans the whole table).

Best forLightweight "search articles or notes" features in blogs and internal admin tools, with modest data volume.

Avoid whenSearch is a core product feature and needs Chinese segmentation, typo correction, and complex ranking; consider a professional search engine then.

Pros

Searching for keywords inside long text is much faster than LIKE

It comes with the database, so you do not install a separate search service

Results can be sorted by relevance, which is enough for light search

Cons

Chinese needs word segmentation, and the built-in result is often not good enough

Typo correction, synonyms, and highlighting are far behind a dedicated search engine

The syntax changes with the database, so changing databases means rewriting search

Checklist

  • Common filter columns and foreign keys are indexed
  • Fields that must be unique have unique indexes
  • Slow queries have been checked with EXPLAIN
  • Unused indexes are cleaned up periodically
  • Index changes go through migrations; large-table changes ship in off-peak windows