Query Execution 2 and Query Optimization Instructor: Matei Zaharia - - PowerPoint PPT Presentation

query execution 2 and query optimization
SMART_READER_LITE
LIVE PREVIEW

Query Execution 2 and Query Optimization Instructor: Matei Zaharia - - PowerPoint PPT Presentation

Query Execution 2 and Query Optimization Instructor: Matei Zaharia cs245.stanford.edu Query Execution Overview Query representation (e.g. SQL) Logical query plan (e.g. relational algebra) Query optimization Optimized logical plan Physical


slide-1
SLIDE 1

Query Execution 2 and Query Optimization

Instructor: Matei Zaharia cs245.stanford.edu

slide-2
SLIDE 2

Query Execution Overview

Query representation

(e.g. SQL)

Logical query plan

(e.g. relational algebra)

Optimized logical plan Physical plan

(code/operators to run)

CS 245 2

Query optimization

slide-3
SLIDE 3

Example SQL Query

SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE birthdate LIKE ‘%1960’ ); (Find the movies with stars born in 1960)

CS 245 3

slide-4
SLIDE 4

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’

CS 245 4

slide-5
SLIDE 5

Ptitle sstarName=name

StarsIn Pname

sbirthdate LIKE ‘%1960’

MovieStar

´

Logical Query Plan

CS 245 5

slide-6
SLIDE 6

Improved Logical Query Plan

Ptitle

starName=name

StarsIn Pname

sbirthdate LIKE ‘%1960’

MovieStar

CS 245 6

slide-7
SLIDE 7

Need expected size StarsIn MovieStar P s

Estimate Result Sizes

CS 245 7

slide-8
SLIDE 8

Parameters: join order, memory size, project attributes, ... Hash join Seq scan Index scan Parameters: select condition, ... StarsIn MovieStar

One Physical Plan

H

CS 245 8

slide-9
SLIDE 9

Parameters: join order, memory size, project attributes, ... Hash join Index scan Seq scan Parameters: select condition, ... StarsIn MovieStar

Another Physical Plan

H

CS 245 9

slide-10
SLIDE 10

Sort-merge join Seq scan Seq scan StarsIn MovieStar

Another Physical Plan

CS 245 10

slide-11
SLIDE 11

Logical plan P1 P2 … Pn C1 C2 … Cn Pick best!

Estimating Plan Costs

Physical plan candidates

CS 245 11

slide-12
SLIDE 12

Execution Methods: Once We Have a Plan, How to Run it?

Several options that trade between complexity, performance and startup time

CS 245 12

slide-13
SLIDE 13

Example: Simple Query

SELECT quantity * price FROM orders WHERE productId = 75

Pquanity*price (σproductId=75 (orders))

CS 245 13

slide-14
SLIDE 14

Method 1: Interpretation

interface Operator { Tuple next(); } class TableScan: Operator { String tableName; } class Select: Operator { Operator parent; Expression condition; } class Project: Operator { Operator parent; Expression[] exprs; }

CS 245 14

interface Expression { Value compute(Tuple in); } class Attribute: Expression { String name; } class Times: Expression { Expression left, right; } class Equals: Expression { Expression left, right; }

slide-15
SLIDE 15

Example Expression Classes

CS 245 15

class Attribute: Expression { String name; Value compute(Tuple in) { return in.getField(name); } } class Times: Expression { Expression left, right; Value compute(Tuple in) { return left.compute(in) * right.compute(in); } }

probably better to use a numeric field ID instead

slide-16
SLIDE 16

Example Operator Classes

CS 245 16

class TableScan: Operator { String tableName; Tuple next() { // read & return next record from file } } class Project: Operator { Operator parent; Expression[] exprs; Tuple next() { tuple = parent.next(); fields = [expr.compute(tuple) for expr in exprs]; return new Tuple(fields); } }

slide-17
SLIDE 17

Running Our Query with Interpretation

CS 245 17

  • ps = Project(

expr = Times(Attr(“quantity”), Attr(“price”)), parent = Select( expr = Equals(Attr(“productId”), Literal(75)), parent = TableScan(“orders”) ) ); while(true) { Tuple t = ops.next(); if (t != null) {

  • ut.write(t);

} else { break; } }

Pros & cons of this approach?

recursively calls Operator.next() and Expression.compute()

slide-18
SLIDE 18

Method 2: Vectorization

Interpreting query plans one record at a time is simple, but it’s too slow

» Lots of virtual function calls and branches for each record (recall Jeff Dean’s numbers)

Keep recursive interpretation, but make Operators and Expressions run on batches

CS 245 18

slide-19
SLIDE 19

Implementing Vectorization

CS 245 19

class TupleBatch { // Efficient storage, e.g. // schema + column arrays } interface Operator { TupleBatch next(); } class Select: Operator { Operator parent; Expression condition; } ... class ValueBatch { // Efficient storage } interface Expression { ValueBatch compute( TupleBatch in); } class Times: Expression { Expression left, right; } ...

slide-20
SLIDE 20

Typical Implementation

Values stored in columnar arrays (e.g. int[]) with a separate bit array to mark nulls Tuple batches fit in L1 or L2 cache Operators use SIMD instructions to update both values and null fields without branching

CS 245 20

slide-21
SLIDE 21

Pros & Cons of Vectorization

+ Faster than record-at-a-time if the query processes many records + Relatively simple to implement – Lots of nulls in batches if query is selective – Data travels between CPU & cache a lot

CS 245 21

slide-22
SLIDE 22

Method 3: Compilation

Turn the query into executable code

CS 245 22

slide-23
SLIDE 23

Compilation Example

Pquanity*price (σproductId=75 (orders))

class MyQuery { void run() { Iterator<OrdersTuple> in = openTable(“orders”); for(OrdersTuple t: in) { if (t.productId == 75) {

  • ut.write(Tuple(t.quantity * t.price));

} } } }

CS 245 23

generated class with the right field types for orders table

Can also theoretically generate vectorized code

slide-24
SLIDE 24

Pros & Cons of Compilation

+ Potential to get fastest possible execution + Leverage existing work in compilers – Complex to implement – Compilation takes time – Generated code may not match hand-written

CS 245 24

slide-25
SLIDE 25

What’s Used Today?

Depends on context & other bottlenecks Transactional databases (e.g. MySQL): mostly record-at-a-time interpretation Analytical systems (Vertica, Spark SQL): vectorization, sometimes compilation ML libs (TensorFlow): mostly vectorization (the records are vectors!), some compilation

CS 245 25

slide-26
SLIDE 26

Query Optimization

CS 245 26

slide-27
SLIDE 27

Outline

What can we optimize? Rule-based optimization Data statistics Cost models Cost-based plan selection

CS 245 27

slide-28
SLIDE 28

Outline

What can we optimize? Rule-based optimization Data statistics Cost models Cost-based plan selection

CS 245 28

slide-29
SLIDE 29

What Can We Optimize?

Operator graph: what operators do we run, and in what order? Operator implementation: for operators with several impls (e.g. join), which one to use? Access paths: how to read each table?

» Index scan, table scan, C-store projections, …

CS 245 29

slide-30
SLIDE 30

Typical Challenge

There is an exponentially large set of possible query plans Result: we’ll need techniques to prune the search space and complexity involved

Access paths for table 1 Access paths for table 2 Algorithms for join 1 Algorithms for join 2

⨯ ⨯ ⨯ ⨯ …

CS 245 30

slide-31
SLIDE 31

Outline

What can we optimize? Rule-based optimization Data statistics Cost models Cost-based plan selection

CS 245 31

slide-32
SLIDE 32

What is a Rule?

Procedure to replace part of the query plan based on a pattern seen in the plan Example: When I see expr OR TRUE for an expression expr, replace this with TRUE

CS 245 32

slide-33
SLIDE 33

Implementing Rules

Each rule is typically a function that walks through query plan to search for its pattern

void replaceOrTrue(Plan plan) { for (node in plan.nodes) { if (node instanceof Or) { if (node.right == Literal(true)) { plan.replace(node, Literal(true)); break; } // Similar code if node.left == Literal(true) } } }

Or expr TRUE

node node.left node.right

CS 245 33

slide-34
SLIDE 34

Implementing Rules

Rules are often grouped into phases

» E.g. simplify Boolean expressions, pushdown selects, choose join algorithms, etc

Each phase runs rules till they no longer apply

plan = originalPlan; while (true) { for (rule in rules) { rule.apply(plan); } if (plan was not changed by any rule) break; }

CS 245 34

slide-35
SLIDE 35

Result

Simple rules can work together to optimize complex query plans (if designed well):

SELECT * FROM users WHERE (age>=16 && loc==CA) || (age>=16 && loc==NY) || age>=18 (age>=16) && (loc==CA || loc==NY) || age>=18 (age>=16 && (loc IN (CA, NY)) || age>=18 age>=18 || (age>=16 && (loc IN (CA, NY))

CS 245 35

slide-36
SLIDE 36

Example Extensible Optimizer

For Thursday, you’ll read about Spark SQL’s Catalyst optimizer

» Written in Scala using its pattern matching features to simplify writing rules » >500 contributors worldwide, >1000 types of expressions, and hundreds of rules

We’ll also use Spark SQL in assignment 2

CS 245 36

slide-37
SLIDE 37

CS 245 37

slide-38
SLIDE 38

CS 245 38

slide-39
SLIDE 39

Common Rule-Based Optimizations

Simplifying expressions in select, project, etc

» Boolean algebra, numeric expressions, string expressions, etc » Many redundancies because queries are

  • ptimized for readability or generated by code

Simplifying relational operator graphs

» Select, project, join, etc

These relational optimizations have the most impact

CS 245 39

slide-40
SLIDE 40

Common Rule-Based Optimizations

Selecting access paths and operator implementations in simple cases

» Index column predicate ⇒ use index » Small table ⇒ use hash join against it » Aggregation on field with few values ⇒ use in-memory hash table

Rules also often used to do type checking and analysis (easy to write recursively)

Also very high impact

CS 245 40

slide-41
SLIDE 41

Common Relational Rules

Push selects as far down the plan as possible Recall: σp(R ⨝ S) = σp(R) ⨝ S

if p only references R

σq(R ⨝ S) = R ⨝ σq(S)

if q only references S

σp∧q(R ⨝ S) = σp(R) ⨝ σq(S)

if p on R, q on S

CS 245 41

Idea: reduce # of records early to minimize work in later ops; enable index access paths

slide-42
SLIDE 42

Common Relational Rules

Push projects as far down as possible Recall:

Px(σp(R)) = Px(σp(Px∪z(R)))

z = the fields in p

Px∪y(R ⨝ S) = Px∪y ((Px∪z (R)) ⨝ (Py∪z (S)))

x = fields in R, y = in S, z = in both

CS 245 42

Idea: don’t process fields you’ll just throw away

slide-43
SLIDE 43

Project Rules Can Backfire!

Example: R has fields A, B, C, D, E p: A=3 ∧ B=“cat” x: {E}

Px(σp(R)) vs Px(σp(P{A,B,E}(R)))

CS 245 43

slide-44
SLIDE 44

What if R has Indexes?

A = 3 B = “cat” Intersect buckets to get pointers to matching tuples

CS 245 44

In this case, should do σp(R) first!

slide-45
SLIDE 45

Bottom Line

Many possible transformations aren’t always good for performance Need more info to make good decisions

» Data statistics: properties about our input or intermediate data to be used in planning » Cost models: how much time will an operator take given certain input data statistics?

CS 245 45

slide-46
SLIDE 46

Outline

What can we optimize? Rule-based optimization Data statistics Cost models Cost-based plan selection

CS 245 46

slide-47
SLIDE 47

What Are Data Statistics?

Information about the tuples in a relation that can be used to estimate size & cost

» Example: # of tuples, average size of tuples, # distinct values for each attribute, % of null values for each attribute

Typically maintained by the storage engine as tuples are added & removed in a relation

» File formats like Parquet can also have them

CS 245 47

slide-48
SLIDE 48

Some Statistics We’ll Use

For a relation R, T(R) = # of tuples in R S(R) = average size of R’s tuples in bytes B(R) = # of blocks to hold all of R’s tuples V(R, A) = # distinct values of attribute A in R

CS 245 48

slide-49
SLIDE 49

Example

CS 245 49

R: A: 20 byte string B: 4 byte integer C: 8 byte date D: 5 byte string

A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d

slide-50
SLIDE 50

Example

CS 245 50

T(R) = 5 S(R) = 37 V(R, A) = 3 V(R, C) = 5 V(R, B) = 1 V(R, D) = 4 R: A: 20 byte string B: 4 byte integer C: 8 byte date D: 5 byte string

A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d

slide-51
SLIDE 51

Challenge: Intermediate Tables

Keeping stats for tables on disk is easy, but what about intermediate tables that appear during a query plan? Examples: σp(R) R ⨝ S

CS 245 51

We already have T(R), S(R), V(R, a), etc, but how to get these for tuples that pass p? How many and what types of tuple pass the join condition?

Should we do (R ⨝ S) ⨝ T or R ⨝ (S ⨝ T) or (R ⨝ T) ⨝ S?

slide-52
SLIDE 52

Stat Estimation Methods

Algorithms to estimate subplan stats An ideal algorithm would have:

1) Accurate estimates of stats 2) Low cost 3) Consistent estimates (e.g. different plans for a subtree give same estimated stats)

Can’t always get all this!

CS 245 52

slide-53
SLIDE 53

Size Estimates for W = R1⨯R2

S(W) = T(W) =

CS 245 53

slide-54
SLIDE 54

Size Estimates for W = R1⨯R2

S(W) = T(W) =

CS 245 54

S(R1) + S(R2) T(R1) ´ T(R2)

slide-55
SLIDE 55

Size Estimate for W = σA=a(R)

S(W) = T(W) =

CS 245 55

slide-56
SLIDE 56

Size Estimate for W = σA=a(R)

S(W) = S(R) T(W) =

CS 245 56

Not true if some variable-length fields are correlated with value of A

slide-57
SLIDE 57

Example

CS 245 57

R V(R,A)=3 V(R,B)=1 V(R,C)=5 V(R,D)=4 W = σZ=val(R) T(W) =

A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d

slide-58
SLIDE 58

Example

CS 245 58

R V(R,A)=3 V(R,B)=1 V(R,C)=5 V(R,D)=4 W = σZ=val(R) T(W) =

A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d what is probability this tuple will be in answer?

slide-59
SLIDE 59

Example

CS 245 59

R V(R,A)=3 V(R,B)=1 V(R,C)=5 V(R,D)=4 W = σZ=val(R) T(W) =

A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d

T(R) V(R,Z)

slide-60
SLIDE 60

Assumption:

Values in select expression Z=val are uniformly distributed over all V(R, Z) values

CS 245 60

slide-61
SLIDE 61

Alternate Assumption:

Values in select expression Z=val are uniformly distributed over a domain with DOM(R, Z) values

CS 245 61

slide-62
SLIDE 62

Example

CS 245 62

R V(R,A)=3, DOM(R,A)=10 V(R,B)=1, DOM(R,B)=10 V(R,C)=5, DOM(R,C)=10 V(R,D)=4, DOM(R,D)=10 W = σZ=val(R) T(W) =

A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d

Alternate assumption

slide-63
SLIDE 63

Example

CS 245 63

R V(R,A)=3, DOM(R,A)=10 V(R,B)=1, DOM(R,B)=10 V(R,C)=5, DOM(R,C)=10 V(R,D)=4, DOM(R,D)=10 W = σZ=val(R) T(W) =

A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d

Alternate assumption

what is probability this tuple will be in answer?

slide-64
SLIDE 64

Example

CS 245 64

R V(R,A)=3, DOM(R,A)=10 V(R,B)=1, DOM(R,B)=10 V(R,C)=5, DOM(R,C)=10 V(R,D)=4, DOM(R,D)=10 W = σZ=val(R) T(W) =

A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d

T(R) DOM(R,Z) Alternate assumption

slide-65
SLIDE 65

SC(R, A) = average # records that satisfy equality condition on R.A T(R) V(R,A) SC(R,A) = T(R) DOM(R,A)

CS 245 65

Selection Cardinality

slide-66
SLIDE 66

What About W = σz ³ val(R)?

T(W) = ?

CS 245 66

slide-67
SLIDE 67

What About W = σz ³ val(R)?

T(W) = ? Solution 1: T(W) = T(R) / 2

CS 245 67

slide-68
SLIDE 68

What About W = σz ³ val(R)?

T(W) = ? Solution 1: T(W) = T(R) / 2 Solution 2: T(W) = T(R) / 3

CS 245 68

slide-69
SLIDE 69

Solution 3: Estimate Fraction of Values in Range

Example: R

CS 245 69

Z Min=1 V(R,Z)=10 W = σz ³ 15(R) Max=20

f = 20-15+1 = 6 (fraction of range) 20-1+1 20 T(W) = f ´ T(R)

slide-70
SLIDE 70

Equivalently, if we know values in column: f = fraction of distinct values ≥ val T(W) = f ´ T(R)

CS 245 70

Solution 3: Estimate Fraction of Values in Range

slide-71
SLIDE 71

What About More Complex Expressions?

E.g. estimate selectivity for SELECT * FROM R WHERE user_defined_func(a) > 10

CS 245 71

slide-72
SLIDE 72

CS 245 72

slide-73
SLIDE 73

Size Estimate for W = R1 ⨝ R2

Let X = attributes of R1 Y = attributes of R2

CS 245 73

Case 1: X ∩ Y = ∅: Same as R1 x R2

slide-74
SLIDE 74

R1 A B C R2 A D

CS 245 74

Case 2: W = R1 ⨝ R2, X ∩ Y = A

slide-75
SLIDE 75

R1 A B C R2 A D

CS 245 75

Case 2: W = R1 ⨝ R2, X ∩ Y = A

Assumption (“containment of value sets”):

V(R1, A) £ V(R2, A) Þ Every A value in R1 is in R2 V(R2, A) £ V(R1, A) Þ Every A value in R2 is in R1

slide-76
SLIDE 76

R1 A B C R2 A D

Take 1 tuple Match

Computing T(W) when V(R1, A) £ V(R2, A)

CS 245 76

1 tuple matches with T(R2) tuples... V(R2, A) so T(W) = T(R1) ´ T(R2) V(R2, A)

slide-77
SLIDE 77

CS 245 77

V(R1, A) £ V(R2, A) ⇒ T(W) = T(R1) ´ T(R2)

V(R2, A)

V(R2, A) £ V(R1, A) ⇒ T(W) = T(R1) ´ T(R2)

V(R1, A)

slide-78
SLIDE 78

T(W) = T(R1) ⨯ T(R2) max(V(R1, A), V(R2, A))

CS 245 78

In General for W = R1 ⨝ R2

Where A is the common attribute set

slide-79
SLIDE 79

Values uniformly distributed over domain R1 A B C R2 A D

This tuple matches T(R2) / DOM(R2, A), so

T(W) = T(R1) T(R2) = T(R1) T(R2) DOM(R2, A) DOM(R1, A)

Assume these are the same

CS 245 79

Case 2 with Alternate Assumption

slide-80
SLIDE 80

Tuple Size after Join

In all cases: S(W) = S(R1) + S(R2) – S(A) size of attribute A

CS 245 80

slide-81
SLIDE 81

PAB(R)

σA=aÙB=b(R) R ⨝ S with common attributes A, B, C Set union, intersection, difference, …

CS 245 81

Using Similar Ideas, Can Estimate Sizes of:

slide-82
SLIDE 82

E.g. W = σA=a(R1) ⨝ R2 Treat as relation U T(U) = T(R1) / V(R1, A) S(U) = S(R1) Also need V(U, *) !!

CS 245 82

For Complex Expressions, Need Intermediate T, S, V Results

slide-83
SLIDE 83

To Estimate V

E.g., U = σA=a(R1) Say R1 has attributes A, B, C, D V(U, A) = V(U, B) = V(U, C) = V(U, D) =

CS 245 83

slide-84
SLIDE 84

R1 V(R1, A)=3 V(R1, B)=1 V(R1, C)=5 V(R1, D)=3 U = σA=a(R1)

A B C D cat 1 10 10 cat 1 20 20 dog 1 30 10 dog 1 40 30 bat 1 50 10

CS 245 84

Example

slide-85
SLIDE 85

R1 V(R1, A)=3 V(R1, B)=1 V(R1, C)=5 V(R1, D)=3 U = σA=a(R1)

A B C D cat 1 10 10 cat 1 20 20 dog 1 30 10 dog 1 40 30 bat 1 50 10

CS 245 85

Example

V(U, A) = 1 V(U, B) = 1 V(U, C) = T(R1) V(R1,A) V(U, D) = somewhere in between…

slide-86
SLIDE 86

V(U, A) = V(R, A) / 2 V(U, B) = V(R, B)

CS 245 86

Possible Guess in U = σA≥a(R)

slide-87
SLIDE 87

For Joins: U = R1(A,B) ⨝ R2(A,C)

We’ll use the following estimates: V(U, A) = min(V(R1, A), V(R2, A)) V(U, B) = V(R1, B) V(U, C) = V(R2, C) Called “preservation of value sets”

CS 245 87

slide-88
SLIDE 88

Example:

Z = R1(A,B) ⨝ R2(B,C) ⨝ R3(C,D)

T(R1) = 1000 V(R1,A)=50 V(R1,B)=100 T(R2) = 2000 V(R2,B)=200 V(R2,C)=300 T(R3) = 3000 V(R3,C)=90 V(R3,D)=500

R1 R2 R3

CS 245 88

slide-89
SLIDE 89

T(U) = 1000´2000 V(U,A) = 50 200 V(U,B) = 100 V(U,C) = 300

Partial Result: U = R1 ⨝ R2

CS 245 89

slide-90
SLIDE 90

End Result: Z = U ⨝ R3

T(Z) = 1000´2000´3000 V(Z,A) = 50 200´300 V(Z,B) = 100 V(Z,C) = 90 V(Z,D) = 500

CS 245 90

slide-91
SLIDE 91

Another Statistic: Histograms

CS 245 91

10 20 30 40 5 10 15 12

number of tuples in R with A value in a given range

σA=a(R) = ?

Requires some care to set bucket boundaries

σA≥a(R) = ?

slide-92
SLIDE 92

Outline

What can we optimize? Rule-based optimization Data statistics Cost models Cost-based plan selection

CS 245 92