query optimization problem pick the best plan from the space of physical plans
DESCRIPTION
Heuristics for pruning plan space Predicates as early as possible Avoid plans with cross products Only left-deep join treesTRANSCRIPT
![Page 1: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/1.jpg)
Query Optimization Problem
Pick the best plan from the space of physical plans
![Page 2: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/2.jpg)
Cost-Based Optimization
• Prune the space of plans using heuristics• Estimate cost for remaining plans
– Be smart about how you iterate through plans• Pick the plan with least cost
Focus on queries with joins
![Page 3: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/3.jpg)
Heuristics for pruning plan space
• Predicates as early as possible• Avoid plans with cross products• Only left-deep join trees
![Page 4: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/4.jpg)
Physical Plan Selection
Logical Query Plan
P1 P2 …. Pn
C1 C2 …. Cn Pick minimum cost one
Physical plans
Costs
![Page 5: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/5.jpg)
Simple Cost Model
Cost (R S) = T(R) + T(S) Cost (R S) = T(R) + T(S)
All other operators have 0 cost
Note: The simple cost model used for illustration only
![Page 6: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/6.jpg)
Cost Model Example
R S
T
X
T(R) + T(S)
T(X) + T(T)
Total Cost: T(R) + T(S) + T(T) + T(X)
![Page 7: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/7.jpg)
Selinger Algorithm
• Dynamic Programming based• Dynamic Programming:
– General algorithmic paradigm– Exploits “principle of optimality”
![Page 8: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/8.jpg)
Principle of Optimality
Optimal for “whole” made up from optimal for “parts”
![Page 9: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/9.jpg)
Principle of Optimality
Query: R1 R2 R3 R4 R5
R3 R2
R4R1
R5Optimal Plan:
![Page 10: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/10.jpg)
Principle of Optimality
Query: R1 R2 R3 R4 R5
R3 R2
R4R1
R5Optimal Plan:
Optimal plan for joining R3, R2, R4, R1
![Page 11: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/11.jpg)
Principle of Optimality
Query: R1 R2 R3 R4 R5
R3 R2
R4R1
R5Optimal Plan:
Optimal plan for joining R3, R2, R4
![Page 12: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/12.jpg)
Exploiting Principle of Optimality
Query: R1 R2 … Rn
R3 R1
R2
R2 R3
R1
Optimalfor joining R1, R2, R3
Sub-Optimalfor joining R1, R2, R3
![Page 13: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/13.jpg)
Exploiting Principle of Optimality
R3 R1
R2
Ri
RjSub-Optimal
for joining R1,…,Rn
A sub-optimal sub-plan cannot lead to anoptimal plan
![Page 14: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/14.jpg)
Query: R1 R2 R3 R4
{ R1 } { R2 } { R3 } { R4 }
{ R1, R2 } { R1, R3 } { R1, R4 } { R2, R3 } { R2, R4 } { R3, R4 }
{ R1, R2, R3 } { R1, R2, R4 } { R1, R3, R4 } { R2, R3, R4 }
{ R1, R2, R3, R4 }
Progressof
algorithm
Selinger Algorithm:
![Page 15: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/15.jpg)
Notation
OPT ( { R1, R2, R3 } ):
Cost of optimal plan to join R1,R2,R3
T ( { R1, R2, R3 } ):
Number of tuples in R1 R2 R3
![Page 16: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/16.jpg)
OPT ( { R1, R2, R3 } ):
OPT ( { R2, R3 } ) + T ( { R2, R3 } ) + T(R1)
OPT ( { R1, R2 } ) + T ( { R1, R2 } ) + T(R3)
OPT ( { R1, R3 } ) + T ( { R1, R3 } ) + T(R2)
Min
Selinger Algorithm:
Note: Valid only for the simple cost model
![Page 17: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/17.jpg)
Query: R1 R2 R3 R4
{ R1 } { R2 } { R3 } { R4 }
{ R1, R2 } { R1, R3 } { R1, R4 } { R2, R3 } { R2, R4 } { R3, R4 }
{ R1, R2, R3 } { R1, R2, R4 } { R1, R3, R4 } { R2, R3, R4 }
{ R1, R2, R3, R4 }
Progressof
algorithm
Selinger Algorithm:
![Page 18: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/18.jpg)
Query: R1 R2 R3 R4
{ R1 } { R2 } { R3 } { R4 }
{ R1, R2 } { R1, R3 } { R1, R4 } { R2, R3 } { R2, R4 } { R3, R4 }
{ R1, R2, R3 } { R1, R2, R4 } { R1, R3, R4 } { R2, R3, R4 }
{ R1, R2, R3, R4 }
Progressof
algorithm
Selinger Algorithm:
![Page 19: Query Optimization Problem Pick the best plan from the space of physical plans](https://reader038.vdocuments.us/reader038/viewer/2022101123/5a4d1bfa7f8b9ab0599eb4e2/html5/thumbnails/19.jpg)
R2
R3
R4
R1
Selinger Algorithm:
Optimal plan:
Query: R1 R2 R3 R4