Docsity

Query Optimization - Advanced Database System - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

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

Advanced Database Systems

Notes 08:Query Optimization

(Plan Space, Query Rewrites)

parse

Query rewriting

Physical plan generation

execute result

SQL query

parse tree

statistics logical query plan

physical query plan

Query Processing - In class order

2; 16.

3; 16.2,16.

1; 13, 15

4; 16.4—16.

Query Optimization Problem

Pick the best plan from the space of physical plans

The Space of Physical Plans is Very Large

  • Algebraic equivalences
  • Different physical operators for the same logical operator - nested loop join, hash join, sort-merge join - index-scan, table-scan
  • Different plumbing details - pipelining vs. materialization
  • Different tree shapes

Approaches for Query Optimization

  • Approach 1: Pick some plan
    • Bad plans can be really bad!
  • Approach 2: Heuristics
    • Ex: maximize use of indexes (MySQL)
  • Approach 3: Cost-based
    • “Enumerate”, find cost, pick best
    • Be smart about how you iterate through the plans. Why?
  • Hybrid

Query Optimization in Practice

  • Hybrid
  • Use heuristics, called query rewrite rules
    • eliminate many of the really bad plans
    • avoid eliminating good plans
  • Cost-based
    • Be smart about how you iterate through plans
    • Ex: dynamic programming, genetic search

Why do we need Query Rewriting?

  • Pruning the HUGE space of physical plans
    • Eliminating redundant conditions/operators
    • Rules that will improve performance with very high probability
  • Preprocessing
    • Getting queries into a form that we know how to handle best

 Reduces optimization time drastically without noticeably affecting quality

Query Rewrite Rules

  • Transform one logical plan into another
    • Do not use statistics
  • Equivalences in relational algebra
  • Push-down predicates
  • Do projects early
  • Avoid cross-products if possible
  • Use left-deep trees
  • Use of constraints, e.g., uniqueness

Example: Parse Tree

SELECT FROM WHERE AND B R S

R.A = “c”

R.C = S.C

D

Select B,D From R,S Where R.A = “c” ∧ R.C=S.C

Along with Parsing …

  • Semantic checks
    • Do the projected attributes exist in the relations in the From clause?
    • Ambiguous attributes?
    • Type checking, ex: R.A > 17.
  • Expand views

Apply Rewrite Rule (1)

ΠB,D [ σR.C=S.C [σR.A=“c” (R X S)]]

πB,D

σR.A = “c” Λ R.C = S.C

X

R S

πB,D

σR.A = “c”

X R S

σR.C = S.C

Apply Rewrite Rule (2)

ΠB,D [ σR.C=S.C [σR.A=“c” (R)] X S]

πB,D

σR.A = “c”

X

R

S

σR.C = S.C

πB,D

σR.A = “c”

X

R S

σR.C = S.C

Equivalences in Relational Algebra

R S = S R Commutativity

(R S) T = R (S T) Associativity

Also holds for: Cross Products, Union, Intersection R x S = S x R (R x S) x T = R x (S x T) R U S = S U R R U (S U T) = (R U S) U T

Rules: Project

Let: X = set of attributes Y = set of attributes XY = X U Y

πxy (R) = πx [πy (R)]