1 of 23

CSE 414 Section 8

Cardinality and Cost Estimation

November 19th, 2026

2 of 23

Announcements

  • Homework 5 was due on Tuesday, Nov 17th and the late deadline is today @ 11:59 pm.
  • HW 6 out now! Setup due Nov 24th, Tuesday

3 of 23

HW 6 Setup Walkthrough

4 of 23

Cardinality Estimation

5 of 23

Cardinality Estimation

  • Used to predict the number of rows/tuples in the output of each RA operator

​

  • Cardinality estimation helps us do cost estimation; they are not the same thing!

6 of 23

Cardinality Estimation (Point Selection)

Assume a uniform distribution.

​

σcolor = “orange”

X ≅ 1 / V(color)

≅ 1/4

Selectivity factor

7 of 23

Cardinality Estimation (Point Selection)

σcolor = “orange”

X ≅ 1/4

64 total dots * 1/4 = 16 dots

Estimated cardinality

8 of 23

Cardinality Estimation (Union)

Assume conditions are disjoint.

​

σcolor = “orange” OR color = “green”

X ≅ 1 / V(color) + 1 / V(color)

≅ 1/4 + 1/4 = 1/2

Selectivity factor

9 of 23

Cardinality Estimation (Union)

σcolor = “orange” OR color = “green”

X ≅ 1/2

64 total dots * 1/2 = 32 dots

Estimated cardinality

10 of 23

Cardinality Estimation (Intersection)

Assume independence.

​

σc1 = “red” AND c2 = “yellow”

X ≅ 1 / V(c1) · 1 / V(c2)

≅ 1/2 · 1/2 = 1/4

c1

c2

11 of 23

Cardinality Estimation (Ranges)

​

​

Schema: Weather(date, pressure). Assume a uniform distribution.

σpressure < p

σpressure > p

σp < pressure < q

​

​

X ≅ (p − min) / (max - min)

X ≅ (max − p) / (max - min)

X ≅ (q − p) / (max - min)

min(pressure)

max(pressure)

p

q

12 of 23

Cardinality Estimation (Equijoin)

Assume uniform distribution and full overlap.

⋈c1 = c2

X ≅ (1 / (V(c1) · V(c2))) · min(V(c1), V(c2))

≅ 1 / max(V(c1), V(c2))

≅ 1/4

c1

c2

13 of 23

Cardinality Estimation (Equijoin)

Assume uniform distribution and full overlap.

⋈c1 = c2

X ≅ (1 / (V(c1) · V(c2))) · min(V(c1), V(c2))

≅ 1 / max(V(c1), V(c2))

≅ 1/4 (didn’t change)

c1

c2

14 of 23

Cardinality Estimation (Equijoin)

Assume uniform distribution and full overlap.

⋈c1 = c2

X ≅ (1 / (V(c1) · V(c2))) · min(V(c1), V(c2))

≅ 1 / max(V(c1), V(c2))

≅ 1/5 (decreased)

c1

c2

15 of 23

Summary

16 of 23

Cost Estimation

17 of 23

Cost Estimation: Factors

Cost estimation - estimate the number of disk I/Os for an operation

​

B(R) = # blocks for relation R

  • Block - the smallest unit of memory that can be read at once

T(R) = # tuples for relation R

V(R, a) = # of unique values of attribute a in relation R

M = # of available memory pages

18 of 23

Cost Estimation: Nested Loop Join (⋈)

Naive: B(R) + T(R)B(S)

for each tuple t1 in R do

for each tuple t2 in S do

if t1 and t2 join then output (t1,t2)

19 of 23

Cost Estimation: Nested Loop Join (⋈)

Block-at-a-time: B(R) + B(S)B(R)

​

for each block bR in R:

for each block bS in S:

for each tuple tR in bR:

for each tuple tS in bS:

if tR and tS can join:

output (tR,tS)

​

20 of 23

Cost Estimation: Nested Loop Join (⋈)

Block-nested-loop: B(R) + (B(R)/(M-1))*B(S) ≅ B(R) + (B(R)/(M)) * B(S)

​

for each group of M blocks bR in R:

for each block bS in S:

for each tuple tR in bR:

for each tuple tS in bS:

if tR and tS can join:

output (tR,tS)

21 of 23

Cost Estimation: Hash Join (⋈)

R joined with S (assume R is smaller in size)

B(R) + B(S)

Assuming B(R) < M for one pass (look at each table once) efficiency, read all of R into a hash table and join with all of S

22 of 23

Cost Estimation: Sort-Merge Join (⋈)

B(R) + B(S)

One pass (look at each table once); Both must be small (B(R) + B(S) < M)

Why would we use this over Hash Join?

  • Tables are sorted on join attributes (No need to hash)
  • Range join instead of equijoin

23 of 23

Selectivity Formulas

  • Selectivity Factor (X) → Proportion of total data needed
    • Assuming uniform distribution of data values on numeric attribute a in table R, if the condition is:
      • a = c → X ≅1 / V(R, a)
      • a < c→ X ≅ (c - min(R, a))/ (max(R, a) - min(R, a))
      • c1 < a < c2→ X ≅ (c2 - c1)/ (max(R, a) - min(R, a))
      • cond1 and cond2 → X ≅ X1 * X2