Discussion 3
B+ Trees
Announcements
Vitamin 3 (B+ trees) is due Mon 9/21 at 11:59PM.
Project 2 (B+ trees) is due Thurs 9/24 at 11:59PM.
Midterm 1 Alterations Form released, due Sun 9/20 at 11:59PM.
Indices (B+ Trees)
Indices
B+ trees
B+ trees
B+ trees
B+ trees
17
13
5
30
24
2*
3*
5*
7*
8*
14*
16*
19*
20*
22*
24*
27*
29*
33*
34*
38*
39*
Root Node
Page 2
Page 4
Page 3
Page 1
Page 5
Page 6
Page 7
Page 8
Page 9
B+ trees
17
13
5
30
24
2*
3*
5*
7*
8*
14*
16*
19*
20*
22*
24*
27*
29*
33*
34*
38*
39*
Root Node
Page 2
Page 4
Page 3
Page 1
Page 5
Page 6
Page 7
Page 8
Page 9
Searching B+ Trees
B+ trees: Search for 27
17
13
5
30
24
2*
3*
5*
7*
8*
14*
16*
19*
20*
22*
24*
27*
29*
33*
34*
38*
39*
Root Node
B+ trees: Search for 27
17
13
5
30
24
2*
3*
5*
7*
8*
14*
16*
19*
20*
22*
24*
27*
29*
33*
34*
38*
39*
Root Node
B+ trees: Search for 27
17
13
5
30
24
2*
3*
5*
7*
8*
14*
16*
19*
20*
22*
24*
27*
29*
33*
34*
38*
39*
Root Node
B+ trees: Search for 27
17
13
5
30
24
2*
3*
5*
7*
8*
14*
16*
19*
20*
22*
24*
27*
29*
33*
34*
38*
39*
Root Node
27*
Inserting into B+ Trees
B+ trees: insert 25
17
24
13
30
Root Node
2*
3*
5*
7*
14*
16*
19*
20*
22*
24*
27*
29*
33*
34*
38*
39*
B+ trees: insert 25
17
24
13
30
Root Node
2*
3*
5*
7*
14*
16*
19*
20*
22*
24*
27*
29*
33*
34*
38*
39*
B+ trees: insert 25
17
24
13
30
Root Node
2*
3*
5*
7*
14*
16*
19*
20*
22*
24*
27*
29*
33*
34*
38*
39*
B+ trees: insert 25
17
24
13
30
Root Node
2*
3*
5*
7*
14*
16*
19*
20*
22*
24*
27*
29*
33*
34*
38*
39*
B+ trees: insert 25
B+ trees: insert 25
17
24
13
30
Root Node
2*
3*
5*
7*
14*
16*
19*
20*
22*
24*
25*
27*
29*
33*
34*
38*
39*
B+ trees: insert 8
17
24
13
30
Root Node
2*
3*
5*
7*
14*
16*
19*
20*
22*
24*
25*
27*
29*
33*
34*
38*
39*
8*
B+ trees: insert 8
17
24
13
30
Root Node
2*
3*
5*
7*
14*
16*
19*
20*
22*
24*
25*
27*
29*
33*
34*
38*
39*
8*
B+ trees: insert 8
17
24
13
30
Root Node
2*
3*
5*
7*
14*
16*
19*
20*
22*
24*
25*
27*
29*
33*
34*
38*
39*
8*
B+ trees: insert 8
17
24
13
30
Root Node
14*
16*
19*
20*
22*
24*
25*
27*
29*
33*
34*
38*
39*
2*
3*
5*
7*
8*
B+ trees: insert 8
17
24
13
30
Root Node
14*
16*
19*
20*
22*
24*
25*
27*
29*
33*
34*
38*
39*
5*
7*
8*
2*
3*
5
B+ trees: insert 8
14*
16*
19*
20*
22*
24*
25*
27*
29*
33*
34*
38*
39*
5*
7*
8*
2*
3*
5
17
24
13
30
B+ trees: insert 8
14*
16*
19*
20*
22*
24*
25*
27*
29*
33*
34*
38*
39*
5*
7*
8*
2*
3*
5
17
24
13
30
B+ trees: insert 8
30
24
14*
16*
19*
20*
22*
24*
25*
27*
29*
33*
34*
38*
39*
5*
7*
8*
2*
3*
5
17
13
B+ trees: insert 8
17
30
24
14*
16*
19*
20*
22*
24*
25*
27*
29*
33*
34*
38*
39*
5*
7*
8*
2*
3*
5
13
B+ trees: insert 8
17
30
24
14*
16*
19*
20*
22*
24*
25*
27*
29*
33*
34*
38*
39*
5*
7*
8*
2*
3*
13
5
B+ trees: Insertion Steps Summary
Worksheet Q1
Worksheet #1a
Worksheet #1a
12. We could insert 12, 25, 44, 81, 80, 78, 76, 79, 77, 75, 74, 73.
Worksheet #1a
More generally: assuming duplicates are allowed (or keys are not constrained to be integers), you can find the max number of keys a tree of the same height/order can store, and subtract the current number of keys
Worksheet #1a
(2 * d) * (2 * d+ 1)h = (2 * 1) * (2 * 1 + 1)2 = 18
18 (max) - 6 (number of values in tree given) = 12 inserts
Worksheet #1b
Worksheet #1b
3. You could insert 1, 4, 5.
Worksheet #1b
34
6
21
50
2
3
11
43
24
72
Insert 1
1
Worksheet #1b
34
6
21
50
2
3
11
43
24
72
Insert 1
1
Worksheet #1b
34
6
21
50
2
3
11
43
24
72
Insert 1
1
2
Worksheet #1b
34
21
50
2
3
11
43
24
72
Insert 1
1
6
2
Worksheet #1b
34
21
50
2
3
11
43
24
72
Insert 1
1
6
2
Worksheet #1b
6
34
50
2
3
11
43
24
72
Insert 1
1
21
2
Worksheet #1b
6
34
50
2
3
11
43
24
72
Insert 4
1
21
2
4
Worksheet #1b
6
34
50
3
11
43
24
72
Insert 4
1
21
2
4
2
Worksheet #1b
6
34
50
3
11
43
24
72
Insert 4
1
21
2
3
4
2
Worksheet #1b
6
34
50
3
11
43
24
72
Insert 5
1
21
2
3
4
2
5
Worksheet #1b
6
34
50
11
43
24
72
Insert 5
1
21
2
3
4
2
5
3
4
Worksheet #1b
6
34
50
11
43
24
72
Insert 5
1
21
3
4
4
2
5
3
2
Worksheet #1b
6
34
50
11
43
24
72
Insert 5
1
21
4
4
2
5
3
2
3
Worksheet #1b
34
50
11
43
24
72
Insert 5
1
21
4
4
2
5
3
2
3
6
Index Types
How is data stored in the index?
Alternative 1 Index (B+ Tree)
17
5
24
(2, Joe)
(3, Jim)
(5, Kay)
(7, Dan)
(20, Tim)
Root Node
(24, Kit)
Data Entries
Interior Nodes
uid | name |
2 | Joe |
3 | Jim |
5 | Kay |
7 | Dan |
20 | Tim |
24 | Kit |
Alternative 2 Index
uid | name |
2 | Joe |
3 | Jim |
5 | Kay |
7 | Dan |
20 | Tim |
24 | Kit |
(2, Joe)
(3, Jim)
(5, Kay)
(7, Dan)
(20, Tim)
(24, Kit)
17
5
24
(2, [1,1])
(3, [1,2])
(5, [2,1])
(7, [2,2])
(20, [3,1])
Root Node
(24, [3,2])
Data Entries
Interior Nodes
Index File
Index Contains
(Key, Record Id) Pairs
Alternative 3 Index
(2, Joe)
(2, Jim)
(2, Kay)
(3, Dan)
(3, Tim)
(20, Kit)
17
5
24
(2, {[1,1], [1,2], [2, 1]}
(3, {[2,2], [3, 1]})
(20, {3, 2}])
Root Node
…
Data Entries
Interior Nodes
Index File
Index Contains
(Key, {list of record Id}) Pairs
Key | Record Id |
2 | {[1,1], [1,2], [1,3]} |
3 | 4 |
…
Clustering
Clustering
Unclustered
Clustered
I/O Cost:
~ 1 I/O per record
~ 1 I/O per page of records
Note on Full Scans and B+ Trees
Worksheet Q2
Worksheet #2a
Worksheet #2a
Yes, but you would generally have to store two copies of the data (and keep both up to date).
Special case: if the search key for one index is a subset of the search key for the other (e.g. index 1 on sid, index 2 on (sid, name)), then we can have just one copy (sorted on (sid, name)).
Worksheet #2b
Assume the table takes up 12 MB on disk (1 MB = 1024 KB) and that page size is 64KB. (This includes extra space allocated for future insertions.)
We want to scan all the records in Submissions. How many I/Os will this operation take?
CREATE TABLE Submissions (
record_id integer UNIQUE,
assignment_id integer,
student_id integer,
time_submitted integer,
grade_received byte,
comment text,
regrade_request text,
PRIMARY KEY(assignment_id, student_id)
);
CREATE INDEX SubmissionLookupIndex
ON Submissions (assignment_id, student_id);
2. Suppose we have an alternative 2 unclustered index on (assignment_id, student_id) with a height of 3 (one must traverse 3 index pages to reach any leaf page). Here's the schema:
Worksheet #2b
Assume the table takes up 12 MB on disk (1 MB = 1024 KB) and that page size is 64KB. (This includes extra space allocated for future insertions.)
We want to scan all the records in Submissions. How many I/Os will this operation take?
CREATE TABLE Submissions (
record_id integer UNIQUE,
assignment_id integer,
student_id integer,
time_submitted integer,
grade_received byte,
comment text,
regrade_request text,
PRIMARY KEY(assignment_id, student_id)
);
CREATE INDEX SubmissionLookupIndex
ON Submissions (assignment_id, student_id);
2. Suppose we have an alternative 2 unclustered index on (assignment_id, student_id) with a height of 3 (one must traverse 3 index pages to reach any leaf page). Here's the schema:
To do a full table scan, we read each page into memory once. There are 12 * 1024/64 = 192 pages in the table, so that’s 192 I/Os (all page reads).
Worksheet #2c
Assume the table takes up 12 MB on disk (1 MB = 1024 KB) and that page size is 64KB. (This includes extra space allocated for future insertions.)
How many I/Os will this operation take?
UPDATE Students SET grade_received=85 WHERE assignment_id=20 AND student_id=12345;
CREATE TABLE Submissions (
record_id integer UNIQUE,
assignment_id integer,
student_id integer,
time_submitted integer,
grade_received byte,
comment text,
regrade_request text,
PRIMARY KEY(assignment_id, student_id)
);
CREATE INDEX SubmissionLookupIndex
ON Submissions (assignment_id, student_id);
2. Suppose we have an alternative 2 unclustered index on (assignment_id, student_id) with a height of 3 (one must traverse 3 index pages to reach any leaf page). Here's the schema:
Worksheet #2c
Assume the table takes up 12 MB on disk (1 MB = 1024 KB) and that page size is 64KB. (This includes extra space allocated for future insertions.)
How many I/Os will this operation take?
UPDATE Students SET grade_received=85 WHERE assignment_id=20 AND student_id=12345;
Read the page into memory: 3 page reads for the index + 1 page read for the leaf page
+ 1 page read for the data page (b/c alt. 2)
Write the modification in memory and then flush the page back to disk: 1 page write for the data page
This costs us a total of 6 disk I/Os.
CREATE TABLE Submissions (
record_id integer UNIQUE,
assignment_id integer,
student_id integer,
time_submitted integer,
grade_received byte,
comment text,
regrade_request text,
PRIMARY KEY(assignment_id, student_id)
);
CREATE INDEX SubmissionLookupIndex
ON Submissions (assignment_id, student_id);
2. Suppose we have an alternative 2 unclustered index on (assignment_id, student_id) with a height of 3 (one must traverse 3 index pages to reach any leaf page). Here's the schema:
Worksheet #2d
Assume the table takes up 12 MB on disk (1 MB = 1024 KB) and that page size is 64KB. (This includes extra space allocated for future insertions.)
In the worst case, how many I/Os does it take to perform an equality search on grade_received?
CREATE TABLE Submissions (
record_id integer UNIQUE,
assignment_id integer,
student_id integer,
time_submitted integer,
grade_received byte,
comment text,
regrade_request text,
PRIMARY KEY(assignment_id, student_id)
);
CREATE INDEX SubmissionLookupIndex
ON Submissions (assignment_id, student_id);
2. Suppose we have an alternative 2 unclustered index on (assignment_id, student_id) with a height of 3 (one must traverse 3 index pages to reach any leaf page). Here's the schema:
Worksheet #2d
Assume the table takes up 12 MB on disk (1 MB = 1024 KB) and that page size is 64KB. (This includes extra space allocated for future insertions.)
In the worst case, how many I/Os does it take to perform an equality search on grade_received?
In the worst case, any record can match the grade_received predicate. Therefore, we must check every record of the table. This is equivalent to performing a table scan, and we would read every page, requiring 192 page reads.
CREATE TABLE Submissions (
record_id integer UNIQUE,
assignment_id integer,
student_id integer,
time_submitted integer,
grade_received byte,
comment text,
regrade_request text,
PRIMARY KEY(assignment_id, student_id)
);
CREATE INDEX SubmissionLookupIndex
ON Submissions (assignment_id, student_id);
2. Suppose we have an alternative 2 unclustered index on (assignment_id, student_id) with a height of 3 (one must traverse 3 index pages to reach any leaf page). Here's the schema:
Bulkloading
Bulkloading Procedure
Creating a B+ tree from scratch is very costly if we insert every node because we have to traverse from the top of the tree to the bottom each time. Instead, use bulkloading:
Worksheet Q3
Worksheet #3
Suppose we were to create an order d=2 B+ tree via bulk-loading with a fill factor of 3/4. We insert keys with all integer values from 1-16 in order.
Here, fill factor specifies the fill factor for leaves only; inner nodes should be filled up to full and split in half exactly.
Draw out the final B+ tree. What is its height?
1
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
Inserting 1-16 in order into an empty B+ tree
1
2
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
Leaf node has exceeded fill factor of 3/4 (since there are more than 3 entries)
1
2
3
4
4
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
Split leaf node
1
2
3
4
5
4
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
4
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
7
4
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
Leaf node has exceeded fill factor of 3/4 (since there are more than 3 entries)
1
2
3
4
5
6
4
7
7
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
Split leaf node
1
2
3
4
5
6
4
7
7
8
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
4
7
7
8
9
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
4
7
7
8
9
10
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
Leaf node has exceeded fill factor of 3/4 (since there are more than 3 entries)
1
2
3
4
5
6
4
7
10
7
8
9
10
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
Split leaf node
1
2
3
4
5
6
4
7
10
7
8
9
10
11
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
4
7
10
7
8
9
10
11
12
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
4
7
10
7
8
9
10
11
12
13
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
Leaf node has exceeded fill factor of 3/4 (since there are more than 3 entries)
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
Split leaf node
1
2
3
4
5
6
4
7
10
13
7
8
9
10
11
12
13
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
4
7
10
13
7
8
9
10
11
12
13
14
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
4
7
10
13
7
8
9
10
11
12
13
14
15
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
4
7
10
13
7
8
9
10
11
12
13
14
15
16
Leaf node has exceeded fill factor of 3/4 (since there are more than 3 entries)
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
4
7
10
13
7
8
9
10
11
12
13
14
15
Split leaf node
Parent node is full (since there are more than 4 entries)
16
16
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Split parent node into 2 nodes with d = 2 entries each
16
4
7
10
13
16
Bulk Loading: d = 2, f = 3/4 * GREY indicates an INTERMEDIATE step, which is not a valid state
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Split parent node into 2 nodes with d = 2 entries each
16
4
7
13
16
10
Worksheet #3
The final height of the tree would be 2.
Summary
Attendance Link