Download presentation
Presentation is loading. Please wait.
1
Algebraic Laws
2
{P1,P2,…..} {P1,C1>...} parse convert apply laws estimate result sizes consider physical plans estimate costs pick best execute Pi answer SQL query parse tree logical query plan “improved” l.q.p l.q.p. +sizes statistics
3
Algebraic laws for transforming logical query plans Commutative and associative laws: Above laws are applicable for both sets and bags
4
Laws Involving Selection ( ) Splitting laws Order is flexible
5
Algebraic Laws involving selection For the binary operators, we push the selection only if all attributes in the condition C are in R.
6
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)
7
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. StarsIn(title,year,starName) Movie(title,year,length,studioName) CREATE VIEW MoviesOf1996 AS SELECT * FROM MOVIE WHERE year=1996; Query: Which stars worked for which studios in 1996? SELECT starName,studioName FROM MoviesOf1996 NATURAL JOIN StarsIN;
8
pull selection up then push down
9
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.
10
Examples for pushing projection Schema R(a,b,c), S(c,d,e)
11
Example: Pushing Projection Schema: StarsIn(title,year,starName) Query: SELECT starName FROM StarsIn WHERE year = 1996; Should we transform to ? Depends! starName year=1996 StarsIn starName year=1996 StarsIn starName,year
12
Reasons for not pushing the projection If there is an index on StarsIn.year, such index is useless in the projected relation starName,year (StarsIn) –While such an index is very useful for the selection on “ year=1996 ”
13
Improving logical query plans 1.Push as far down as possible (sometimes pull them up first). 2.Do splitting of complex conditions in 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
14
Example of improvement SELECT title FROM StarsIn, MovieStar WHERE starName = name AND birthdate LIKE '%1960'; title starname=name AND birthdate LIKE ‘%1960’ StarsIn MovieStar title StarsIn MovieStar starName=name birthdate LIKE ‘%1960’
15
title StarsIn MovieStar starName=name birthdate LIKE ‘%1960’ name And a better plan introducing a projection to filter out useless attributes:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.