Top Banner
CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites) Shivnath Babu
38

CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Dec 31, 2015

Download

Documents

nyssa-tanner

CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites). Shivnath Babu. SQL query. parse. parse tree. Query rewriting. statistics. logical query plan. Physical plan generation. physical query plan. execute. result. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

CPS216: Advanced Database Systems

Notes 08:Query Optimization (Plan Space, Query Rewrites)

Shivnath Babu

Page 2: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

parse

Query rewriting

Physical plan generation

execute

result

SQL query

parse tree

logical query planstatistics

physical query plan

Query Processing - In class order

2; 16.1

3; 16.2,16.3

1; 13, 15

4; 16.4—16.7

Page 3: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Roadmap• Query optimization: problem definition• Space of physical plans

– Counting exercise

• Approaches for query optimization– Heuristic-based (Oracle calls them rule-based)– Cost-based– Hybrid

• Heuristics for query optimization (Query rewrites)

Page 4: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Query Optimization Problem

Pick the best plan from the space of physical plans

Page 5: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

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

Page 6: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

A Plan Counting Exercise

• Work on blackboard

Page 7: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

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

Page 8: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

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

Page 9: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

parse

Query rewriting

Physical plan generation

execute

result

SQL query

parse tree

logical query planstatistics

physical query plan

Initial logical plan

“Best” logical plan

Logical plan

Rewrite rules

Page 10: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

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

Page 11: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

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

Page 12: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Example Query

Select B,D

From R,S

Where R.A = “c” R.C=S.C

Page 13: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Example: Parse Tree<Query>

<SFW>

SELECT <SelList> FROM <FromList> WHERE <Cond>

<Attribute> <SelList> <RelName> <FromList> <Cond> AND <Cond>

B <Attribute> R <RelName>

S<Attr> <Op> <Const>

<Attr> <Op> <Attr>

R.A = “c”

R.C S.C=

D

Select B,DFrom R,SWhere R.A = “c” R.C=S.C

Page 14: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

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.5

• Expand views

Page 15: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Initial Logical Plan

Relational Algebra: B,D [ R.A=“c” R.C = S.C (RXS)]

Select B,DFrom R,SWhere R.A = “c” R.C=S.C

B,D

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

X

R S

Page 16: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

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

Page 17: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

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

Page 18: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Apply Rewrite Rule (3)

B,D [[R.A=“c”(R)] S]

B,D

R.A = “c”

R

S

B,D

R.A = “c”

X

R

S

R.C = S.CNatural join

Page 19: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

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

Page 20: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Rules: Project

Let: X = set of attributes

Y = set of attributes

XY = X U Y

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

Page 21: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Let p = predicate with only R attribs

q = predicate with only S attribs

m = predicate with only R,S attribs

p (R S) =

q (R S) =

Rules: combined

[p (R)] S

R [q (S)]

Page 22: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Rules: combined (continued)

pq (R S) = [p (R)] [q (S)]

pqm (R S) =

m [(p R) (q S)]pvq (R S) =

[(p R) S] U [R (q S)]

Page 23: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

p1p2 (R) p1 [p2 (R)]

p (R S) [p (R)] S

R S S R

x [p (R)] x {p [xz (R)]}

Which are “good” transformations?

Page 24: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Conventional wisdom: do projects early

Example: R(A,B,C,D,E) x={E} P: (A=3) (B=“cat”)

x {p (R)} vs. E {p{ABE(R)}}

Page 25: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

But: What if we have A, B indexes?

B = “cat” A=3

Intersect pointers to get

pointers to matching tuples

Page 26: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Bottom line:

• No transformation is always good

• Some are usually good: – Push selections down– Avoid cross-products if possible– Subqueries Joins

Page 27: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

More 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• Subqueries Joins• Use of constraints, e.g., uniqueness

Page 28: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Avoid Cross Products (if possible)

• Which join trees avoid cross-products?• If you can't avoid cross products, perform

them as late as possible

Select B,DFrom R,S,T,UWhere R.A = S.B R.C=T.C R.D = U.D

Page 29: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Use Left Deep Plans

• What are some left-deep, right-deep, and bushy plans for this query?

• Why is this heuristic useful?

– Reason #1: We maximize the possibility of using indexes

– Reason #2: Better for nested-loop join

• What about hash joins?

• Homework: Construct examples where (i) right-deep plan is best, (ii) where bushy is best

Select B,DFrom R,S,T,UWhere R.A = S.A R.A=T.A R.A = U.A

Page 30: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

More 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• Subqueries Joins• Use of constraints, e.g., uniqueness

Page 31: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

SQL Query with an Uncorrelated SubqueryFind the movies with stars born in 1960

MovieStar(name, address, gender, birthdate) StarsIn(title, year, starName)

SELECT titleFROM StarsInWHERE starName IN (

SELECT nameFROM MovieStarWHERE birthdate LIKE ‘%1960’

);

Page 32: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Parse Tree<Query>

<SFW>

SELECT <SelList> FROM <FromList> WHERE <Condition>

<Attribute> <RelName> <Tuple> IN <Query>

title StarsIn <Attribute> ( <Query> )

starName <SFW>

SELECT <SelList> FROM <FromList> WHERE <Condition>

<Attribute> <RelName> <Attribute> LIKE <Pattern>

name MovieStar birthDate ‘%1960’

Page 33: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Generating Relational Algebra

title

StarsIn <condition>

<tuple> IN name

<attribute> birthdate LIKE ‘%1960’

starName MovieStar

Two-argument selection

Page 34: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Rewrite Rule for Two-argument Selection with Conditions Involving IN

Lexp <condition>

<tuple> IN Rexp

Two-argument selection

Lexp

Rexp

δ

X

<condition>

Page 35: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Applying the Rewrite Rule

title

StarsIn <condition>

<tuple> IN name

<attribute> birthdate LIKE ‘%1960’

starName MovieStar

title

starName=name

StarsIn δ

birthdate LIKE ‘%1960’

MovieStar

name

Page 36: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

Improving the Logical Query Plan

title

starName=name

StarsIn name

birthdate LIKE ‘%1960’

MovieStar

title

starName=name

StarsIn δ

birthdate LIKE ‘%1960’

MovieStar

name

Page 37: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

SQL Query with an Correlated Subquery

MovieStar(name, address, gender, birthdate) StarsIn(title, year, starName)

SELECT titleFROM StarsInWHERE starName IN (

SELECT nameFROM MovieStarWHERE name LIKE ‘Tom%’ and year = birthdate + 30

);

Page 38: CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites)

parse

Query rewriting

Physical plan generation

execute

result

SQL query

parse tree

logical query planstatistics

physical query plan

Query Processing - In class order

2; 16.1

3; 16.2,16.3

1; 13, 15

4; 16.4—16.7