Docsity

Database Query Optimization: Understanding Query Plans and Cost Analysis, Slides of Introduction to Database Management Systems

Partial preview of the text

Download Database Query Optimization: Understanding Query Plans and Cost Analysis and more Slides Introduction to Database Management Systems in PDF only on Docsity!

Database Management Systems Design

Introduction

  • SQL query gets translated into relational algebra

expression

  • Relational algebra expression is represented as tree
    • This is what DBMS “understands” how to process
    • Expression becomes a plan once we identify access methods for each operator
  • Relational algebra expression might have an equivalent

expression

  • Example: R(A,B,C) , S(A, D, F)
  • But, each expression might have different cost
  • How do we find the cheapest expression?

R  S = π R. A , R. B , R. C , S. D , S. F (σ R. A = S. A ( R × S ))

Query Optimizer

  • Module in DBMS in charge of finding cheapest

available plan to execute a query

  • Building one is not easy!
  • Optimizer searches for plans and compares then based

on cost

  • Cost can be:
    • Resource usage
    • Response time
    • Power consumption
    • Number of I/Os
    • Network transmission cost

Query Plans

  • Query plan specifies the operations to be executed
  • Tree of operators
    • Each operator corresponds to a relational operator
  • Leaf nodes usually represent base tables

R S

R S

T

S

ΠA,B

S

ΠA,B

σA>

Pipelined vs Materialized Execution

  • Pipelined
    • The output tuple from one operator immediately becomes input tuple to its parent operator in the tree
  • Materialized
    • The output tuples from one operator must be stored to disk first (into a temporary table)
    • Once the operator finishes, its parent operator can access the materialed tuples
  • Most execution engines use pipelined
    • Saving in I/O can be substantial!
  • Some operator cannot be pipelined
    • Sorting, projections with duplicate elimination
  • Query optimizer must be aware of this issue!

Generation of Query Plans

  • Optimizer generates query plan after a search finds the optimal one
    • According to some criteria
  • Search is a search by construction
    • Alterative plans are built and compared
    • Cheapest one is kept
  • Two major algorithms exist
    • Dynamic programming (SAC+79)
      • Exhaustive search of plan space
      • Finds the optimal
    • Randomized Algorithm
      • Random search of plan space
      • Quickly finds sub-optimal but good plan
  • Optimization philosophy find a good plan quick, avoid bad ones!

Left-deep plans

R S

R S

T

R S

T

U

Each join always has base table as inner table

Bushy Plans

R S

T

R S

R S

T

U

U V

Cost of a plan

• The cost of plan depends on the metric you

wish to optimize

• Resource usage (CPU + I/O + Network)

  • Cost is the sum of the resources used by each operator

• Response time

  • Cost of the slowest path in the tree

• Number of I/Os

  • Cost is the sum of the I/Os generated by each operator Docsity.com

Organization of an optimizer

Query Execution Engine

Query Parser

Parse Tree

Query Plan

SQL Query

Catalog Manager

Catalog

Query Optimizer

Plan Generator

Cost Estimator

Equivalence Rules

• Projections

  • Cascading projections

• Joins

  • Commutative rule
  • Associative rule

R  S = ( S  R )

R  ( S  T ) = ( R  S )  T

π A 1 ( R ) ≡ π A 1 (π A 2 ( π Ak ( R )))

Equivalence Rules(2)

• Commute selections and projections

• Pushing selections

• Decomposing selections

σ P ( R ) =σ p 1 ∧ p 2 ∧...∧ pk ( R )

σ p ( R  S ) = σ p ( R )  S

π A (σ P ( R )) =σ P (π A ( R )) ⇔ p ≡ Aop val

Estimating Cost of Operators

• Key feature for this is selectivity factors,

selectivity, and join costs

• Example:

  • R has no index
    • |R| = 100,000, ||R|| = 5000
  • S has un-clustered Index on Join attribute - |S| = 70,000, ||S|| = 2500
  • What algorithm shall be use for join?
    • Chose between: BNLJ, INLJ, e GHJ with 20 B

R S

System R Search Algorithm

• Idea:

  • Build every possible plan and keep track of
    • Cheapest plan (overall)
    • Cheapest plans that bring data in sorted order (called interesting orders)

• Dynamic Programming (divide-and-conquer)

  • To find plan for n-way join you
    • First find singe table plans
    • Then find plans for all (n-1)-way join and find a plan to join missing table with an (n-1)-way join
    • Plan for smaller joins are saved on a table Docsity.com