CSE 344: Section 6
Cost Estimation
November 4th, 2021
Administrivia
Cost Estimation: Factors
B(R) = # blocks for relation R
T(R) = # tuples for relation R
V(R, a) = # of unique values of attribute a in relation R
M = # of available memory pages
Cost Estimation: Selection (σ)
Table scan = B(R)
Point Selection:
Index Based Selection (clustered) = B(R)/V(R, a)
Index Based Selection (unclustered) = T(R)/V(R, a)
Cost Estimation Disk I/O Formulas
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)
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)
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)
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
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?
Selectivity Formulas
Cardinality Estimation Example
Cardinality Estimation
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
SELECT sname
FROM Supply x, Supplier y
WHERE x.sid=y.sid AND x.pno=2 AND y.scity=’Seattle’ AND y.sstate=’WA’;
Supply(sid, pno, quantity)
Supplier(sid, sname, scity, sstate)
⋈sid=sid
Cardinality Estimation
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
Supply Statistics:
Supplier Statistics:
⋈sid=sid
Cardinality Estimation
Supply Statistics:
Supplier Statistics:
T(Supply) * 1 / V(Supply, pno)
= 4
T(Supplier) * 1 / V(Supplier, scity) * 1 / V(Supplier, sstate)
= 5
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
Cardinality Estimation
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 5
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
Cardinality Estimation
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 50
But wait a second…. Seattle is in Washington!
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
Cardinality Estimation
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 50
Min(T1, T2)
= 4
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
Cardinality Estimation
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 50
T3 = 4
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
Cardinality Estimation
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 50
T3 = 4
No filtering at this step
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
Cardinality Estimation
Supply Statistics:
Supplier Statistics:
T1 = 4
T2 = 50
T3 = 4
Total = 4
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
Cost Estimation Example
Cost Estimation
Supply Statistics:
Supplier Statistics:
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
(Hash Join)
Cost Estimation
Supply Statistics:
Supplier Statistics:
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
(Hash Join)
(On the fly)
(Sequential
Scan)
(Sequential
Scan)
Cost Estimation
Supply Statistics:
Supplier Statistics:
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
(Hash Join)
(On the fly)
(Sequential
Scan)
(Sequential
Scan)
(On the fly)
Cost Estimation
Supply Statistics:
Supplier Statistics:
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
(Hash Join)
(On the fly)
(On the fly)
Cost = B(Supply) = 100
Cost = B(Supplier) = 100
Cost Estimation
Supply Statistics:
Supplier Statistics:
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
Cost = 0
Cost = 0
C1 = 100
C2 = 100
Cost Estimation
Supply Statistics:
Supplier Statistics:
Supply
σpno=2
Supplier
σscity=’Seattle’ Λ sstate=’WA’
πsname
⋈sid=sid
Total Cost = 100 + 100 = 200 I/Os
C1 = 100
C2 = 100
C3 = 0
C4 = 0
Pipelined Execution