3. ¯
. ¯ ¯, ,
¯ , , , –× ∩∪ •
) , ¯
– select •
– (projection) –π •
.( ¯
– π A→B • . - ¯ ¯ ,
( ¯ ) –δ •
– join •
¯ ¯ – sort •
,
(? ¯)
¯
– max, min, average, sum •
– count •
¯ 9 ¯ 10
∪ ∩
A1 A2 A3 R ∪ S = {r: r ∈ R or r ∈ S} R ∩ S = {r: r ∈ R and r ∈ S} ¯
a b c A1 A2 A3 R A1 A2 A3
S A1 A2 A3
R
d e f a b c a b c a b c
c b d R∪S d e f a b c c b d
c b d c b d a c e
A1 A2 A3
S c b d R∩S
c b d d a f A1 A2 A3
¯
d a f a b c
¯
¯
¯ 11
c b d ¯ 12
4. × ¯
R × S = {(r1,r2,…,rn,s1,…,sm): r ∈ R and s ∈ S}
R S = {r: r ∈ R and r ∉ S} ¯
R A1 A2 A3 A1 A2 A3 B1 B2
R A1 A2 A3 S A1 A2 A3
a b c a b c a b
a b c a b c a b c a b
a b c
a b c c b e c b d a b
c b d
c b d a c e a b c c b
S B1 B2
a b c c b
A1 A2 A3 a b R×S
RS c b d c b
a b c c b , ¯ ¯
c b d (R.a, S.a ) ¯
( ) (R ) × (S ):
¯ 13 ¯ 14
select σ π projection
.C select
σC(R) = {r ∈ R : r satisfies C} πℓ1, ℓ2,…, ℓn(R)={(rℓ1,rℓ2,…rℓn) : (r1,…,rn) ∈ R}
A1 A2 A3
σA2=b(R)
R R πA1,A3(R) πA2(R)
a b c A1 A2 A3
A1 A2 A3 A1 A3 A2
a b c a b c
a b c a c b
c b d a b c
a b c a c b
c c c c b d
c b d c d b
σa≤A2≤b(R) " [a,b]
¯ 15 ¯ 16
5. join ( ) join
. ¯
¯ ¯ :A = attr(R) ∩ attr(S) ¯ S ,R join
. R S = {r s : r ∈ R, s ∈ S, πA(r) = πA(s) }
:
:
Q(name, address, tel_no) = R(name, address) S(name, tel_no)
A B C A B C D
C D
: join a b c a b c d1
A ¯ S,R c d1
a1 b1 c a1 b1 c d1
r.A = s.A - ¯ r ∈ R, s ∈ S c2 d2
a1 b1 c1 a1 d c2 d2
s- r r s c2 d3
.A ¯ a1 d c2 a1 d c2 d3
s = (Homer Simpson, 555-2121)
r = (Homer Simpson, 123 Fake St. Springfield)
r s = (Homer Simpson, 123 Fake St. Springfield, 555-2121)
¯ 17 ¯ 18
(join) join
. ¯ ' ¯ ¯ R
¯ S R ¯ s r
. ¯ , ' ¯ ¯ S
.A
: ¯ R S
. ¯ , ¯, , ¯ ¯ S R
R S = πS∪(R/S) (σAR=A(ρA→AR(r) × s))
¯ 19 ¯ 20
6. .( ) R
.R ( ) = B(R)
. .R = T(R)
. .V(R)=T(δ(R)) " ,R = V(R)
. . ¯ = M
. . ¯
¯ (pipeline) , ¯-
.
,
. ¯
¯ 21 ¯ 22
. -
. max, min, average
Union(relation R, relation S) {
forall r ∈ R { double average(relation R, column A) {
read r; int count = 0;
output r; value sum = 0;
} forall r ∈ R {
forall s ∈ S { read r;
read s; sum = sum + r.A;
output s; count = count + 1;
} }
} return sum / (double) count;
B(R) + B(S) : ¯ }
O(1) : ¯ B(R) : ¯
¯ 23 ¯ 24
7. δ
B(δ(R)) ≤ M - 4 . S
relation delta(relation R) { B(δ (S) ) ≤ M ¯
search_structure = ∅; : -4
( ¯ R )
forall r ∈ R {
read r;
if (r ∉ search_structure) { SR:
insert r into search_structure; . δ(S) ¯
output r; .R
} . S- r ,R- r ¯
} , S ¯
} .
. ¯ ¯ search_structure
¯ 25 ¯ 26
SR SR
relation Difference(relation R, relation S) {
S=(20, 11, 30, 20,11,11) search_structure = ∅;
R=(11,40,20,11, 20, 20, 50, 40) forall s ∈ S {
read s;
11:3, 20:2, 30:1 S if s ∉ search_structure {
insert s into search_structure;
R search_structure[s].count = 0;
11:2, 20:2, 30:1 :11 }
search_structure[s].count ++;
11:2, 20:2, 30:1 :40 }
forall r ∈ R {
11:2, 20:1, 30:1 :20 read r;
if (r ∈ search_structure) and (search_structure[r].count > 0)
11:1, 20:1, 30:1 :11 search_structure[r].count --;
}
11:1, 20:0, 30:1 :20 forall s ∈ search_structure
repeat search_structure[s].count times
11:1, 20:0, 30:1 :20 output s;
¯ 27
} ¯ 28
8. ¯ S ¯ ¯
( ¯) ¯ ¯ ¯ S ¯
, S ¯
: ¯ .(A- ) ¯
B(S) + B(R)
: ¯ .( ¯ ¯) R ¯
B(S) , r.A R- r ¯
(r.A = s.A ¯) s ¯
. r s
¯ 29 ¯ 30
R S R S
A B B C A B B C
a b1 b c1 a b1 b c1
a2 b2 b1 c2 (a,b1,c2) a2 b2 b1 c2
(a,b1,c3)
a1 b1 b1 c3 a1 b1 b1 c3
a6 b3 b3 c4 a6 b3 b3 c4
a4 b3 a4 b3
( ¯ )
a5 b3 a5 b3
( )
¯ 31 ¯ 32
9. R S R S
A B B C A B B C
a b1 b c1 a b1 b c1
--- a2 b2 b1 c2 (a1,b1,c2) a2 b2 b1 c2
(a1,b1,c3)
a1 b1 b1 c3 a1 b1 b1 c3
a6 b3 b3 c4 a6 b3 b3 c4
a4 b3 a4 b3
(a6,b3),(a4,b3),(a5,b3)
a5 b3 a5 b3
(b3,c4)
¯ 33 ¯ 34
¯ ¯ δ(S) ¯ : ¯
¯
:
B(S) + B(R) B(R) O(1) σ, π,
B(R) + B(S) O(1) ∪
. ¯ ¯
B(R) δ(R) δ
B(R) + B(S) δ(S) ×∩
¯ –
¯ 35 ¯ 36
10. nested loops
.( ) :
. for each tuple s in S {
read s;
. for each tuple r in R {
. read r;
. process tuples r and s
}
}
T(S)T(R) : ¯
T(S)B(R) R :
¯ 37 ¯ 38
:
join ¯ .S ¯ M-4 ¯
for each tuple s in S {
read s; :
for each tuple r in R { S M-4
( ¯ )R ¯
read r; R- r ¯
if s and r join to make a tuple t s ¯ ¯ S
output t; r.A = s.A :
} r s
}
T(S)B(R) : ¯ (M-4) + B(R) : ¯
B(S)/(M-4)
:
¯ 39 ¯ 40
11. :
¯
(M-4) + B(R) : while (S is not exhausted) {
B(S)/(M-4)
: read the next M-4 blocks of S and put them in a
search structure ordered by A;
: ¯ forall blocks b of R {
read block b;
B(S)/(M-4) ((M-4) + B(R)) = B(S) + B(S) B(R) /(M-4) forall tuples r in b {
≅ B(S) B(R) / M search r.A in the search structure;
forall tuple s found
output s r;
M >> 4 }
, }
? .R }
¯ 41 ¯ 42
.( ) . ¯
. ,( ) S- R
. .R S
.R r- S s ¯ ¯
. ( ) r >s
. .( )S s
.R r r <s
,r=s
. s r
?
¯ 43 ¯ 44
12. היכן השתמשנו
?M בהנחה על
¯ :
S- R- ¯ ,A S- R-
.|σA=a(S)| > M A a
.R S ¯
.R r- S s ¯ ¯
S s πA(r) >πA(s) 1 B(R) δ
R r πA(r) <πA(s)
1 1 Min, Max
πA(s’) =πA(s) S ¯ ¯
:πA(r’) =πA(s) R r’ ¯ *2 B(S)+B(R) ∩ , ∪, / ( )
r’ s’ ¯ s’ ¯
*2 B(S)+B(R)
B(S) + B(R) : ¯
B(S R) : ¯ ¯ ¯ ¯, ¯ *
¯ 45 ¯ 46
:
.( )
: ¯ ¯
.
. ¯
. . (hash)
¯ ; ¯ ¯
. . ¯
: ¯ ,
, ¯
. ¯
¯ 47 ¯ 48
13. δ
53,42,73,81,22,53,91,61,53,11,92,91 : , ¯
.( ) : , ,
1: 81,91,61,11,91 . k- ¯
2: 42,22,92
3: 53,73,53,53 : , ¯ ¯
: ¯ . ¯ :
¯ ¯ ( ) ¯ M
:( ¯ ) δ k
1: 81,91,61,11,91
2: 42,22,92 ?למה צריך יותר ממעבר אחד M ≥ 2k + 2 :
3: 53,73,53,53 למה לא לסלק את הכפולים k ≤ M/2 -1 "
?כאשר מנסים להכניסם לדלי
¯ 49 ¯ 50
: δ : δ
. ( ) B(R) ¯¯
: ¯ ¯
( ¯, ) B(R) > M
, k= M/2-1 -
. :
, k- ¯ ,
. ¯ : ...
. ¯
.M k- ¯ h-
.( ¯ ¯) δ
h = logk(B(R)/M)
:
M≥ ⇐ ¯ ¯ ¯
(2h+1)B(R) :
¯ ,B(R)/k ≤ M
M M
2
B ( R ) ≤ kM = M − 1 ≈
2 2
¯
3B(R) , ¯ 51 ¯ 52
14. for all s ∈ S :
write s to bucket BS[h(s)];
2B(S) S
for all r ∈ R
write r to bucket BR[h(r)]; 2B(R) R
B(S) S
. ¯ ¯ BR[i]- BS[i] ,i ¯
B(R) R
for i=1 to number_of_buckets { 3B(S) + 3B(R) ¯"
find the intersection of Bs[i] and Br[i] using the single pass
algorithm and produce the sub-relation Ti
output Ti
}
¯ 53 ¯ 54
¯
¯ A : B ≤ M2 / 2
h(s.A) " S1,…Sk S 3B(R) δ
2B(S) B(Si) ≤ M :i ¯ ¯ 3B(S) + 3B(R) '¯ , ,
2B(R) h(r.A) " R1,…Rk R
(? ¯) Ri Si i=1,..,k
Σi(B(Si)+B(Ri)) (? ) 5- 3 , B
3B(S)+3B(R) ¯"
¯ 55 ¯ 56
21. ¯, ¯ ¯
w0 < w1<…< wm , ¯ ¯
ni = T(σwi<R(A)≤w(i+1)(R)) .( ) ¯
:σ
σ R(A)≤ x (R) = ∑n
ai ≤ x
i ?
.
: (percentile) , ,
{x ∈ R : x ≤ a i } i : i ¯ ¯ a1,…,a99 . ¯ R(X,Y) S(Y,Z) U(Z,W) :
= (R S) U) .
T ( R) 100
E(T(R S)) = 40,000 :
m a x {i : x ≤ a i } : ¯ R (S U) .
σ R (A ) ≤ x (R ) = T (R ) E(T(S U)) = 20,000 :
100 '
¯ 81 ¯ 82
Pipeling
,(S U) R - :
R S U ¯ . ¯
.(R )
S U
:pipelining .
U
= R ≠ R
¯ R S S U U S
. ¯
. ¯ pipeline .
. ¯ , ¯
¯ 83 ¯ 84
22. , ¯ :pipeline R(X,Y) S(Y,Z) U(Z,W)
T(R)=1000 T(S)=2000 T(U)=5000
Bushy
E(T(R S)) = T(S)T(R)/max{V(S,Y),V(R,Y)}
= 2,000 × 1,000 / 50 = 40,000
U R
( ) n !n
: R S S U
, •
Estimated cost = 40,000 Estimated cost = 20,000
. ¯¯ ¯ •
¯ 85 ¯ 86
¯ (1) ¯ : ¯
W(A,B) X(B,C) Y(C,D) Z(D,E) . R1=W X
T 100 200 300 400 . R1
: " R2 = R1 Y
V A: 20 B: 50 C: 50 D: 40
; ¯ R1
V B: 60 C: 100 D: 50 E: 100 ; Y
W X Y Z ;R1 Y
.
((W X) Y) Z: : " R2 Z
" ; R2
333 :R1 = W X ; Z
1000 :R2 = (W X) Y ;R2 Z
333 + 1000 = 1333 : .
¯ 87 ¯ 88