Algebraic Laws SQL query parse tree convert logical
Algebraic Laws
SQL query parse tree convert logical query plan apply laws “improved” l. q. p estimate result sizes l. q. p. +sizes consider physical plans {P 1, P 2, …. . } answer execute Pi statistics pick best {P 1, C 1>. . . } estimate costs
Algebraic laws for transforming logical query plans • Commutative and associative laws: Above laws are applicable for both sets and bags
Laws Involving Selection ( ) Splitting laws Order is flexible
Algebraic Laws involving selection For the binary operators, we push the selection only if all attributes in the condition C are in R.
Example: • Consider R(A, B) and S(B, C) and the expression below: A=1 AND B<C(R S) 1. Splitting AND A=1 ( B < C(R S)) 2. Push to S A=1 (R B < C(S)) 3. Push to R A=1 (R) B < C(S)
Pushing selections • Usually selections are pushed down the expression tree. • The following example shows that it is sometimes useful to pull selection up in the tree. Stars. In(title, year, star. Name) Movie(title, year, length, studio. Name) CREATE VIEW Movies. Of 1996 AS SELECT * FROM MOVIE WHERE year=1996; Query: Which stars worked for which studios in 1996? SELECT star. Name, studio. Name FROM Movies. Of 1996 NATURAL JOIN Stars. IN;
pull selection up then push down
Laws for (bag) Projection • A simple law: Project out attributes that are not needed later. – i. e. keep only the input attr. and any join attribute.
Examples for pushing projection Schema R(a, b, c), S(c, d, e)
Example: Pushing Projection • Schema: Stars. In(title, year, star. Name) • Query: SELECT star. Name FROM Stars. In WHERE year = 1996; star. Name Should we transform to year=1996 ? Depends! year=1996 star. Name, year Stars. In
Reasons for not pushing the projection • If there is an index on Stars. In. year, such index is useless in the projected relation star. Name, year(Stars. In) – While such an index is very useful for the selection on “year=1996”
Improving logical query plans 1. Push as far down as possible (sometimes pull them up first). 2. Do splitting of complex conditions in order to push even further. 3. Push as far down as possible, introduce new early (but take care for exceptions) 4. Combine with to produce -joins or equi-joins • Choose an order for joins
Example of improvement SELECT title FROM Stars. In, Movie. Star WHERE star. Name = name AND birthdate LIKE '%1960'; title starname=name AND birthdate LIKE ‘%1960’ star. Name=name birthdate LIKE ‘%1960’ Stars. In Movie. Star
And a better plan introducing a projection to filter out useless attributes: title star. Name=name Stars. In birthdate LIKE ‘%1960’ Movie. Star
- Slides: 15