











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












CS 245 Notes 6 1
CS 245: Database System Principles
Hector Garcia-Molina
CS 245 Notes 6 2
Q Query Plan
CS 245 Notes 6 3
Q Query Plan
CS 245 Notes 6 4
Select B,D From R,S Where R.A = “c” S.E = 2 R.C=S.C
CS 245 Notes 6 5
a 1 10 10 x 2 b 1 20 20 y 2 c 2 10 30 z 2 d 2 35 40 x 1 e 3 45 50 y 3
CS 245 Notes 6 6
a 1 10 10 x 2 b 1 20 20 y 2 c 2 10 30 z 2 d 2 35 40 x 1 e 3 45 50 y 3 Answer B D 2 x
CS 245 Notes 6 7
CS 245 Notes 6 8
a 1 10 10 x 2 a 1 10 20 y 2 . . C 2 10 10 x 2 . .
CS 245 Notes 6 9
a 1 10 10 x 2 a 1 10 20 y 2 . . C 2 10 10 x 2 . .
Bingo! Got one...
CS 245 Notes 6 10
CS 245 Notes 6 11
Ex: Plan I B,D
CS 245 Notes 6 12
R S
natural join
CS 245 Notes 6 19
R S A B C C D E a 1 10 10 x 2 b 1 20 20 y 2 c 2 10 30 z 2 d 2 35 40 x 1 e 3 45 50 y 3
A C I 1 I 2
=“c”
<c,2,10> (^) <10,x,2> check=2? output: <2,x>
CS 245 Notes 6 20
R S A B C C D E a 1 10 10 x 2 b 1 20 20 y 2 c 2 10 30 z 2 d 2 35 40 x 1 e 3 45 50 y 3
A C I 1 I 2
=“c”
<c,2,10> (^) <10,x,2> check=2? output: <2,x>
next tuple: <c,7,15>
CS 245 Notes 6 21
CS 245 Notes 6 22
parse
convert
apply laws
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
statistics
CS 245 Notes 6 23
SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE birthdate LIKE ‘%1960’ ); (Find the movies with stars born in 1960)
CS 245 Notes 6 24
CS 245 Notes 6 25
Example: Generating Relational Algebra
StarsIn
starName MovieStar Fig. 7.15: An expression using a two-argumentand relational algebra , midway between a parse tree CS 245 Notes 6 26
Example: Logical Query Plan
MovieStar Fig. 7.18: Applying the rule for IN conditions
CS 245 Notes 6 27
Example: Improved Logical Query Plan
starName=name
MovieStar Fig. 7.20: An improvement on fig. 7.18.
Question: Push project to StarsIn?
CS 245 Notes 6 28
Need expected size
StarsIn
MovieStar
CS 245 Notes 6 29
Parameters: join order, memory size, project attributes,...
Hash join
SEQ scan index scan Parameters: Select Condition,... StarsIn MovieStar
CS 245 Notes 6 30
P1 P2 …. Pn
C1 C2 …. Cn
Pick best!
CS 245 Notes 6 37
CS 245 Notes 6 38
CS 245 Notes 6 39
R x S = S x R (R x S) x T = R x (S x T)
CS 245 Notes 6 40
p1p2(R) =
p1vp2(R) =
CS 245 Notes 6 41
p1p2(R) =
p1vp2(R) =
p1 [ p2 (R)]
[ p1 (R)] U [ p2 (R)]
CS 245 Notes 6 42
R = {a,a,b,b,b,c} S = {b,b,c,c,d} RUS =?
CS 245 Notes 6 43
R = {a,a,b,b,b,c} S = {b,b,c,c,d} RUS =?
CS 245 Notes 6 44
Option 2 (MAX) makes this rule work: p1 v p2 (R) = p1(R) U p2(R) Example: R={a,a,b,b,b,c} P1 satisfied by a,b; P2 satisfied by b,c
CS 245 Notes 6 45
Option 2 (MAX) makes this rule work:
p1 v p2 (R) = p1(R) U p2(R)
Example: R={a,a,b,b,b,c} P1 satisfied by a,b; P2 satisfied by b,c p1 v p2 (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} CS 245 Notes 6 46
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?
CS 245 Notes 6 47
-> Use “SUM” option for bag unions -> Some rules cannot be used for bags
CS 245 Notes 6 48
Let: X = set of attributes Y = set of attributes XY = X U Y
xy (R) =
CS 245 Notes 6 55
pq (R S) =
p [q (R S) ] =
p [ R q (S) ] =
[p (R)] [q (S)]
CS 245 Notes 6 56
Rules: combined
Let x = subset of R attributes z = attributes in predicate P (subset of R attributes)
x [p (R) ] =
CS 245 Notes 6 57
Rules: combined
Let x = subset of R attributes z = attributes in predicate P (subset of R attributes)
x [p (R) ] = {p [ x (R) ] }
CS 245 Notes 6 58
Rules: combined
Let x = subset of R attributes z = attributes in predicate P (subset of R attributes)
x [p (R) ] = x{p [ x (R) ] }
xz
CS 245 Notes 6 59
Rules: combined
Let x = subset of R attributes y = subset of S attributes z = intersection of R,S attributes xy (R S) =
CS 245 Notes 6 60
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) ] }
CS 245 Notes 6 61
xy {p (R S)} =
CS 245 Notes 6 62
xy {p (R S)} =
xy {p [xz’ (R) yz’ (S)] } z’ = z U { attributes used in P }
CS 245 Notes 6 63
Rules for combined with X
similar... e.g., p (R X S) =?
CS 245 Notes 6 64
p(R U S) = p(R) U p(S) p(R - S) = p(R) - S = p(R) - p(S)
Rules U combined:
CS 245 Notes 6 65
p1p2 (R) p1 [p2 (R)]
p (R S) [p (R)] S R S S R x [p (R)] x {p [xz (R)] }
CS 245 Notes 6 66
Example: R(A,B,C,D,E) x={E} P: (A=3) (B=“cat”)
x {p (R)} vs. E {p{ABE (R)} }
CS 245 Notes 6 73
Example R A: 20 byte string B: 4 byte integer C: 8 byte date D: 5 byte string
A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d
CS 245 Notes 6 74
Example R A: 20 byte string B: 4 byte integer C: 8 byte date D: 5 byte string
A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d T(R) = 5 S(R) = 37 V(R,A) = 3 V(R,C) = 5 V(R,B) = 1 V(R,D) = 4
CS 245 Notes 6 75
CS 245 Notes 6 76
CS 245 Notes 6 77
Size estimate for W = A=a (R)
CS 245 Notes 6 78
Example R V(R,A)= V(R,B)= V(R,C)= V(R,D)=
W = z=val (R) T(W) =
A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d
CS 245 Notes 6 79
Example R V(R,A)= V(R,B)= V(R,C)= V(R,D)=
W = z=val (R) T(W) =
A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d
CS 245 Notes 6 80
Values in select expression Z = val are uniformly distributed over possible V(R,Z) values.
CS 245 Notes 6 81
Values in select expression Z = val are uniformly distributed over domain with DOM(R,Z) values.
CS 245 Notes 6 82
Example R Alternate assumption V(R,A)=3 DOM(R,A)= V(R,B)=1 DOM(R,B)= V(R,C)=5 DOM(R,C)= V(R,D)=4 DOM(R,D)=
A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d
W = z=val (R) T(W) =?
CS 245 Notes 6 83
C=val T(W) = (1/10)1 + (1/10)1 + ... = (5/10) = 0.
B=val T(W)= (1/10)5 + 0 + 0 = 0.
A=val T(W)= (1/10)2 + (1/10)2 + (1/10) = 0.
CS 245 Notes 6 84
Example R Alternate assumption V(R,A)=3 DOM(R,A)= V(R,B)=1 DOM(R,B)= V(R,C)=5 DOM(R,C)= V(R,D)=4 DOM(R,D)=
A B C D cat 1 10 a cat 1 20 b dog 1 30 a dog 1 40 c bat 1 50 d
W = z=val (R) T(W) = (^) DOM(R,Z)T(R)
CS 245 Notes 6 91
Equivalently: fV(R,Z) = fraction of distinct values T(W) = [f V(Z,R)] T(R) = f T(R) V(Z,R)
CS 245 Notes 6 92
Let x = attributes of R y = attributes of R
CS 245 Notes 6 93
Let x = attributes of R y = attributes of R
X Y =
Same as R1 x R
Case 1
CS 245 Notes 6 94
Case 2
CS 245 Notes 6 95
Case 2
Assumption:
V(R1,A) V(R2,A) Every A value in R1 is in R V(R2,A) V(R1,A) Every A value in R2 is in R
“containment of value sets” Sec. 7.4.
CS 245 Notes 6 96
Take 1 tuple (^) Match
CS 245 Notes 6 97
Take 1 tuple (^) Match
1 tuple matches with T(R2) tuples... V(R2,A) so T(W) = T(R2) T(R1) V(R2, A) CS 245 Notes 6 98
[A is common attribute]
CS 245 Notes 6 99
max{ V(R1,A), V(R2,A) }
CS 245 Notes 6 100
Values uniformly distributed over domain
This tuple matches T(R2)/DOM(R2,A) so T(W) = T(R2) T(R1) = T(R2) T(R1) DOM(R2, A) DOM(R1, A)
Assume the same
CS 245 Notes 6 101
size of attribute A
CS 245 Notes 6 102
A=aB=b (R) …. Sec. 16.4. R S with common attribs. A,B,C Sec. 16.4. Union, intersection, diff, …. Sec. 16.4.
CS 245 Notes 6 109
T(R1) = 1000 V(R1,A)=50 V(R1,B)= T(R2) = 2000 V(R2,B)=200 V(R2,C)= T(R3) = 3000 V(R3,C)=90 V(R3,D)=
R R R
CS 245 Notes 6 110
CS 245 Notes 6 111
CS 245 Notes 6 112
A Note on Histograms
10 20 30 40
10
20
30
40 number of tuples in R with A value in given range
A=val (R) =?
CS 245 Notes 6 113
CS 245 Notes 6 114