Chapter 13 Query Optimization Chapter 13 Query Optimization

  • Slides: 56
Download presentation
Chapter 13: Query Optimization

Chapter 13: Query Optimization

Chapter 13: Query Optimization n Introduction n Transformation of Relational Expressions n Catalog Information

Chapter 13: Query Optimization n Introduction n Transformation of Relational Expressions n Catalog Information for Cost Estimation n Statistical Information for Cost Estimation n Cost-based optimization n Dynamic Programming for Choosing Evaluation Plans n Materialized views

Introduction n Alternative ways of evaluating a given query l Equivalent expressions l Different

Introduction n Alternative ways of evaluating a given query l Equivalent expressions l Different algorithms for each operation

Introduction (Cont. ) n An evaluation plan defines exactly what algorithm is used for

Introduction (Cont. ) n An evaluation plan defines exactly what algorithm is used for each operation, and how the execution of the operations is coordinated. n Find out how to view query execution plans on your favorite database

Introduction (Cont. ) n Cost difference between evaluation plans for a query can be

Introduction (Cont. ) n Cost difference between evaluation plans for a query can be enormous l E. g. seconds vs. days in some cases n Steps in cost-based query optimization 1. Generate logically equivalent expressions using equivalence rules 2. Annotate resultant expressions to get alternative query plans 3. Choose the cheapest plan based on estimated cost n Estimation of plan cost based on: l Statistical information about relations. Examples: 4 number of tuples, number of distinct values for an attribute l Statistics estimation for intermediate results 4 to compute cost of complex expressions l Cost formulae for algorithms, computed using statistics

Generating Equivalent Expressions

Generating Equivalent Expressions

Transformation of Relational Expressions n Two relational algebra expressions are said to be equivalent

Transformation of Relational Expressions n Two relational algebra expressions are said to be equivalent if the two expressions generate the same set of tuples on every legal database instance l Note: order of tuples is irrelevant l we don’t care if they generate different results on databases that violate integrity constraints n In SQL, inputs and outputs are multisets of tuples l Two expressions in the multiset version of the relational algebra are said to be equivalent if the two expressions generate the same multiset of tuples on every legal database instance. n An equivalence rule says that expressions of two forms are equivalent l Can replace expression of first form by second, or vice versa

Equivalence Rules 1. Conjunctive selection operations can be deconstructed into a sequence of individual

Equivalence Rules 1. Conjunctive selection operations can be deconstructed into a sequence of individual selections. 2. Selection operations are commutative. 3. Only the last in a sequence of projection operations is needed, the others can be omitted. 4. Selections can be combined with Cartesian products and theta joins. a. (E 1 X E 2) = E 1 b. 1(E 1 2 E 2) = E 1 1 2 E 2

Equivalence Rules (Cont. ) 5. Theta-join operations (and natural joins) are commutative. E 1

Equivalence Rules (Cont. ) 5. Theta-join operations (and natural joins) are commutative. E 1 E 2 = E 2 E 1 6. (a) Natural join operations are associative: (E 1 E 2 ) E 3 = E 1 (E 2 E 3) (b) Theta joins are associative in the following manner: (E 1 1 E 2) 2 3 E 3 = E 1 1 3 (E 2 2 E 3) where 2 involves attributes from only E 2 and E 3.

Pictorial Depiction of Equivalence Rules

Pictorial Depiction of Equivalence Rules

Equivalence Rules (Cont. ) 7. The selection operation distributes over theta join operation under

Equivalence Rules (Cont. ) 7. The selection operation distributes over theta join operation under the following two conditions: (a) When all the attributes in 0 involve only the attributes of one of the expressions (E 1) being joined. 0 E 1 E 2) = ( 0(E 1)) E 2 (b) When 1 involves only the attributes of E 1 and 2 involves only the attributes of E 2. 1 E 2) = ( 1(E 1)) ( (E 2))

Equivalence Rules (Cont. ) 8. The projection operation distributes over theta join operation as

Equivalence Rules (Cont. ) 8. The projection operation distributes over theta join operation as follows: (a) if involves only attributes from L 1 L 2: L 1 L 2 ( E 1 E 2 ) (b) Consider a join E 1 = ( L 1 ( E 1 )) ( L 2 ( E 2 )) E 2. l Let L 1 and L 2 be sets of attributes from E 1 and E 2, respectively. l Let L 3 be attributes of E 1 that are involved in join condition , but are not in L 1 L 2, and l let L 4 be attributes of E 2 that are involved in join condition , but are not in L 1 L 2. L L ( E 1 1 2 E 2 ) = L L (( L L ( E 1 )) 1 2 1 3 ( L L ( E 2 ))) 2 4

Equivalence Rules (Cont. ) 9. The set operations union and intersection are commutative E

Equivalence Rules (Cont. ) 9. The set operations union and intersection are commutative E 1 E 2 = E 2 E 1 n (set difference is not commutative). 10. Set union and intersection are associative. (E 1 E 2) E 3 = E 1 (E 2 E 3) 11. The selection operation distributes over , and –. (E 1 – E 2) = (E 1) – (E 2) and similarly for and in place of – Also: (E 1 – E 2) = (E 1) – E 2 and similarly for in place of –, but not for 12. The projection operation distributes over union L(E 1 E 2) = ( L(E 1)) ( L(E 2))

Transformation Example: Pushing Selections n Query Example: Find the names of all instructors in

Transformation Example: Pushing Selections n Query Example: Find the names of all instructors in the Music department, along with the titles of the courses that they teach l name, title( dept_name= “Music” (instructor (teaches course_id, title (course)))) n Transformation using rule 7 a. l name, title(( dept_name= “Music”(instructor)) (teaches course_id, title (course))) n Performing the selection as early as possible reduces the size of the relation to be joined.

Example with Multiple Transformations n Query Example: Find the names of all instructors in

Example with Multiple Transformations n Query Example: Find the names of all instructors in the Music department who have taught a course in 2009, along with the titles of the courses that they taught l name, title( dept_name= “Music” year = 2009 (instructor (teaches course_id, title (course)))) n Transformation using join associatively (Rule 6 a): l name, title( dept_name= “Music” gear = 2009 ((instructor teaches) course_id, title (course))) n Second form provides an opportunity to apply the “perform selections early” rule, resulting in the subexpression dept_name = “Music” (instructor) year = 2009 (teaches)

Multiple Transformations (Cont. )

Multiple Transformations (Cont. )

Transformation Example: Pushing Projections n Consider: name, title( dept_name= “Music” (instructor) teaches) course_id, title

Transformation Example: Pushing Projections n Consider: name, title( dept_name= “Music” (instructor) teaches) course_id, title (course)))) n When we compute dept_name = “Music” (instructor teaches) we obtain a relation whose schema is: (ID, name, dept_name, salary, course_id, sec_id, semester, year) n Push projections using equivalence rules 8 a and 8 b; eliminate unneeded attributes from intermediate results to get: name, title( name, course_id ( dept_name= “Music” (instructor) teaches)) course_id, title (course)))) n Performing the projection as early as possible reduces the size of the relation to be joined.

Join Ordering Example n For all relations r 1, r 2, and r 3,

Join Ordering Example n For all relations r 1, r 2, and r 3, (r 1 r 2 ) r 3 = r 1 (r 2 r 3 ) (Join Associativity) n If r 2 r 3 is quite large and r 1 (r 1 r 2 ) r 2 is small, we choose r 3 so that we compute and store a smaller temporary relation.

Join Ordering Example (Cont. ) n Consider the expression name, title( dept_name= “Music” (instructor)

Join Ordering Example (Cont. ) n Consider the expression name, title( dept_name= “Music” (instructor) teaches) course_id, title (course)))) n Could compute teaches course_id, title (course) first, and join result with dept_name= “Music” (instructor) but the result of the first join is likely to be a large relation. n Only a small fraction of the university’s instructors are likely to be from the Music department l it is better to compute dept_name= “Music” (instructor) first. teaches

Enumeration of Equivalent Expressions n Query optimizers use equivalence rules to systematically generate expressions

Enumeration of Equivalent Expressions n Query optimizers use equivalence rules to systematically generate expressions equivalent to the given expression n Can generate all equivalent expressions as follows: l Repeat 4 apply all applicable equivalence rules on every subexpression of every equivalent expression found so far 4 add newly generated expressions to the set of equivalent expressions Until no new equivalent expressions are generated above n The above approach is very expensive in space and time l Two approaches 4 Optimized 4 Special plan generation based on transformation rules case approach for queries with only selections, projections and joins

Implementing Transformation Based Optimization n Space requirements reduced by sharing common sub-expressions: l when

Implementing Transformation Based Optimization n Space requirements reduced by sharing common sub-expressions: l when E 1 is generated from E 2 by an equivalence rule, usually only the top level of the two are different, subtrees below are the same and can be shared using pointers 4 E. g. when applying join commutativity E 1 l E 2 Same sub-expression may get generated multiple times 4 Detect duplicate sub-expressions and share one copy n Time requirements are reduced by not generating all expressions l Dynamic programming 4 We will study only the special case of dynamic programming for join order optimization

Cost Estimation n Cost of each operator computed as described in previous lecture l

Cost Estimation n Cost of each operator computed as described in previous lecture l Need statistics of input relations 4 E. g. number of tuples, sizes of tuples n Inputs can be results of sub-expressions l Need to estimate statistics of expression results l To do so, we require additional statistics 4 E. g. number of distinct values for an attribute n More on cost estimation later

Choice of Evaluation Plans n Must consider the interaction of evaluation techniques when choosing

Choice of Evaluation Plans n Must consider the interaction of evaluation techniques when choosing evaluation plans l choosing the cheapest algorithm for each operation independently may not yield best overall algorithm. E. g. 4 merge-join may be costlier than hash-join, but may provide a sorted output which reduces the cost for an outer level aggregation. 4 nested-loop join may provide opportunity for pipelining n Practical query optimizers incorporate elements of the following two broad approaches: 1. Search all the plans and choose the best plan in a cost-based fashion.

Cost-Based Optimization n Consider finding the best join-order for r 1 r 2 .

Cost-Based Optimization n Consider finding the best join-order for r 1 r 2 . . . rn. n There are (2(n – 1))!/(n – 1)! different join orders for above expression. With n = 7, the number is 665280, with n = 10, the number is greater than 176 billion! n No need to generate all the join orders. Using dynamic programming, the least-cost join order for any subset of {r 1, r 2, . . . rn} is computed only once and stored for future use. n As the number of joins increases, the number of alternative plans grows rapidly; we need to restrict the search space. n Left-deep trees allow us to generate all fully pipelined plans. l Intermediate results not written to temporary files. l Not all left-deep trees are fully pipelined (e. g. , Sort-Merge join).

Left Deep Join Trees n In left-deep join trees, the right-hand-side input for each

Left Deep Join Trees n In left-deep join trees, the right-hand-side input for each join is a relation, not the result of an intermediate join.

Dynamic Programming in Optimization n To find best join tree for a set of

Dynamic Programming in Optimization n To find best join tree for a set of n relations: l To find best plan for a set S of n relations, consider all possible plans of the form: S 1 (S – S 1) where S 1 is any non-empty subset of S. l Recursively compute costs for joining subsets of S to find the cost of each plan. Choose the cheapest of the 2 n – 2 alternatives. l Base case for recursion: single relation access plan 4 Apply l all selections on Ri using best choice of indices on Ri When plan for any subset is computed, store it and reuse it when it is required again, instead of recomputing it 4 Dynamic programming

Interesting Sort Orders n Consider the expression (r 1 r 2 ) r 3

Interesting Sort Orders n Consider the expression (r 1 r 2 ) r 3 (with A as common attribute) n An interesting sort order is a particular sort order of tuples that could be useful for a later operation l Using merge-join to compute r 1 r 2 may be costlier than hash join but generates result sorted on A l Which in turn may make merge-join with r 3 cheaper, which may reduce cost of join with r 3 and minimizing overall cost l Sort order may also be useful for order by and for grouping n Not sufficient to find the best join order for each subset of the set of n given relations l must find the best join order for each subset, for each interesting sort order l Simple extension of the dynamic programming algorithm l Usually, number of interesting orders is quite small and does not affect time/space complexity significantly

Join Order Optimization Algorithm procedure findbestplan(S) if (bestplan[S]. cost ) return bestplan[S] // else

Join Order Optimization Algorithm procedure findbestplan(S) if (bestplan[S]. cost ) return bestplan[S] // else bestplan[S] has not been computed earlier, compute it now if (S contains only 1 relation) set bestplan[S]. plan and bestplan[S]. cost based on the best way of accessing S /* Using selections on S and indices on S */ else for each non-empty subset S 1 of S such that S 1 S P 1= findbestplan(S 1) P 2= findbestplan(S - S 1) A = best algorithm for joining results of P 1 and P 2 cost = P 1. cost + P 2. cost + cost of A if cost < bestplan[S]. cost = cost bestplan[S]. plan = “execute P 1. plan; execute P 2. plan; join results of P 1 and P 2 using A” return bestplan[S] * Some modifications to allow indexed nested loops joins on relations that have selections (see book)

Cost of Optimization n With dynamic programming time complexity of optimization with bushy n

Cost of Optimization n With dynamic programming time complexity of optimization with bushy n n trees is O(3 n). l With n = 10, this number is 59000 instead of 176 billion! Space complexity is O(2 n) To find best left-deep join tree for a set of n relations: l Consider n alternatives with one relation as right-hand side input and the other relations as left-hand side input. l Modify optimization algorithm: 4 Replace “for each non-empty subset S 1 of S such that S 1 S” 4 By: for each relation r in S let S 1 = S – r. If only left-deep trees are considered, time complexity of finding best join order is O(n 2 n) l Space complexity remains at O(2 n) Cost-based optimization is expensive, but worthwhile for queries on large datasets (typical queries have small n, generally < 10)

Cost Based Optimization with Equivalence Rules n Physical equivalence rules allow logical query plan

Cost Based Optimization with Equivalence Rules n Physical equivalence rules allow logical query plan to be converted to physical query plan specifying what algorithms are used for each operation. n Efficient optimizer based on equivalent rules depends on l A space efficient representation of expressions which avoids making multiple copies of subexpressions l Efficient techniques for detecting duplicate derivations of expressions l A form of dynamic programming based on memoization, which stores the best plan for a subexpression the first time it is optimized, and reuses in on repeated optimization calls on same subexpression l Cost-based pruning techniques that avoid generating all plans n Pioneered by the Volcano project and implemented in the SQL Server optimizer

Heuristic Optimization n Cost-based optimization is expensive, even with dynamic programming. n Systems may

Heuristic Optimization n Cost-based optimization is expensive, even with dynamic programming. n Systems may use heuristics to reduce the number of choices that must be made in a cost-based fashion. n Heuristic optimization transforms the query-tree by using a set of rules that typically (but not in all cases) improve execution performance: l Perform selection early (reduces the number of tuples) l Perform projection early (reduces the number of attributes) l Perform most restrictive selection and join operations (i. e. with smallest result size) before other similar operations. l Some systems use only heuristics, others combine heuristics with partial cost-based optimization.

Structure of Query Optimizers n Many optimizers considers only left-deep join orders. l Plus

Structure of Query Optimizers n Many optimizers considers only left-deep join orders. l Plus heuristics to push selections and projections down the query tree l Reduces optimization complexity and generates plans amenable to pipelined evaluation. n Heuristic optimization used in some versions of Oracle: l Repeatedly pick “best” relation to join next 4 Starting from each of n starting points. Pick best among these n Intricacies of SQL complicate query optimization l E. g. nested subqueries

Structure of Query Optimizers (Cont. ) n Some query optimizers integrate heuristic selection and

Structure of Query Optimizers (Cont. ) n Some query optimizers integrate heuristic selection and the generation of alternative access plans. l Frequently used approach 4 heuristic rewriting of nested block structure and aggregation 4 followed by cost-based join-order optimization for each block l Some optimizers (e. g. SQL Server) apply transformations to entire query and do not depend on block structure l Optimization cost budget to stop optimization early (if cost of plan is less than cost of optimization) l Plan caching to reuse previously computed plan if query is resubmitted 4 Even with different constants in query n Even with the use of heuristics, cost-based query optimization imposes a substantial overhead. l But is worth it for expensive queries l Optimizers often use simple heuristics for very cheap queries, and perform exhaustive enumeration for more expensive queries

Statistics for Cost Estimation

Statistics for Cost Estimation

Statistical Information for Cost Estimation n nr: number of tuples in a relation r.

Statistical Information for Cost Estimation n nr: number of tuples in a relation r. n br: number of blocks containing tuples of r. n lr: size of a tuple of r. n fr: blocking factor of r — i. e. , the number of tuples of r that fit into one block. n V(A, r): number of distinct values that appear in r for attribute A; same as the size of A(r). n If tuples of r are stored together physically in a file, then:

Histograms n Histogram on attribute age of relation person n Equi-width histograms n Equi-depth

Histograms n Histogram on attribute age of relation person n Equi-width histograms n Equi-depth histograms

Selection Size Estimation n A=v(r) 4 nr / V(A, r) : number of records

Selection Size Estimation n A=v(r) 4 nr / V(A, r) : number of records that will satisfy the selection 4 Equality condition on a key attribute: size estimate = 1 n A V(r) (case of A V(r) is symmetric) l Let c denote the estimated number of tuples satisfying the condition. l If min(A, r) and max(A, r) are available in catalog l l 4 c = 0 if v < min(A, r) 4 c = If histograms available, can refine above estimate In absence of statistical information c is assumed to be nr / 2.

Size Estimation of Complex Selections n The selectivity of a condition i is the

Size Estimation of Complex Selections n The selectivity of a condition i is the probability that a tuple in the relation r satisfies i. l If si is the number of satisfying tuples in r, the selectivity of i is given by si /nr. n Conjunction: 1 2. . . n (r). Assuming indepdence, estimate of tuples in the result is: n Disjunction: 1 2 . . . n (r). Estimated number of tuples: n Negation: (r). Estimated number of tuples: nr – size( (r))

Join Operation: Running Example Running example: student takes Catalog information for join examples: n

Join Operation: Running Example Running example: student takes Catalog information for join examples: n nstudent = 5, 000. n fstudent = 50, which implies that bstudent =5000/50 = 100. n ntakes = 10000. n ftakes = 25, which implies that btakes = 10000/25 = 400. n V(ID, takes) = 2500, which implies that on average, each student who has taken a course has taken 4 courses. l Attribute ID in takes is a foreign key referencing student. l V(ID, student) = 5000 (primary key!)

Estimation of the Size of Joins n The Cartesian product r x s contains

Estimation of the Size of Joins n The Cartesian product r x s contains nr ns tuples; each tuple occupies sr + ss bytes. n If R S = , then r s is . n If R S is a key for R, then a tuple of s will join with at most one tuple from r l therefore, the number of tuples in r number of tuples in s. s is no greater than the n If R S in S is a foreign key in S referencing R, then the number of tuples in r s is exactly the same as the number of tuples in s. case for R S being a foreign key referencing S is symmetric. 4 The n In the example query student takes, ID in takes is a foreign key referencing student l hence, the result has exactly ntakes tuples, which is 10000

Estimation of the Size of Joins (Cont. ) n If R S = {A}

Estimation of the Size of Joins (Cont. ) n If R S = {A} is not a key for R or S. If we assume that every tuple t in R produces tuples in R number of tuples in R S is estimated to be: S, the If the reverse is true, the estimate obtained will be: The lower of these two estimates is probably the more accurate one. n Can improve on above if histograms are available l Use formula similar to above, for each cell of histograms on the two relations

Estimation of the Size of Joins (Cont. ) n Compute the size estimates for

Estimation of the Size of Joins (Cont. ) n Compute the size estimates for depositor customer without using information about foreign keys: l V(ID, takes) = 2500, and V(ID, student) = 5000 l The two estimates are 5000 * 10000/2500 = 20, 000 and 5000 * 10000/5000 = 10000 l We choose the lower estimate, which in this case, is the same as our earlier computation using foreign keys.

Size Estimation for Other Operations n Projection: estimated size of A(r) = V(A, r)

Size Estimation for Other Operations n Projection: estimated size of A(r) = V(A, r) n Set operations l For unions/intersections of selections on the same relation: rewrite and use size estimate for selections 4 E. g. l 1 (r) 2 (r) can be rewritten as 1 ˅ 2 (r) For operations on different relations: 4 estimated size of r s = size of r + size of s. 4 estimated size of r s = minimum size of r and size of s. 4 estimated size of r – s = r. 4 All the three estimates may be quite inaccurate, but provide upper bounds on the sizes.

Size Estimation (Cont. ) n Outer join: l Estimated size of r 4 Case

Size Estimation (Cont. ) n Outer join: l Estimated size of r 4 Case l s = size of r s + size of right outer join is symmetric Estimated size of r s = size of r s + size of r + size of s

Estimation of Number of Distinct Values Selections: (r) n If forces A to take

Estimation of Number of Distinct Values Selections: (r) n If forces A to take a specified value: V(A, (r)) = 1. 4 e. g. , A=3 n If forces A to take on one of a specified set of values: V(A, (r)) = number of specified values. 4 (e. g. , (A = 1 V A = 3 V A = 4 )), n If the selection condition is of the form A op r estimated V(A, (r)) = V(A. r) * s 4 where s is the selectivity of the selection. n In all the other cases: use approximate estimate of min(V(A, r), n (r) ) l More accurate estimate can be got using probability theory, but this one works fine generally

Estimation of Distinct Values (Cont. ) Joins: r s n If all attributes in

Estimation of Distinct Values (Cont. ) Joins: r s n If all attributes in A are from r estimated V(A, r s) = min (V(A, r), n r s) n If A contains attributes A 1 from r and A 2 from s, then estimated V(A, r s) = min(V(A 1, r)*V(A 2 – A 1, s), V(A 1 – A 2, r)*V(A 2, s), nr l s) More accurate estimate can be got using probability theory, but this one works fine generally

Estimation of Distinct Values (Cont. ) n Estimation of distinct values are straightforward for

Estimation of Distinct Values (Cont. ) n Estimation of distinct values are straightforward for projections. l They are the same in A (r) as in r. n The same holds for grouping attributes of aggregation. n For aggregated values l For min(A) and max(A), the number of distinct values can be estimated as min(V(A, r), V(G, r)) where G denotes grouping attributes l For other aggregates, assume all values are distinct, and use V(G, r)

Multiquery Optimization n Example Q 1: select * from (r natural join t) natural

Multiquery Optimization n Example Q 1: select * from (r natural join t) natural join s Q 2: select * from (r natural join u) natural join s l Both queries share common subexpression (r natural join s) l May be useful to compute (r natural join s) once and use it in both queries 4 But this may be more expensive in some situations – e. g. (r natural join s) may be expensive, plans as shown in queries may be cheaper n Multiquery optimization: find best overall plan for a set of queries, expoiting sharing of common subexpressions between queries where it is useful

Multiquery Optimization (Cont. ) n Simple heuristic used in some database systems: l optimize

Multiquery Optimization (Cont. ) n Simple heuristic used in some database systems: l optimize each query separately l detect and exploiting common subexpressions in the individual optimal query plans 4 May l not always give best plan, but is cheap to implement Shared scans: widely used special case of multiquery optimization n Set of materialized views may share common subexpressions l As a result, view maintenance plans may share subexpressions l Multiquery optimization can be useful in such situations

Motivating Example SELECT S. sname FROM Reserves R, Sailors S WHERE R. sid=S. sid

Motivating Example SELECT S. sname FROM Reserves R, Sailors S WHERE R. sid=S. sid AND R. bid=100 AND S. rating>5 • Cost: 500+500*1000 I/Os (pipeline) sname Plan: – Not too bad! • Misses several opportunities: selections could have been `pushed’ earlier and no bid=100 rating > 5 (pipeline) use of indexes. • Goal of optimization: Find more efficient plans that compute the same answer. (Block Nested loop) sid=sid Sailors Reserves

Schema for Examples Sailors (sid: integer, sname: string, rating: integer, age: real) Reserves (sid:

Schema for Examples Sailors (sid: integer, sname: string, rating: integer, age: real) Reserves (sid: integer, bid: integer, day: dates, rname: string) • Reserves: – Each tuple is 40 bytes long, 100 tuples per page, 1000 pages. – Assume there are 100 boats • Sailors: – Each tuple is 50 bytes long, 80 tuples per page, 500 pages. – Assume there are 10 different ratings • Assume there are 5 pages in the buffer pool!

Alternative Plans – Push Selects (pipeline) sname (pipeline) bid=100 rating > 5 (pipeline) (Block

Alternative Plans – Push Selects (pipeline) sname (pipeline) bid=100 rating > 5 (pipeline) (Block Nested Loop) sid=sid (Block Nested loop) sid=sid Sailors rating > 5 Reserves 500, 500 IOs (pipeline) Reserves Sailors 250, 500 IOs

Alternative Plans – Push Selects (pipeline) sname bid=100 (pipeline) (Block Nested loop) sid=sid rating

Alternative Plans – Push Selects (pipeline) sname bid=100 (pipeline) (Block Nested loop) sid=sid rating > 5 (pipeline) bid = 100 rating > 5 Reserves (pipeline) Sailors (pipeline) Reserves Sailors 250, 500 IOs

Alternative Plans – Push Selects (pipeline) sname bid=100 rating > 5 (pipeline) sid=sid (pipeline)

Alternative Plans – Push Selects (pipeline) sname bid=100 rating > 5 (pipeline) sid=sid (pipeline) Sailors 250, 500 IOs Reserves (pipeline) (Block Nested loop) rating > 5 (pipeline) sname bid=100 (pipeline) Sailors Reserves 6000 IOs

Alternative Plans – Push Selects (pipeline) sname rating > 5 (pipeline) (Block Nested loop)

Alternative Plans – Push Selects (pipeline) sname rating > 5 (pipeline) (Block Nested loop) sid=sid bid=100 (pipeline) Sailors (pipeline) Reserves 6000 IOs rating > 5 (Scan & Write to temp T 2) Sailors 4250 IOs 1000 + 500+ 250 + (10 * 250)

(pipeline) sname More Alternative Plans sid=sid • Main difference: Sort Merge Join (Scan; write

(pipeline) sname More Alternative Plans sid=sid • Main difference: Sort Merge Join (Scan; write to bid=100 temp T 1) Reserves (Sort-Merge Join) rating > 5 (Scan; write to temp T 2) Sailors • With 5 buffers, cost of plan: – Scan Reserves (1000) + write temp T 1 (10 pages, if we have 100 boats, uniform distribution) = 1010. – Scan Sailors (500) + write temp T 2 (250 pages, if have 10 ratings) = 750. – Sort T 1 (2*2*10) + sort T 2 (2*4*250) + merge (10+250) = 2300 Total: 4060 page I/Os. • If use BNL join, join = 10+4*250, total cost = 2770. • Can also `push’ projections, but must be careful! – T 1 has only sid, T 2 only sid, sname: – T 1 fits in 3 pgs, cost of BNL under 250 pgs, total < 2000. –