← Lessons

quiz vs the machine

Silver1120

Databases

Index Selectivity and Cardinality

Why an index on gender rarely helps but one on email does.

4 min read · intro · beat Silver to climb

Two Related Numbers

Cardinality is the number of distinct values in a column. Selectivity measures how well a filter narrows the result, often as distinct values divided by total rows. A column with high cardinality usually gives high selectivity.

Why It Matters for Indexes

An index pays off when a lookup returns a small slice of the table. A column like email has near unique values, so an index lookup returns one row. A column like a boolean flag matches half the table, so the database may decide a full scan is cheaper than jumping through the index for so many rows.

Practical Signals

  • High cardinality columns such as user id are great index candidates.
  • Low cardinality columns such as status often are not worth indexing alone.
  • Combining a low cardinality column with a high one in a composite index can still help.

Key idea

Indexes help most on high cardinality columns where a lookup returns few rows, and add little value on low selectivity columns the optimizer would rather scan.

Check yourself

Answer to earn rating on the learn ladder.

1. What does high cardinality mean?

2. Why might the optimizer ignore an index on a boolean flag?