110 likes | 217 Views
Query Compiler. 16.3 From Parse Trees to Logical Query Plans. A.Sri Harsha 211. Agenda. Converting Parse tree to Logical query Plan Conversion to Relational Algebra Algebraic Laws Removing Sub-queries from Conditions More Algebraic Laws
E N D
Query Compiler 16.3 From Parse Trees to Logical Query Plans A.Sri Harsha 211
Agenda • Converting Parse tree to Logical query Plan • Conversion to Relational Algebra • Algebraic Laws • Removing Sub-queries from Conditions • More Algebraic Laws • Grouping Associative/Commutative Operators
Converting Parse tree to logical query plan Parse Tree Replace nodes and structures of parse tree by operators of relational algebra Relational Algebra expression Turn it to an expression that can be converted to the most efficient physical query plan Logical Query plan
Conversion to Relational Algebra Parse Tree Π movieTitle σ StarName = name AND birthdate LIKE '%1960' X StarsIn MovieStar
Algebraic Laws • Commutativity: • R x S = S x R • R S = S R • R U S = S U R • R ∩ S = S ∩ R • Associativity: • (R x S) x T = R x (S x T) • (R S) T = R (S T) • (R U S) U T = R U (S U T) • (R ∩ S) ∩ T = R ∩ (S ∩ T)
Removing Sub-queries from Conditions Suppose we have a two-argument selection in which the first argument represents some relation R and the second argument is a <Condition> of the form t INS, where expression S is an uncorrelated subquery, and t is a tuple composed of some attributes of R. • Replace the <Condition> by the tree that is the expression for S. If S may have duplicates, then it is necessary to include a <delta> operation at the root of the expression of S, so the expression being formed does not produce more copies of tuples than the original query does. • Replace the two-argument selection by a one-argument selection <sigma>C, where C is the condition that equates each component of the tuple t to the corresponding attribute of the relation S. • Give <sigma>C an argument that is the product of R and S.
Removing Sub-queries from Conditions σ σ C X R <Condition> δ R t IN S S
Removing Sub-queries from Conditions Applying the rule for IN conditions Π movieTitle StarName = name Π StarsIn birthdate LIKE '%1960' σ name
Grouping Associative/Commutative Operators Natural Joins or theta-joins can also be obtained with each other under certain circumstances • We must replace the natural joins with theta-joins that equate the attributes of the same. • We must add of projection to eliminate duplicate copies of attributes involved in a natural join that has become a theta-join • The theta-join conditions must be associative.
Grouping Associative/Commutative Operators U U V W U U V W R S T U R S T