Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 4432query processing - lecture 131 CS4432: Database Systems II Lecture #13 Query Processing Professor Elke A. Rundensteiner.

Similar presentations


Presentation on theme: "CS 4432query processing - lecture 131 CS4432: Database Systems II Lecture #13 Query Processing Professor Elke A. Rundensteiner."— Presentation transcript:

1 CS 4432query processing - lecture 131 CS4432: Database Systems II Lecture #13 Query Processing Professor Elke A. Rundensteiner

2 CS 4432query processing - lecture 132 Query in SQL  Query Plan in Algebra (logical)  Other Query Plan in Algebra (logical)

3 CS 4432query processing - lecture 133 Query plan 1 (in relational algebra)  B,D  R.A =“c”  S.E=2  R.C=S.C  X RS

4 CS 4432query processing - lecture 134 Query plan 2 (in relational algebra)  B,D  R.A = “c”  S.E = 2 R S natural join

5 CS 4432query processing - lecture 135 Relational algebra optimization What are transformation rules ? –preserve equivalence What are good transformations? –reduce query execution costs

6 CS 4432query processing - lecture 136 Rules: Natural joins & cross products & union R S=SR (R S) T= R (S T)

7 CS 4432query processing - lecture 137 Note: Carry attribute names in results, so order is not important Can also write as trees, e.g.: T R R SS T

8 CS 4432query processing - lecture 138 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 Rules: Natural joins & cross products & union R S=SR (R S) T= R (S T)

9 CS 4432query processing - lecture 139 Rules: Selects  p1  p2 (R) =  p1vp2 (R) =  p1 [  p2 (R)] [  p1 (R)] U [  p2 (R)]

10 CS 4432query processing - lecture 1310 Bags vs. Sets R = {a,a,b,b,b,c} S = {b,b,c,c,d} RUS = ? Option 1 SUM RUS = {a,a,b,b,b,b,b,c,c,c,d} Option 2 MAX RUS = {a,a,b,b,b,c,c,d}

11 CS 4432query processing - lecture 1311 Option 2 (MAX) makes this rule work:  p1vp2 (R) =  p1 (R) U  p2 (R) Example: 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}

12 CS 4432query processing - lecture 1312 “Sum” option makes more sense: Senators (……)Rep (……) 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 Union?

13 CS 4432query processing - lecture 1313 Executive Decision -> Use “SUM” option for bag unions -> Some rules cannot be used for bags

14 CS 4432query processing - lecture 1314 Rules: Project Let: X = set of attributes Y = set of attributes XY = X U Y  xy (R) =  x [  y (R)]

15 CS 4432query processing - lecture 1315 Let p = predicate with only R attribs q = predicate with only S attribs m = predicate with only R,S attribs  p (R S) =  q (R S) = Rules:  combined [  p (R)] S R [  q (S)]

16 CS 4432query processing - lecture 1316 Some Rules can be Derived:  p  q (R S) =  p  q  m (R S) =  pvq (R S) = Rules:  combined (continued)

17 CS 4432query processing - lecture 1317 Do one, others for homework:  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) ]

18 CS 4432query processing - lecture 1318 --> Derivation for first one:  p  q (R S) =  p [  q (R S) ] =  p [ R  q (S) ] = [  p (R)] [  q (S)]

19 CS 4432query processing - lecture 1319 Rules:  combined Let x = subset of R attributes z = attributes in predicate P (subset of R attributes)  x [  p ( R ) ] =  {  p [  x ( R ) ] } x x  xz

20 CS 4432query processing - lecture 1320 Rules:  combined Let x = subset of R attributes y = subset of S attributes z = intersection of R,S attributes  xy (R S) =  xy { [  xz ( R ) ] [  yz ( S ) ] }

21 CS 4432query processing - lecture 1321  xy {  p (R S) } =  xy {  p [  xz’ (R)  yz’ (S)] } z’ = z U { attributes used in P }

22 CS 4432query processing - lecture 1322 Rules for    combined with X similar... e.g.,  p (R X S) = ?

23 CS 4432query processing - lecture 1323  p (R U S) =  p (R) U  p (S)  p (R - S) =  p (R) - S =  p (R) -  p (S) Rules   U  combined:

24 CS 4432query processing - lecture 1324  p1  p2 (R)   p1 [  p2 (R)]  p (R S)  [  p (R)] S R S  S R  x [  p (R)]   x {  p [  xz (R)] } Which are “good” transformations?

25 CS 4432query processing - lecture 1325 Conventional wisdom: do projects early Example: R(A,B,C,D,E) x={E} P: (A=3)  (B=“cat”)  x {  p (R)} vs.  E {  p {  ABE (R)} }

26 CS 4432query processing - lecture 1326 What if we have A, B indexes? B = “cat” A=3 Intersect pointers to get pointers to matching tuples But

27 CS 4432query processing - lecture 1327 Bottom line: No transformation is always good Usually good: early selections

28 CS 4432query processing - lecture 1328 In textbook: more transformations Eliminate common sub-expressions Other operations, such as, duplicate elimination and others.


Download ppt "CS 4432query processing - lecture 131 CS4432: Database Systems II Lecture #13 Query Processing Professor Elke A. Rundensteiner."

Similar presentations


Ads by Google