Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Define selectivity and cardinality for an index. Why might the planner ignore an index on a boolean column is_active where 95 percent of rows are true, and when would it use it?

Cardinality is the number of distinct values in a column, and selectivity is the fraction of rows a predicate returns. For is_active = true, the predicate returns 95 percent of rows, so reading the index and then almost every table page is slower than one sequential scan, and the planner ignores the index. For is_active = false, only 5 percent of rows match, so an index helps, and a partial index WHERE NOT is_active is smaller and better still.

Why this question is asked

Developers often add an index, see that the planner does not use it, and conclude the planner is wrong. Usually the planner is right, and understanding why is the key to indexing well. Interviewers ask about selectivity and cardinality because they decide whether an index is worth having, and because the boolean column is the clearest example of a low-cardinality column that is useful for one value and useless for the other. In this answer, we will define the two terms, work out the cost of both plans, see when the index helps, look at the statistics the planner uses, and add physical correlation to the picture.

1A Quick Recap of Selectivity and Cardinality

Cardinality is how many distinct values a column has. A user id column in a users table has high cardinality, one value per row. A boolean column has a cardinality of two, or three if NULL is allowed. Cardinality describes the column as a whole.

Selectivity describes one predicate: the fraction of rows it returns. WHERE id = 42 returns one row out of 10 million, a selectivity of 0.0000001, which we call highly selective. WHERE is_active = true returns 9.5 million out of 10 million, a selectivity of 0.95, which is not selective at all. Notice that the same low-cardinality column can have very different selectivity for its different values. That is the whole point of the boolean example.

2Why the Planner Ignores the Index for is_active = true

The planner estimates the cost of each plan in units of page reads and CPU. A sequential scan reads every page of the table once, in order, which disks and the operating system handle efficiently. An index scan reads index pages and then, for each matching row, fetches its table page, often in random order.

SQL
-- users: 10,000,000 rows on 200,000 pages; 95% active
EXPLAIN SELECT * FROM users WHERE is_active = true;
-- Seq Scan on users (rows=9500000)
-- Filter: is_active
SET enable_seqscan = off; -- only to see the alternative, never in production
EXPLAIN ANALYZE SELECT * FROM users WHERE is_active = true;
-- Bitmap Heap Scan on users (actual time=480..5200 rows=9500000) -- slower than the seq scan
Same column, two predicates
PredicateRows (of 10 M)SelectivityBest plan
is_active = true9,500,0000.95sequential scan
is_active = false500,0000.05index or bitmap scan
NOT is_active, partial index500,0000.05scan of a small partial index
id = 4210.0000001index scan

With 95 percent of rows matching, the index scan would visit nearly every table page anyway, and on top of that it reads the index. Even a bitmap heap scan, which sorts the matches by page first, ends up reading all 200,000 pages plus the index. So the sequential scan is the cheaper plan, and the planner chooses it correctly. A rough rule is that once a predicate returns more than a few percent of rows, depending on correlation and storage, a sequential scan starts to win.

3When the Index Does Help

For is_active = false, only 500,000 rows match. If those rows are spread over the table, a bitmap scan can still touch many pages, but it reads far fewer than 200,000 and often wins clearly. If the query also has a LIMIT, or an ORDER BY that the index provides, it wins by a lot.

We can do better than a full index on is_active, though. A full index stores an entry for every row, 95 percent of them for the value we never search. A partial index contains only the rows we care about.

Java
CREATE INDEX users_inactive_idx ON users (id) WHERE NOT is_active;
EXPLAIN SELECT id FROM users WHERE NOT is_active ORDER BY id LIMIT 100;
-- Limit -> Index Only Scan using users_inactive_idx -- reads about 100 entries
-- index size: about 11 MB instead of about 214 MB for a full index

So the index column can be something useful, here id to support the sort, while the predicate does the filtering. The query must state NOT is_active, or is_active = false, so the planner can prove the partial index applies.

4How the Planner Knows the Numbers

The planner does not count rows before every query. It uses statistics that ANALYZE collects from a sample of the table, either run manually or by autovacuum. They are visible in the pg_stats view. For each column, they include the fraction of NULLs, the number of distinct values, the most common values with their frequencies, a histogram of the remaining values, and the correlation between physical row order and the column's sort order.

SQL
SELECT null_frac, n_distinct, most_common_vals, most_common_freqs, correlation
FROM pg_stats WHERE tablename = 'users' AND attname = 'is_active';
-- null_frac | n_distinct | most_common_vals | most_common_freqs | correlation
-- 0 | 2 | {t,f} | {0.95,0.05} | 0.81

From most_common_freqs, the planner reads directly that true matches 95 percent and false 5 percent, and it multiplies by the row count to estimate 9.5 million and 500,000 rows. If statistics are stale, for example after a bulk update that deactivated many users, the estimate is wrong, and running ANALYZE fixes it.

5Correlation Also Matters

Correlation measures how well the physical order of rows matches the index order, from -1 to 1. If inactive users are clustered at the start of the table, because old accounts were deactivated and were inserted first, the 500,000 matches sit on a few thousand neighbouring pages and an index scan is very cheap. If they are scattered randomly, each match may be on a different page, and the planner raises its cost estimate. This is also why range scans on a column that grows with time, such as created_at in an append-only table, are cheap: correlation is close to 1.

6Follow-up questions

What does pg_stats.most_common_vals show?

It lists the values that appear most often in a column, according to the last ANALYZE sample, and most_common_freqs gives the fraction of rows for each. The planner uses them to estimate selectivity for equality predicates on those values precisely. Values not in the list are estimated from the remaining fraction and the number of distinct values, or from the histogram for range predicates. How many values are kept depends on the statistics target, 100 by default, which can be raised per column.

Is it ever worth indexing a low-cardinality column on its own?

Rarely as a full index. A partial index for the rare value, or a composite index where the low-cardinality column is combined with a selective or sortable column, is almost always better. The column alone only helps when a value is rare and we query exactly that value.

7Conclusion

In this answer, we defined cardinality as the number of distinct values and selectivity as the fraction of rows a predicate returns. For is_active = true, 95 percent of rows match, so an index scan would read nearly every page plus the index, and the planner correctly prefers a sequential scan. For the rare value, an index helps, and a partial index WHERE NOT is_active is far smaller. The planner estimates all of this from ANALYZE statistics, such as most common values and histograms, and adjusts the cost by the correlation between physical and index order.

What to say in the interviewAbout 90 seconds aloud

Open with

Cardinality is the number of distinct values in a column, so a boolean has a cardinality of two. Selectivity is about a predicate: the fraction of rows it returns.

Then, if they want more

The point of the boolean example is that the same column has very different selectivity for its two values. For is_active equals true, 95 percent of rows match, so an index scan would read the index and then visit almost every table page, often in random order, which is more work than one sequential scan.

The planner sees that and correctly ignores the index. For is_active equals false, only 5 percent match, so an index or bitmap scan is worth it, especially with a limit or an order by.

Even better is a partial index WHERE NOT is_active, indexed on a useful column such as id, which holds only the inactive rows and is a small fraction of the size of a full index. The planner gets these numbers from ANALYZE: pg_stats has the null fraction, number of distinct values, most common values with their frequencies, and histograms.

So after a big change in the data, I would run ANALYZE. Correlation matters too: if the matching rows are physically close together, the index scan is cheaper than if they are spread over the whole table.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Define selectivity and cardinality for an index. Why might the planner ignore an index...": https://scrivio-interview-library.netlify.app/databases/indexing/5225907215/