CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 183 Database Systems II Query Compiler.

Slides:



Advertisements
Similar presentations
16.4 Estimating the Cost of Operations Project GuidePrepared By Dr. T. Y. LinVinayan Verenkar Computer Science Dept San Jose State University.
Advertisements

CS4432: Database Systems II
CS CS4432: Database Systems II Logical Plan Rewriting.
Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
Query Compiler. The Query Compiler Parses SQL query into parse tree Transforms parse tree into expression tree (logical query plan) Transforms logical.
Query Execution Optimizing Performance. Resolving an SQL query Since our SQL queries are very high level, the query processor must do a lot of additional.
COMP 451/651 Optimizing Performance
The Query Compiler Parses SQL query into parse tree Transforms parse tree into expression tree (logical query plan) Transforms logical query plan into.
Algebraic Laws For the binary operators, we push the selection only if all attributes in the condition C are in R.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
CS 4432query processing - lecture 141 CS4432: Database Systems II Lecture #14 Query Processing – Size Estimation Professor Elke A. Rundensteiner.
Estimating the Cost of Operations We don’t want to execute the query in order to learn the costs. So, we need to estimate the costs. How can we estimate.
CS 245Notes 71 CS 245: Database System Principles Notes 7: Query Optimization Hector Garcia-Molina.
CS CS4432: Database Systems II Query Processing – Size Estimation.
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
16.4 Estimating the Cost of Operations Project GuidePrepared By Dr. T. Y. LinAkshay Shenoy Computer Science Dept San Jose State University.
Estimating the Cost of Operations. From l.q.p. to p.q.p Having parsed a query and transformed it into a logical query plan, we must turn the logical plan.
Cost based transformations Initial logical query plan Two candidates for the best logical query plan.
CS Spring 2002Notes 61 CS 277: Database System Implementation Notes 6: Query Processing Arthur Keller.
CS 4432query processing - lecture 131 CS4432: Database Systems II Lecture #13 Query Processing Professor Elke A. Rundensteiner.
1 Anna Östlin Pagh and Rasmus Pagh IT University of Copenhagen Advanced Database Technology March 25, 2004 QUERY COMPILATION II Lecture based on [GUW,
CS 4432query processing1 CS4432: Database Systems II.
CS 4432logical query rewriting - lecture 151 CS4432: Database Systems II Lecture #15 Logical Query Rewriting Professor Elke A. Rundensteiner.
The Query Compiler 16.1 Parsing and Preprocessing Meghna Jain(205) Dr. T. Y. Lin.
16.5 Introduction to Cost- based plan selection Amith KC Student Id: 109.
16.2 ALGEBRAIC LAWS FOR IMPROVING QUERY PLANS Ramya Karri ID: 206.
Query Processing & Optimization
Algebraic Laws. {P1,P2,…..} {P1,C1>...} parse convert apply laws estimate result sizes consider physical plans estimate costs pick best execute Pi answer.
CS 4432query processing - lecture 121 CS4432: Database Systems II Lecture #12 Query Processing Professor Elke A. Rundensteiner.
CPS216: Advanced Database Systems Notes 03:Query Processing (Overview, contd.) Shivnath Babu.
CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:
CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 242 Database Systems II Query Execution.
CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:
CSCE Database Systems Chapter 15: Query Execution 1.
Advanced Database Systems Notes:Query Processing (Overview) Shivnath Babu.
Cost based transformations Initial logical query plan Two candidates for the best logical query plan.
DBMS 2001Notes 6: Query Compilation1 Principles of Database Management Systems 6: Query Compilation and Optimization Pekka Kilpeläinen (partially based.
CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites) Shivnath Babu.
CS 245Notes 61 CS 245: Database System Principles Notes 6: Query Processing Hector Garcia-Molina.
CS 245Notes 61 CS 245: Database System Principles Notes 6: Query Processing Hector Garcia-Molina.
SCUHolliday - COEN 17814–1 Schedule Today: u Query Processing overview.
CPS216: Data-Intensive Computing Systems Introduction to Query Processing Shivnath Babu.
CS 4432query processing1 CS4432: Database Systems II Lecture #11 Professor Elke A. Rundensteiner.
Query Compiler A.Sri Harsha From Parse Trees to Logical Query Plans.
Chapters 15-16a1 (Slides by Hector Garcia-Molina, Chapters 15 and 16: Query Processing.
Estimating the Cost of Operations. Suppose we have parsed a query and transformed it into a logical query plan (lqp) Also suppose all possible transformations.
CS 4432estimation - lecture 161 CS4432: Database Systems II Lecture #16 Query Processing : Estimating Sizes of Results Professor Elke A. Rundensteiner.
CS4432: Database Systems II Query Processing- Part 2.
CS 245Notes 61 CS 245: Database System Principles Notes 6: Query Processing Hector Garcia-Molina.
Data Engineering SQL Query Processing Shivnath Babu.
Chapter 13 Query Optimization Yonsei University 1 st Semester, 2015 Sanghyun Park.
Chapter 13: Query Processing
CS4432: Database Systems II Query Processing- Part 1 1.
CPS216: Advanced Database Systems Notes 02:Query Processing (Overview) Shivnath Babu.
1 Ullman et al. : Database System Principles Notes 6: Query Processing.
16.4 Estimating the Cost of Operations Project GuidePrepared By Dr. T. Y. LinAkshay Shenoy Computer Science Dept San Jose State University.
1/14/2005Yan Huang - CSCI5330 Database Implementation – Query Optimization Query Optimization.
Advanced Database Systems: DBS CB, 2nd Edition
CS 245: Database System Principles
Chapter 15 QUERY EXECUTION.
Focus: Relational System
CS 245: Database System Principles
Outline - Query Processing
Algebraic Laws.
CPSC-608 Database Systems
Yan Huang - CSCI5330 Database Implementation – Query Processing
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Outline - Query Processing
CPSC-608 Database Systems
Presentation transcript:

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 183 Database Systems II Query Compiler

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 184 Introduction The Query Compiler translates an SQL query into a physical query plan, which can be executed, in three steps: The query is parsed and represented as a parse tree. The parse tree is converted into a relational algebra expression tree ( logical query plan ). The logical query plan is refined into a physical query plan, which also specifies the algorithms used in each step and the way in which data is obtained.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 185 Introduction parse convert query rewrite estimate result sizes consider physical plans estimate costs pick best execute {P1,P2,…..} {(P1,C1),(P2,C2)...} Pi answer SQL query parse tree logical query plan “improved” l.q.p l.q.p. +sizes

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 186 Introduction Example SELECT B,D FROM R,S WHERE R.A = “c”  S.E = 2  R.C=S.C Conceptual evaluation strategy: Perform cartesian product, Apply selection, and Project to specified attributes. Use as starting point for optimization.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 187 Introduction Example  B,D  R.A =“c”  S.E=2  R.C=S.C  X RS  B,D [  R.A=“c”  S.E=2  R.C = S.C (RXS)]

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 188 Introduction Example  B,D  R.A = “c”  S.E = 2 R S natural join This logical query plan is equivalent. It is more efficient, since it reduces the sizes of the intermediate tables.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 189 Introduction Example Needs to be refined into physical query plan. E.g., use R.A and S.C indexes as follows: (1) Use R.A index to select R tuples with R.A = “c” (2) For each R.C value found, use S.Cindex to find matching tuples (3) Eliminate S tuples S.E  2 (4) Join matching R,S tuples, project B,D attributes and place in result

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 190 Parsing Parse Trees Nodes correspond to either atoms (terminal symbols) or syntactic categories (non-terminal symbols). An atom is a lexical element such as a keyord, name of an attribute or relation, constant, operator, parenthesis. A syntactic category denotes a family of query subparts that all play the same role within a query, e.g. Condition. Syntactic categories are enclosed in triangular brackets, e.g..

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 191 Parsing Example SELECT title FROM StarsIn WHERE starName IN (SELECT name FROM MovieStar WHERE birthdate LIKE ‘%1960’);

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 192 Parsing SELECT FROM WHERE IN title StarsIn ( ) starName SELECT FROM WHERE LIKE name MovieStar birthDate ‘%1960’

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 193 Parsing Grammar for SQL The following grammar describes a simple subset of SQL. Queries ::= SELECT FROM WHERE ; Selection lists ::=, ::= From lists ::=, ::=

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 194 Parsing Grammar for SQL Conditions ::= AND ::= IN ( ) ::= = ::= LIKE Syntactic categories Relation and Attribute are not defined by grammar rules, but by the database schema. Syntactic category Pattern defined as some regular expression.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 195 Conversion to Query Plan How to convert a parse tree into a logical query plan, i.e. a relational algebra expression? Queries with conditions without subqueries are easy: Form Cartesian product of all relations in. Apply a selection  c where C is given by. Finally apply a projection  L where L is the list of attributes in. Queries involving subqueries are more difficult. Remove subqueries from conditions and represent them by a two-argument selection in the logical query plan. See the textbook for details.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 196 Algebraic Laws for Query Plans Introduction Algebraic laws allow us to transform a Relational Algebra (RA) expression into an equivalent one. Two RA expressions are equivalent if, for all database instances, they produce the same answer. The resulting expression may have a more efficient physical query plan. Algebraic laws are used in the query rewrite phase.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 197 Algebraic Laws for Query Plans Introduction Commutative law : Order of arguments does not matter. x + y = y + x Associative law : May group two uses of the operator either from the left or the right. (x + y) + z = x + (y + z) Operators that are commutative and associative can be grouped and ordered arbitrarily.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 198 Algebraic Laws for Query Plans Natural Join, Cartesian Product and Union 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 R S=SR (R S) T= R (S T)

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 199 Algebraic Laws for Query Plans Natural Join, Cartesian Product and Union R S=SR To prove this law, need to show that any tuple resulting from the left side expression is also produced by the right side expression, and vice versa. Suppose tuple t is in R S. There must be tuples r in R and s in S that agree with t on all shared attributes. If we evaluate S R, tuples s and r will again result in t.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 200 Algebraic Laws for Query Plans Natural Join, Cartesian Product and Union R S=SR Note that the order of attributes within a tuple does not matter (carry attribute names along). Relation as bag of tuples According to the same reasoning, the number of copies of t must be identical on both sides. The other direction of the proof is essentially the same, given the symmetry of S and R.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 201 Algebraic Laws for Query Plans Selection  p1  p2 (R) =  p1vp2 (R) =  p1 [  p2 (R)] [  p1 (R)] U [  p2 (R)]  p1 [  p2 (R)] =  p2 [  p1 (R)] Simple conditions p1 or p2 may be pushed down further than the complex condition.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 202 Algebraic Laws for Query Plans Bag Union What about the union of relations with duplicates (bags)? R = {a,a,b,b,b,c} S = {b,b,c,c,d} R U S = ? Number of occurrences either SUM or MAX of occurrences in the imput relations. SUM: R U S = {a,a,b,b,b,b,b,c,c,c,d} MAX: R U S = {a,a,b,b,b,c,c,d}

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 203 Algebraic Laws for Query Plans Selection  p1vp2 (R) =  p1 (R) U  p2 (R) MAX implementation of union makes rule work. R={a,a,b,b,b,c} p1 satisfied by a,b, p2 satisfied by b,c  p1vp2 (R) = {a,a,b,b,b,c}  p1 (R) = {a,a,b,b,b}  p2 (R) = {b,b,b,c}  p1 (R) U  p2 (R) = {a,a,b,b,b,c}

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 204 Algebraic Laws for Query Plans Selection  p1vp2 (R) =  p1 (R) U  p2 (R) SUM implementation of union makes more sense. Senators (……)Reps (……) T1 =  yr,state Senators, T2 =  yr,state Reps T1 Yr State T2 Yr State 97 CA 99 CA 99 CA 99 CA 98 AZ 98 CA Use SUM implementation, but then some laws do not hold. Union?

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 205 Algebraic Laws for Query Plans Selection and Set Operations  p (R U S) =  p (R) U  p (S)  p (R - S) =  p (R) - S =  p (R) -  p (S)

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 206 Algebraic Laws for Query Plans Selection and Join p: predicate with only R attributes q: predicate with only S attributes m: predicate with attributes from R and S  p (R S) =  q (R S) = [  p (R)] S R [  q (S)]

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 207 Algebraic Laws for Query Plans Selection and Join  p  q (R S) = [  p (R)] [  q (S)]  p  q  m (R S) =  m [ (  p R) (  q S) ]  pvq (R S) = [ (  p R) S ] U [ R (  q S) ]

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 208 Algebraic Laws for Query Plans Selection and Join  p  q (R S) =  p [  q (R S) ] =  p [ R  q (S) ] = [  p (R)] [  q (S)]

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 209 Algebraic Laws for Query Plans Projection X: set of attributes Y: set of attributes XY: X U Y  xy (R) = May introduce projection anywhere in an expression tree as long as it eliminates no attributes needed by an operator above and no attributes that are in result  x [  y (R)]

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 210 Algebraic Laws for Query Plans Projection and Selection X: subset of R attributes Z: attributes in predicate P (subset of R attributes)  x (  p R) = Need to keep attributes for the selection and for the result  {  p [  x ( R ) ] } x x  xz

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 211 Algebraic Laws for Query Plans Projection, Selection and Join  xy {  p (R S) } =  xy {  p [  xz (R)  yz’ (S)] } Y: subset of S attributes z = subset of R attributes used in P z’ = subset of S attributes used in P

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 212 Improving Logical Query Plans Introduction How to apply the algebraic laws to improve a logical query plan? Goal: minimize the size (number of tuples, number of attributes) of intermediate results. Push selections down in the expression tree as far as possible. Push down projections, or add new projections where applicable.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 213 Improving Logical Query Plans Pushing Selections Replace the left side of one of these (and similar) rules by the right side: Can greatly reduce the number of tuples of intermediate results.  p1  p2 (R)   p1 [  p2 (R)]  p (R S)  [  p (R)] S

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 214 Improving Logical Query Plans Pushing Projections Replace the left side of one of this (and similar) rules by the right side: Reduces the number of attributes of intermediate results and possibly also the number of tuples.  x [  p (R)]   x {  p [  xz (R)]}

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 215 Improving Logical Query Plans Pushing Projections Consider the following example: R(A,B,C,D,E) P: (A=3)  (B=“cat”) Compare  E {  p (R)} vs.  E {  p {  ABE (R)}}

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 216 Improving Logical Query Plans Pushing Projections What if we have indexes on A and B? B = “cat” A=3 Intersect pointers to get pointers to matching tuples Efficiency of logical query plan may depend on choices made during refinement to physical plan. No transformation is always good!

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 217 Improving Logical Query Plans Grouping Associative / Commutative Operators For operators which are commutative and associative, we can order and group their arguments arbitrarily. In particular: natural join, union, intersection. As the last step to produce the final logical query plan, group nodes with the same (associative and commutative) operator into one n-ary node. Best grouping and ordering determined during the generation of physical query plan.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 218 Improving Logical Query Plans Grouping Associative / Commutative Operators U AB CDE  U AB CDE

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 219 From Logical to Physical Plans So far, we have parsed and transformed an SQL query into an optimized logical query plan. In order to refine the logical query plan into a physical query plan, we consider alternative physical plans, estimate their cost, and pick the plan with the least (estimated) cost. We have to estimate the cost of a plan without executing it. And we have to do that efficiently!

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 220 From Logical to Physical Plans When creating a physical query plan, we have to decide on the following issues. order and grouping of operations that are associative and commutative, algorithm for each operator in the logical plan, additional operators which are not represented in the logical plan, e.g. sorting, the way in which intermediate results are passed from one operator to the next, e.g. by storing on disk or passing one tuple at a time.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 221 Estimating the Cost of Operations Intermediate relations are the output of some relational operator and the input of another one. The size of intermediate relations has a major impact on the cost of a physical query plan. It impacts in particular - the choice of an implementation for the various operators and - the grouping and order of commutative / associative operators.

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 222 A method for estimating the size of an intermediate relation should be - reasonably accurate, - efficiently computable, - not depend on how that relation is computed. We want to rank alternative query plans w.r.t. their estimated costs. Accuracy of the absolute values of the estimates not as important as the accuracy of their ranks. Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 223 Size estimates make use of the following statistics for relation R: T(R) : # tuples in R S(R) : # of bytes in each R tuple B(R): # of blocks to hold all R tuples V(R, A) : # distinct values for attribute A in R. MIN(R,A): minimum value of attribute A in R. MAX(R,A): maximum value of attribute A in R. HIST(R,A): histogram for attribute A in R. Statistics need to be maintained up-to-date under database modifications! Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 224 RA: 20 byte string B: 4 byte integer C: 8 byte date D: 5 byte string ABCD cat110a cat120b dog130a dog140c bat150d T(R) = 5 S(R) = 37 V(R,A) = 3V(R,C) = 5 V(R,B) = 1V(R,D) = 4 Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 225 Size estimate for W = R1 x R2 T(W) = T(R1)  T(R2) S(W) = S(R1) + S(R2) Size estimate for W =  A=a (R) Assumption: values of A are uniformly distributed over the attribute domain T(W) = T(R)/V(R,A) S(W) = S(R) Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 226 Size estimate for W =  z  val (R) Solution 1: on average, half of the tuples will satisfy an inequality condition T(W) = T(R)/2 Solution 2: more selective queries are more frequent, e.g. professors who earn more than $200’000 (rather than less than $200’000 ) T(W) = T(R)/3 Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 227 Solution 3: estimate the number of attribute values in query range Use minimum and maximum value to define range of the attribute domain. Assume uniform distribution of values over the attribute domain. Estimate is the fraction of the domain that falls into the query range. Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 228 Z MIN(R,Z)=1 V(R,Z)=10 W=  z  15 (R) MAX(R,Z)=20 f = = 6 (fraction of range) T(W) = f  T(R) R Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 229 Size estimate for W = R1 R2 Consider only natural join of R1(X,Y) and R2(Y,Z). We do not know how the Y values in R1 and R2 relate: - disjoint, i.e. T( R1 R2 ) = 0, - Y may be a foreign key of R1 and the primary key of R2, i.e. T(R1 R2) = T(R1), - all the R1 and all the R2 tuples have the same Y value, i.e. T(R1 R2) = T(R1)  T(R2). Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 230 Make several simplifying assumptions. Containment of value sets : V(R1,Y)  V(R2,Y)  every Y value in R1 is in R2 V(R2,Y)  V(R1,Y)  every Y value in R2 is in R1 This assumption is satisfied when Y is foreign key in R1 and primary key in R2. Is also approximately true in many other cases. Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 231 Preservation of value sets : If A is an attribute of R1 but not of R2, then V(R1 R2,A) = V(R1,A). Again, holds if the join attribute Y is foreign key in R1 and primary key in R2. Can only be violated if there are “dangling tuples” in R1, i.e. R1 tuples that have no matching partner in R2. Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 232 Uniform distribution of attribute values : the values of attribute A are uniformly distributed over their domain, i.e. P(A=a1) = P(A=a2) =... = P(A=ak). This assumption is necessary to make cost estimation tractable. It is often violated, but nevertheless allows reasonably accurate ranking of query plans. Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 233 Independence of attributes : the values of attributes A and B are independent from each other, i.e. P(A=a|B=b) = P(A=a) and P(B=b|A=a) = P(B=b). This assumption is necessary to make cost estimation tractable. Again, often violated, but nevertheless allows reasonably accurate ranking of query plans. Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 234 Suppose that t1 is some tuple in R1, t2 some tuple in R2. What is the probability that t1 and t2 agree on the join attribute Y? If V(R1,Y)  V(R2,Y), then the Y value of t1 appears in R2, because of the containment of value sets. Assuming uniform distribution of the Y values in R2 over their domain, the probability of t2 having the same Y value as t1 is 1/V(R2,Y). Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 235 If V(R2,Y)  V(R1,Y), then the Y value of t2 appears in R1, and the probability of t1 having the same Y value as t2 is 1 / V(R1,Y). T(W) = number of pairs of tuples from R1 and R2 times the probability that an arbitrary pair agrees on Y. T(R1 R2) = T(R1) T(R2) / max(V(R1,Y), V(R2,Y)). Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 236 For complex query expressions, need to estimate T,S,V results for intermediate results. For example, W = [  A=a (R1) ] R2 treat as relation U T(U) = T(R1)/V(R1,A) S(U) = S(R1) Also need V (U, *) for all attributes of U(R1)! Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 237 R1V(R1,A)=3 V(R1,B)=1 V(R1,C)=5 V(R1,D)=3 U =  A=a (R1) ABCD cat110 cat120 dog13010 dog14030 bat15010 V(U,A) =1 V(U,B) =1 V(U,C) = T(R1)/ V(R1,A) V(U,D)... somewhere in between Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 238 R1(A,B), R2(A,C). Consider join U = R1 R2. Estimate V results for U. V(U,A) = min { V(R1, A), V(R2, A) } Holds due to containment of value sets. V(U,B) = V(R1, B) V(U,C) = V(R2, C) Holds due to preservation of value sets. Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 239 Consider the following 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 Group and order as (R1 R2) R3 Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 240 Partial result: U = R1 R2 T(U) = 1000  2000 / 200 V(U,A) = 50 V(U,B) = 100 V(U,C) = 300 Estimating the Cost of Operations

CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 241 Final result: Z = U R3 T(Z) = 1000  2000  3000 / (200  300) V(Z,A) = 50 V(Z,B) = 100 V(Z,C) = 90 V(Z,D) = 500 Estimating the Cost of Operations