Log inStart free
LESSON 33.27 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Database indexes

Watch a database price a full scan against an index, then pick one.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 3 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 33.27 · INDEX BAY4 OF 64 ROWS MATCH · 16 PAGES
01
Stage 1 of 6 · The table
The table keeps 64 rows in 16 heap pages, in arrival order. The query matches 4 rows.
Explain it simply

Each small box is one request in a log, with its tenant's first letter and its minute. A page, the block the database reads in one go, holds 4 rows. The blue boxes are the rows the query wants.

16 pages of 4 rows. 4 rows match the query and are marked.

rows and index keysmatching rows, chosen planpages read, costrows fetched for nothingindex it cannot use
page 0page 1page 2page 3page 4page 5page 6page 7page 8page 9page 10page 11page 12page 13page 14page 15g 0g 1u 2g 3i 4a 5g 6g 7i 8g 9i 10u 11i 12g 13g 14a 15g 16g 17g 18g 19i 20g 21g 22u 23u 24g 25u 26g 27g 28u 29g 30i 31g 32g 33g 34g 35g 36g 37i 38g 39g 40u 41g 42i 43a 44i 45u 46g 47g 48i 49u 50g 51g 52a 53u 54i 55g 56g 57i 58g 59u 60i 61g 62g 63SELECT * FROM requests WHERE tenant = 'acme'table: 16 heap pages of 4 rows (tenant initial, minute)4 of 64 rows match: selectivity 6%page 0page 1page 2page 3page 4page 5page 6page 7page 8page 9page 10page 11page 12page 13page 14page 15g 0g 1u 2g 3i 4a 5g 6g 7i 8g 9i 10u 11i 12g 13g 14a 15g 16g 17g 18g 19i 20g 21g 22u 23u 24g 25u 26g 27g 28u 29g 30i 31g 32g 33g 34g 35g 36g 37i 38g 39g 40u 41g 42i 43a 44i 45u 46g 47g 48i 49u 50g 51g 52a 53u 54i 55g 56g 57i 58g 59u 60i 61g 62g 63SELECT * FROM requests WHERE tenant = 'acme'table: 16 heap pages of 4 rows (tenant initial, minute)4 of 64 rows match: selectivity 6%
Toy: 64 rows, 16 pages, 8 leaves
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
s=kN\sy{s}{out}{s} = \frac{\sy{match}{out}{k}}{\sy{rows}{data}{N}}
s=464≈6%s = \frac{4}{64} \approx 6\%

Selectivity is the share of rows the query keeps.

Common mix-ups

MYTHAn index always makes a query faster.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01The table has a (tenant) index. Does the planner use it for globex?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.