CodeBlanks Logo Icon CodeBlanks

Index selectivity

Learn selectivity and cardinality.

Index selectivity

Selectivity is the ratio of rows matching a condition. High selectivity (few rows) uses the index well; low selectivity (many rows) skips the index and scans the table.

-- seçicilik = eşleşen / toplam
-- 0.01 yüksek, 0.9 düşük

High selectivity is good for indexes.

Index selectivity — details

The optimizer computes selectivity from statistics gathered by ANALYZE. Stale statistics give wrong plans; that is why ANALYZE runs after big changes.

Practice Index selectivity

Run ANALYZE to refresh statistics for the products table.

Starter code

query.sql
-- gather statistics
ANALYZE

Sample solution

query.sql
ANALYZE products;

Expected output

ANALYZE

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does high selectivity mean?

  • Many rows match
  • The whole table is scanned
  • The index is skipped
  • Few rows match Correct answer

Explanation: High selectivity means few rows match.

2 Gather statistics

Question
ANALYZE ____
Answer
ANALYZE products

Explanation: products is the right answer.

3 Index selectivity: match the terms

Term Meaning
selectivity selectivity
high high
few rows few rows
ANALYZE statistics

All lessons in Expert