Docsity

Cost-based optimization - Advanced Database System - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Cost-based optimization - Advanced Database System - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Advanced Database Systems

Notes 09:Query Optimization

(Cost-based optimization)

Query Optimization Problem

Pick the best plan from the space of

physical plans

Heuristics for pruning plan space

  • Predicates as early as possible
  • Avoid plans with cross products
  • Only left-deep join trees

Physical Plan Selection

Logical Query Plan

P1 P2 …. Pn

C1 C2 …. Cn

Pick minimum cost one

Physical plans

Costs

Simple Cost Model

Cost (R S) = T(R) + T(S)

All other operators have 0 cost

Note: The simple cost model used for illustration only

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)

Principle of Optimality

Optimal for “whole” made up from

optimal for “parts”

Principle of Optimality

Query: (^) R1 R2 R3 R4 R

R3 R

R

R

R

Optimal Plan:

Principle of Optimality

Query: (^) R1 R2 R3 R4 R

R3 R

R

R

R

Optimal Plan:

Optimal plan for joining R3, R2, R

Exploiting Principle of Optimality

Query: (^) R1 R2 … Rn

R3 R

R

R2 R

R

Optimal for joining R1, R2, R

Sub-Optimal for joining R1, R2, R

Query: (^) R1 R2 R3 R

{ 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 }

Progress of algorithm

Selinger Algorithm:

Notation

OPT ( { R1, R2, R3 } ):

Cost of optimal plan to join R1,R2,R

T ( { R1, R2, R3 } ):

Number of tuples in R1 R2 R

Query: (^) R1 R2 R3 R

{ 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 }

Progress of algorithm

Selinger Algorithm:

Query: (^) R1 R2 R3 R

{ 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 }

Progress of algorithm

Selinger Algorithm: