Sub topic 5
Selectivity: When an Index Helps and When It Does Not
Assume you know basic SELECT, WHERE, ORDER BY, and JOIN, and that you understand tables, rows, primary keys, and linear versus logarithmic work.
What selectivity means
Selectivity describes how narrowly a predicate filters a table. A highly selective predicate matches few rows. A low-selectivity predicate matches many rows.
For example, id = 18421 is usually highly selective because an identifier normally identifies one row. status = 'open' may have low selectivity if most orders are open.
Cardinality is the number of distinct values in a column. A column with high cardinality, such as order_id, often supports selective lookups. A column with low cardinality, such as a Boolean is_active, often does not.
| Predicate | Typical matches | Likely index value |
|---|---|---|
| `id = 18421` | One row | High |
| `customer_id = 42` | A customer’s rows | Depends on customer size |
| `status = 'open'` | A large fraction of rows | Often low |
| `created_at` range for one day | Varies by workload | Often useful |
Why an index can be slower
An index avoids reading every table row, but it adds work. The database first walks the index, then may fetch matching rows from the table. If the predicate matches a large fraction of the table, those random table fetches can cost more than reading the table sequentially.
The planner compares estimated costs. It considers table size, estimated row counts, available indexes, page layout, and statistics. The planner may choose a sequential scan even when an index exists.
A table scan can also be reasonable for a small table. Walking an index may cost more than reading a few pages, regardless of selectivity.
Check the planner's estimate
Use EXPLAIN to inspect the chosen plan and estimated row counts:
CREATE TABLE orders (
id bigint PRIMARY KEY,
status text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE INDEX orders_status_idx ON orders (status);
EXPLAIN SELECT * FROM orders WHERE status = 'open';Look for Index Scan, Bitmap Heap Scan, or Seq Scan. The estimate after rows= is the number of rows the planner expects to match. EXPLAIN ANALYZE also executes the query and reports actual counts, so use it carefully on expensive statements.
Common errors
- Assuming every
WHEREcolumn needs an index. Low-selectivity columns may not benefit. - Ignoring the result size. A query returning half the table may be faster with a sequential scan.
- Trusting stale statistics. After major data changes, refresh statistics with
ANALYZE. - Testing only a tiny development table. Plan choices can change as the table grows and data distributions change.
- Confusing selectivity with uniqueness. A non-unique column can still be selective for a particular value.
Exercise
A table has ten million rows, and 70% have status open. An index exists on status. Should you expect an index scan for WHERE status = 'open'?
Show answer
No. The predicate has low selectivity and returns most rows, so the planner will often prefer a sequential scan. Verify the decision with EXPLAIN.