Index selectivity

On this page 7

Index selectivity is the measure of how effectively an index narrows a search, usually expressed as the ratio of distinct key values to total rows or as the fraction of rows a predicate returns. In databases and in indexing practice, it is the single most important property deciding whether an index is worth reading at all, because the query planner compares the estimated cost of an index lookup with the cost of a full table scan.

A highly selective index points to a small slice of the table, such as one customer by email address, while a low-selectivity index points to a large share of rows, such as all orders with status ‘shipped’. The distinction drives index design, column order in composite indexes and plan choice, and misjudging it is one of the most common causes of slow queries.

How index selectivity is calculated

Index selectivity is calculated in two related ways, and practitioners should know which one a tool reports. Column selectivity (sometimes called index cardinality ratio) divides the number of distinct values by the total number of rows, giving a value between near zero and exactly 1.0 for a unique index. Predicate selectivity is the estimated fraction of rows that a specific condition returns, so a lower number means a more selective filter.

The two views can disagree. A column with many distinct values can still produce a poorly selective predicate when the data is skewed, for example when 60% of rows share one value and the rest are spread thinly. For this reason, modern optimizers rely on histograms and most-common-value lists, not only on distinct counts, to estimate how many rows each predicate will return.

The table below shows typical selectivity values for common column types in a table of one million rows.

Column typeDistinct valuesColumn selectivityIndex usefulness
Primary key1,000,0001.0Excellent for point lookups
Email addressAbout 999,000Close to 1.0Excellent
Customer ID on ordersAbout 50,0000.05Good for equality filters
Country codeAbout 2000.0002Depends on skew
Boolean flag20.000002Usually poor unless skewed
Typical column selectivity in a one-million-row table

Why the query planner depends on selectivity

The query planner uses selectivity to estimate row counts, and row counts drive every cost decision in a plan. An index lookup through a non-clustered or secondary B-tree index requires random I/O for each matching row, because the table rows live elsewhere, while a sequential scan reads pages in order and benefits from prefetching and larger reads.

As a result, there is a break-even point beyond which an index scan becomes more expensive than reading the whole table. This threshold is not a fixed constant; it depends on storage type, row width, clustering of the data on disk and memory caching. On spinning disks the crossover was often a few percent of rows, while on SSDs with a warm buffer cache it can be considerably higher.

The following table summarizes the factors that shift the break-even point.

FactorEffect on thresholdReason
Correlation of key and physical orderRaises itMatching rows sit on fewer pages
Covering indexRaises it sharplyNo table lookups are needed
Fast random I/O on SSDRaises itRandom reads cost less
Wide rowsLowers itFewer rows fit on each page
Cold cacheLowers itEach lookup may hit storage
Factors that move the index-versus-scan threshold

Because the planner relies on estimates, stale statistics produce wrong selectivity numbers and therefore wrong plans. Running the engine’s statistics collection, such as ANALYZE or UPDATE STATISTICS, after large data changes is a basic part of index maintenance.

Selectivity in composite and specialized indexes

In a composite index, selectivity applies to the combination of leading columns, not to each column alone. Two columns that are individually weak, such as region and product category, can together form a highly selective prefix. Optimizers that assume column independence often underestimate this combined effect, which is why several engines support extended or multi-column statistics.

Column order also matters. A common rule places equality-filtered columns first and range-filtered columns last, and among equality columns it often favors the more selective one. However, the rule is secondary to matching real query patterns: a leading column that queries rarely filter on makes the index unusable for them regardless of its selectivity.

Different index types respond to selectivity in different ways, as the comparison below shows.

Index typePreferred selectivityTypical use
B-tree indexHighPoint lookups and narrow ranges
Hash indexVery highExact equality on unique-like keys
Bitmap indexLowAnalytical filters on few distinct values
Partial indexHigh within subsetRare values in a skewed column
Unique indexMaximumIntegrity and single-row access
How index types relate to selectivity

The partial index deserves special attention. When only 1% of orders have status ‘pending’, an index restricted to that status is small and extremely selective, while the remaining 99% of rows never enter the index at all.

Designing indexes with selectivity in mind

Good index design starts from the queries, then checks selectivity to confirm that an index will actually be used. Practitioners typically inspect distinct counts, value distribution and execution plans before creating an index, because an unused index still costs write overhead and storage on every insert, update and delete.

The decision table below captures common situations.

SituationRecommended choiceReason
Filter returns well under 1% of rowsStandard B-tree indexLookups are cheap and few
Filter targets one rare value in a skewed columnPartial indexSmall index with high selectivity
Two weak columns always filtered togetherComposite indexCombined prefix is selective
Filter returns a large share of rowsNo index or covering indexScan is usually cheaper
Low-cardinality columns in analyticsBitmap or columnar storageBuilt for bulk filtering
Index decisions based on selectivity

Common mistakes and misconceptions

Several errors recur in production systems, and most come from treating selectivity as a static property of a column rather than of a predicate on real data.

  • Assuming that every filtered column needs an index, which adds write cost without improving reads.
  • Indexing a boolean or status column without checking skew, so the planner ignores the index.
  • Ignoring parameter-sensitive plans, where a cached plan built for a rare value is reused for a common one.
  • Forgetting that functions on indexed columns, such as wrapping a column in LOWER(), prevent the index from being used unless an expression index exists.
  • Trusting outdated statistics after bulk loads, which leads to severe misestimates.

A related misconception holds that low selectivity always makes an index useless. In practice, a covering index or an index that supports ordering and grouping can still pay off, because it avoids sorting or table access even when many rows qualify.

Stripped to the essentials

Index selectivity tells you how much an index helps you skip. Imagine a phone book. Looking up one exact name takes you straight to a single line, so the index is very selective. Looking up everyone whose name starts with a common letter gives you thousands of entries, so you might as well read the whole section.

A database makes the same judgment. If an index leads to just a few rows, the database uses it. If it leads to a large part of the table, the database usually reads everything in order because that is faster. The fewer rows an index points to, the more useful it is.

Where it is applied

A support team at an online retailer complains that the ticket dashboard loads slowly. The tickets table holds about 8 million rows, and the dashboard filters on status = 'open'. A developer had added a regular index on the status column, but the execution plan shows a full table scan. Checking the data reveals only five status values, with ‘closed’ covering 97% of rows.

Because open tickets make up only about 2% of the table, the team replaces the general index with a partial index on open tickets, ordered by creation time. The new index is roughly one fiftieth of the original size, the planner now chooses it, and the dashboard query drops from several seconds to under 50 milliseconds. The team also schedules statistics updates after nightly imports so the estimates stay accurate.