42 Example Join-- File Information 4 Emp( Fn Char(10), Minit Char, LN Char(20), SSN number(9), Bdate Date, Addr char(40), Sex Char, Salary Number(9,2), Dno number(3))
Employee Numbers –r = 10,000 records –|r| = 99 bytes/record –Block Size = 512 bytes/block –bf = 5 records/block –b = 2,000 blocks –B+-Tree on Dno: L dno = 3, d dno = 125, s dno = 80, m dno = 47 B Ldno =
44 Example Join-- File Information 4 Dept( D# Number(3), Dname Char(20), MGRSSN Number(9), MgrStartDate Date)
Dept Numbers –r = 125 records –|r| = 38 bytes/record –bf = 13 records/block –b = 10 blocks –B+-Tree on D#: L dno = 2, d dno = 125, s dno = 1, m dno = 47 B Ldno = 5 45
46 The Sample Query: I want to use Emp ê Dept. What are my options? Dno=D# 4 Nested Loop approach. 4 Use B-Tree on Emp.Dno. 4 Use B-Tree on Dept.D# 4 Cluster Employee and Department together.
47 Join Query -- Nested Loop Emp ê Dept. Dno=D# 4 CJ1 = b dept + b Emp * b Dept = * 10 = 20,010
48 Join Query -- B-Tree on D# Emp ê Dept. Dno=D# 4 CJ2 D# = b Emp + r Emp * (L Dept.D# + 1) = * (2 + 1) = 32,000
49 Join Query -- B-Tree on Dno Emp ê Dept. Dno=D# CBn Dno = L+ ( s / ( m/2 -1) + s = 3 + ( / ( 47/2 -1) + 80 = = 86 4 CJ2 D# = b Dept + r Dept * (CBn Dno ) = * (86) = 10,760
Join Query – B-Tree on Dno Disucssion 4 This is the better choice. 4 Note: I have ignored the WTDC here because it would be the same in all cases. 50
51 Join Query -- Clustered Files Emp ê Dept. Dno=D# 4 CJ3 = b R + b s = = 2,010 4 This is the best choice. 4 Note: it does restrict how the files are stored.
52 Join Query – Sort Files First Emp ê Dept. Dno=D# 4 Sort Emp: b R *Log 2 (b R ) = 2,000*11= Sort Dept: b S *Log 2 (b S ) = 10*4 = 40 4 CJ3 = b R + b S = = 2010 = = 24050
53 Join Query – Build Hash On Emp.DNO Emp ê Dept. Dno=D# 4 Build Hash: b R + r R = = Hash Join: = 1+ s = 81 4 CJ3 = b s + r S *(81) = *(81) = = = 22135
Example WTDC 4 If all columns are needed, we have –|r| = (D# stored only once) = 134 –r = r Emp = 10,000 –Bf = floor(512/134) = 3 –b = ceil(10,000/3) = 3334 –Thus the WTDC =