Motivation
Sometimes, sorting is a bit overkill for the problem. Often, all we want is to group the same value together, but we do not actually care about the order the values appear in (think GROUP BY or de-duplication). In a database, grouping like values together is called hashing. We cannot build a hash table in the standard way you learned in 61B for the same reason we could not use quick sort in the last note: we cannot fit all of our data in memory! Let’s see how to build an efficient out-of-core hashing algorithm.
General Strategy
Because we cannot fit all of the data in memory at once, we’ll need to build several different hash tables and concatenate them together. There is a problem with this idea though. What happens if we build two separate hash tables that each have the same value in them (e.g. “Brian” occurs in both tables)? Concatenating the the tables will result in some of the “Brian”s not being right next to each other.
To fix this, before building a hash table out of the data in memory, we need to guarantee that if a certain value is in memory, all of its occurrences are also in memory. In other words, if “Brian” occurs in memory at least once, then we can only build the hash table if every occurrence of “Brian” in our data is currently in memory. This ensures that values can only appear in one hash table, making the hash tables safe to concatenate.
The Algorithm
We will use a divide and conquer algorithm to solve this problem. During the “divide” phase we perform partitioning passes, and during the “conquer” phase we construct the hash tables. Just like in the sorting note, we will assume that we have \(B\) buffer frames available to us. As we hash our data, we will use \(1\) of these buffers as an input buffer, and \(B-1\) of these buffers as output buffers.
We begin by streaming data from disk into our \(1\) input buffer. Once we have the input data, we want to break it into partitions. A partition is a set of pages such that for a particular hash function, the values on the pages all hash to the same hash value. The first partitioning pass will hash each record in the input buffer into one of \(B-1\) partitions, with each partition corresponding to one of our \(B-1\) output buffers. If an output buffer fills up, that page is flushed to disk, and the hashing process continues. Any flushed pages that came from the same buffer will be located adjacent to each other on disk. At the end of the first partitioning pass, we have \(B-1\) partitions stored on disk, with each partition’s data stored continuously.
One important property of partitions is that if a certain value appears in one partition, all occurrences of that value in our data will be located in the same partition. This is because identical values will be hashed to the same value. For example, if “Brian” appears in one partition, “Brian” will not appear in any other partition.
After the first partitioning pass, we have \(B-1\) partitions of varying sizes. For partitions that can fit in memory (the partition’s size is less than or equal to \(B\) pages), we can go right into the “conquer” phase, and begin building hash tables. For the partitions that are too big, we need to recursively partition them using a different hash function than we used in the first pass. Why a different hash function? If we reused the original function, every value would hash to its original partition so the partitions would not get any smaller. We can recursively partition with different hash functions as many times as necessary until all of the partitions have at most B pages.
Once all of our partitions can fit in memory, and all of the same values occur in the same partition (by the property of partitions), we can begin the the “conquer” phase. First we select a new hash function for the purpose of building our final hash table. Then, for each partition, we stream the partition into memory, create a hash table using the new hash function, and flush the resulting hash table back to disk.
Drawbacks of Hashing
It is important to note that hashing is very susceptible to data skew. Data skew occurs when the values that we are hashing do not follow a uniform distribution. Due to the property of hashed partitions (that all hashes of the same value will end up in the same partition), data skew can lead to very unevenly-sized partitions, which is sub-optimal for our purposes. Additionally, not all hash functions are created equal. In the optimal case, our hash function is a uniform hash function, which partitions the data into "uniform", equal-sized partitions. However, hash functions are often non-uniform, and partition data unevenly across all of the partitions. For the purposes of this class, we will be using uniform hash functions unless otherwise stated. In the two images below, cylinders represent steps that happen on disk, squares represent steps that happen in memory, and the lines between the two represent data streaming from disk to memory and vice versa.
The image below shows the two phases of Hashing - divide and conquer - when recursive partitioning isn’t required. This means that after initial partitioning, all partitions fit within \(B\) pages. Notice that in the Divide phase, \(1\) buffer page is dedicated to the input buffer and the remaining \(B-1\) are used for partitioning. Afterwards, in the Conquer phase, a separate hash function \(h_r\) is used on each individual partition to form in-memory hash tables.

The image below shows recursive partitioning. Notice that the additional phases only apply to the partition with size greater than \(B\) pages (grey partition).

Example
In the following example, we will assume that we have \(B=3\) buffer pages available to us. We also assume that Brian and Eric hash to the same value for the first hash function but different values for the second hash function, and Jamie and Lakshya hash to the same value for the first hash function.

After the first partitioning pass (Partitioning Pass 1), Partition 0 is 4 pages, and Partition 1 is 2 pages. Partition 1 fits in memory, but Partition 0 does not. This means we must recursively partition Partition
- After Partitioning Pass 2, Partition 0 is broken into two parts: Partition 0.a is 2 pages, and Partition 0.b is 2 pages. As Partition 1 already fit into memory, it was not partitioned during Partitioning Pass 2
- Once all partitions (0.a, 0.b, 1) fit into memory, we perform the Conquer Pass and build the hash tables. You can see that after the final Conquer Pass, all of the like values are next to each other, which is our end goal.
Analysis of External Hashing
We’re not going to be able to create a simple formula to count the number of I/Os like in the sorting algorithm because we do not know how large the partitions will be. One of the first things we need to recognize is that it is possible for the number of pages in the table to increase after a partitioning pass. To see why, consider the following table in which we can fit two integers on a page:
[1, 2] [1, 4] [3, 4]
Let’s assume B=3, so we only divide the data into 2 partitions. Let’s assume 1 and 3 hash to partition 1, and 2 and 4 hash to partition 2. After partitioning, partition 1 will have:
[1, 1], [3]
And partition 2 will have:
[4, 2], [4]
Notice that we now have 4 pages when we only started with 3. Therefore, the only reliable way to count the number of I/Os is to go through each pass and see exactly what will be read and what will be written. Let \(m\) be the total number of partitioning passes required, let \(r_i\) be the number of pages you need to read in for partitioning pass \(i\), let \(w_i\) be the number of pages you need to write out of partitioning pass \(i\), and let \(X\) be the total number of pages we have after partitioning that we need to build our hash tables out of. Here is a formula for the number of I/Os:
\[(\sum_{i=1}^m r_i + w_i) + 2X\]The summation doesn’t tell us anything that we didn’t already know; we need to go through each pass and figure out exactly what was read and written. The final \(2X\) part, says that in order to build our hash tables, we need to read and write every page that we have after the partitioning passes.
Here are some important properties:
- \[r_0 = N\]
- \[r_i \le w_i\]
- \[w_i \ge r_{i+1}\]
- \[X \ge N\]
Property 1 says that we must read in every page during the first partitioning pass. This comes straight from the algorithm.
Property 2 says that during a partitioning pass we will write out at least as many pages as we read in. This comes directly from the explanation above - we may create additional pages during a partitioning pass.
Property 3 says that we will not read in more pages than what we wrote out during the partitioning pass before. In the worst case, every partition from pass \(i\) will need to be repartitioned, so this would require us to read in every page. In most cases, however, some partitions will be small enough to fit in memory, so we can read in fewer pages than we produced during the previous pass.
Property 4 says that the number of pages we will build our hash table out of is at least as big as the number of data pages we started with. This comes from the fact that the partitioning passes can only increase the number of data pages, not decrease them.
Practice Questions
1) How many IOs does it take to hash a 500 page table with B = 10 buffer pages? Assume that we use perfect hash functions that distribute values evenly across all partitions.
2) We want to hash a 30 page table using B=6 buffer pages. Assume that during the first partitioning pass, 1 partition will get 10 data pages and the rest of the pages will be divided evenly among the other partitions. Also assume that the hash function(s) we use for recursive partitioning are perfect. How many IOs does it take to hash this table?
3) If we had 20 buffer pages to externally hash elements, what is the minimum number of pages we could externally hash to guarantee that we would have to use recursive partitioning?
4) If we have B buffer pages, and the table is \(B^2\) pages large, how many passes do we need to hash the table? Assume that we use perfect hash functions.
5) If we have 10 buffer pages, what is the minimum size of the table in pages, such that it needs at least two partitioning passes when hashing (i.e. at least three passes over the data)?
Solutions
1) The first partitioning pass divides the 500 pages into 9 partitions. This means that each partition will have 500 / 9 = 55.6 \(\implies\) 56 pages of data since we round up to full pages. We had to read in the 500 original pages, but we have to write out a total of 56 * 9 = 504 because each partition has 56 pages and there are 9 partitions. The total number of IOs for this pass is therefore 500 + 504 = 1004.
We cannot fit any partition into memory because they all have 56 pages, so we need to recursively partition all of them. On the next partitioning pass each partition will be divided into 9 new partitions (so 9*9 = 81 total partitions) with 56 / 9 = 6.22 \(\implies\) 7 pages each. This pass needed to read in the 504 pages from the previous pass and write out 81 * 7 = 567 pages for a total of 1071 IOs.
Now each partition is small enough to fit into memory. The final conquer pass will read in each partition from the previous pass and write it back out to build the hash table. This means every page from the previous pass is read and written once for a total of 567 + 567 = 1134 IOs.
Adding up the IOs from each pass gives a total of 1004 + 1071 + 1134 = 3209 IOs
2) There will be 5 partitions in total (B-1). The first partition gets 10 pages, so the other 20 will be divided over the other 4 partitions, meaning each of those partitions gets 5 buffer pages. We had to read in all 30 pages to partition them, and we write out 5 * 4 + 10 = 30 pages as well, meaning the first partitioning pass takes 60 IOs.
The partitions that are 5 pages do not need to be recursively partitioned because they fit in memory. The partition that is 10 pages long, however, does. This partition will be repartitioned into 5 new partitions of size 10/5 = 2 pages. We read in all 10 pages for this partition, and we wrote out 5 * 2 = 10 pages, meaning that this recursive partitioning pass takes 20 IOs.
The final conquer pass needs to read in and write out every partition, so it will take 4 * 5 * 2 = 40 IOs to conquer the partitions that did not need to be repartitioned and 5 * 2 * 2 = 20 IOs to conquer the partitions that were created from recursive partitioning, for a total of 60 IOs.
Finally, add up the IOs from each pass and get a total of 60 + 20 + 60 = 140 IOs.
3) 381 pages. Since B = 20, we can potentially hash up to B * (B - 1) = 20 * (20 - 1) = 380 pages without doing recursive partitioning. This is because we have \(B-1\) partitions in the first pass and would like each partition to fit into our buffer of size \(B\). If we want to guarantee recursive partitioning, we need one more page then this, giving us 381 pages as our solution.
Even with a perfect hash function, one page would have B+1 pages, which would require recursive partitioning since B+1 can’t fit into memory when there are B buffer pages.
4) 3 passes. We need two partitioning passes and conquer pass.
We get partitions of size \(\frac{B^2}{B-1} > B\) after one pass, and partitions of size \(\frac{B^2}{\left(B-1\right)^2}\) after two passes, which is less than B for \(B\) \(\geq\) \(3\), the minimum required for hashing.
5) 91 pages. We can hash at most \((B-1)*B\) pages with one partitioning pass, which is \(90\) pages when \(B=10\).
Past Exam Questions
Fa22 Midterm 1 Q2: Hashing (19.5 points)
(a) Inventory (15 points)
Suppose we have the following table:
CREATE TABLE Inventory (
productid INTEGER PRIMARY KEY,
category VARCHAR NOT NULL,
quantity INT NOT NULL
);
This table contains 100,000 records split across 700 pages. For all parts, assume we have 10 buffer pages. Also assume that the conquer (build) phase of hashing is always performed.
For the first 10 parts (part i - part x) only, assume we are partitioning the table on productid and that the first partitioning pass uses an uneven hash function that places 210 pages in the first partition, 105 pages in each of the second and third partitions, and evenly spreads the remaining pages among the other partitions. All following passes after the first make use of independent, perfectly uniform hash functions.
i. (0.5 pt) How many pages will be read in the first partitioning pass?
Solution
700
The table contains 700 pages, all of which will be read in the first partitioning pass.
ii. (0.5 pt) How many pages will be written in the first partitioning pass?
Solution
702
The first three partitions have 210, 105, and 105 pages written out, respectively. Per the question, the remaining 280 pages will be split among the remaining 6 partitions, each of which will have ceil(280 / 6) = 47 pages.
210 + (105*2 + 47*6) = 702 IOs
iii. (1 pt) Consider the first partition generated by the first pass. How many additional (not including the first) partitioning passes will we need to fully hash this partition?
Solution
2
The first partition has 210 pages. The second partitioning pass will create partitions of size ceil(210/9) = 24 pages. This is still larger than B=10, so an additional partitioning pass will have to be performed on the first partition.
Partial credit (0.25 points) was given for the answer of 3 which included the conquer pass.
iv. (1 pt) Now consider the second partition generated by the first pass. How many additional (not including the first) partitioning passes will we need to fully hash this partition?
Solution
2
The second partition has 105 pages. The second partitioning pass will create partitions of size ceil(105/9) = 12 pages. This is still larger than B=10, so an additional partitioning pass will have to be performed on the second partition.
Partial credit (0.25 points) was given for the answer of 3 which included the conquer pass.
v. (1 pt) Now consider the fourth partition generated by the first pass. How many additional (not including the first) partitioning passes will we need to fully hash this partition?
Solution
1
The fourth partition has 47 pages. The second partitioning pass will create partitions of size ceil(47/9) = 6 pages. This is less than or equal to B=10, so no further partitioning passes will be required.
Partial credit (0.25 points) was given for the answer of 2 which included the conquer pass.
vi. (2 pt) In total, across all applicable partitions, how many additional IOs will the second partitioning pass take? Don’t include IOs from writing the output of the first pass to disk.
Solution
1458
The output of the first partitioning pass must first be read in, for a total of 702 IOs. The first partition will be split into 9 partitions of ceil(210 / 9) = 24 pages. The second and third partitions will each be split into 9 partitions of ceil(105 / 9) = 12 pages. The remaining six partitions will each be split into 9 partitions of ceil(47 / 9) = 6 pages. All these new partitions must be written out.
702 + (24*9 + 2*12*9 + 6*6*9) = 1458 IOs
vii. (2 pt) In total, across all partitions, how many additional IOs will any remaining partitioning passes take (if there are any left)? Don’t include IOs from writing the output of the second pass to disk.
If you are not confident in your answers to previous parts, for quarter credit, assume the second partitioning pass returned 27 partitions in which the first three partitions each have 21 pages, the next three each have 15 pages, the next two each have 10 pages, and the rest each have 6 pages.
Solution
999
Any partition of more than B=10 pages from the output of the second partitioning pass (and only these partitions) must be recursively partitioned again. This encompasses 9 partitions of 24 pages each and 18 partitions of 12 pages each. First, read in all these partitions, for a total of 24 * 9 + 2 * 12 * 9 = 432 IOs. The 24 page partitions (of which there are 9) will each be split into 9 partitions of ceil(24 / 9) = 3 pages. The 12 page partitions (of which there are 18) will each be split into 9 partitions of ceil(12 / 9) = 2 pages. All these new partitions must be written out.
(24*9 + 2*12*9) + (3*9*9 + 2*2*9*9) = 999 IOs
The quarter credit option has an answer of 243 IOs.
viii. (2 pt) How many IOs will the conquer pass take?
If you are not confident in your answers to previous parts, for quarter credit, assume the final result of the partitioning stage is 115 partitions with the first ten partitions each having 8 pages, the next seven each having 7 pages, the next six each having 5 pages, and the rest each having 3 pages.
Solution
1782
Every partition resulting from the partitioning passes has to be read in and written out. From the second partitioning pass there were 6*9 partitions of 6 pages each. From the third partitioning pass there were 9*9 partitions of 3 pages each and 2*9*9 partitions of 2 pages each.
2 * (6*6*9 + 3*9*9 + 2*2*9*9) = 1782 IOs
The quarter credit option has an answer of 870 IOs.
ix. (1 pt) Adding everything up, how many IOs will this overall hashing process take from start to finish?
Solution
5641
x. (1 pt) In order to successfully perform this entire hashing process from the previous parts, how many additional hash functions will we need, not including the one used for the first partitioning pass?
Solution
3
Every partitioning pass and the conquer pass each need their own hash function. Thus, not including the first partitioning pass, there are 2 more partitioning passes and 1 conquer pass. This means a total of 3 additional hash functions are needed.
xi. (1 pt) Assuming we now have access to independent, perfectly uniform hash functions for every pass, including the first. How many IOs will it take to hash this table on productid?
Solution
4291
In the first partitioning pass, the entire table (700 pages) is read in. It is split into 9 partitions of size ceil(700 / 9) = 78 pages. These partitions must be written out to complete the first partitioning pass, which totals 700 + 78*9 = 1402 IOs.
In the second partitioning pass, the previous partitions (each of which has 78 pages) are all read in. They are each split into 9 partitions of ceil(78 / 9) = 9 pages. These partitions must then be written out to complete the second partitioning pass, which totals 78*9 + 9*9*9 = 1431 IOs.
All partitions are now less than or equal to B=10 pages, so the conquer phase can begin. Read in every partition that resulted from the partitioning stage, construct the in-memory hash table, and then write it back out. This totals 2 * (9*9*9) = 1458 IOs.
(700 + 78*9) + (78*9 + 9*9*9) + 2 * (9*9*9) = 4291 IOs
xii. (1 pt) Assuming we again have access to independent, perfectly uniform hash functions for every pass, what is the maximum number of pages we can hash using at most 4 distinct hash functions if we are hashing on productid?
Solution
7290
The conquer pass and every partitioning pass each require their own hash function. Thus, given 4 distinct hash functions, 3 partitioning passes and 1 conquer pass can be performed. Each partitioning pass splits each of the input partitions into 9 partitions, and the conquer pass can operate over partitions of up to 10 pages in size.
9 * 9 * 9 * 10 = 7290 pages
xiii. (1 pt) Assuming we again have access to independent, perfectly uniform hash functions for every pass, what is the maximum number of pages we can hash using at most 4 distinct hash functions if we are now hashing on category? Assume that category takes on one of 5 unique values and has a uniform distribution.
Solution
50
The conquer pass can operate on partitions of sizes up to 10 pages each. Thus, since keys with the same value are never split into different partitions by hashing, in the best case, each of the 5 unique values will be placed into its own partition. As long as each of these partitions has up to 10 pages, hashing can be performed as is.
Thus, up to 5 * 10 pages of data can be hashed using 1 partitioning pass and 1 conquer pass. Under this best-case scenario, the first partitioning pass will place each unique value into its own partition, all of which will be exactly 10 pages (and thus can be processed by a conquer pass with B=10).
(b) More hashing questions (4.5 points)
i. (2 pt) Consider the external hashing algorithm presented in lecture. Will the algorithm terminate if we use the same hash function for all partitioning passes? If so, write “yes” below. Otherwise, write “no” and explain in one sentence (less than 25 words) why not.
Solution
No, every record would get put back into the same bucket as in the previous pass.
ii. (2.5 pt) For which of the following tasks can hashing be used to implement? There can be zero, one, or more than one correct choice. In the case that there are zero correct choices, mark F. None of the above.
- A. Ordering a table by a key
- B. Grouping a table by a key
- C. Removing duplicates from a table
- D. Filtering a table on an arbitrary condition
- E. Creating a lookup table to quickly find records with a key
- F. None of the above
Solution
B, C, and E.
- A: False. Hashing arbitrarily orders data onto partitions.
- B: True. Hashing groups data with the same value, so it can be used for GROUP BYs.
- C: True. Hashing groups data with the same value, so it can be used to quickly find (and remove) duplicate values.
- D: False. Hashing cannot filter data on arbitrary predicates (such as inequalities).
- E: True. Once the data is hashed, individual values can be quickly looked up by pulling the relevant partition into memory.
Fa21 Midterm 1 Q5: Hashing (15 points)
We have a file with N=100 pages and a machine with B=5 buffer pages, and we want to hash our file. For the first pass, you have a strange hash function. The hash function used sends half the data to the first bucket. Of the remaining data, 60% goes to the second bucket. The remaining buckets split the remainder of the data equally. Following that, you use a (different) uniform hash function for each of the remaining passes. Assume that all the data in the file is unique with no duplicates on the key being hashed on.
(a) (0.5 pt) How many I/Os are performed when you read in the data pages for the first time?
Solution
100
(b) (0.5 pt) How many partitions are created?
Solution
4
(c) (0.5 pt) How many I/Os are performed in writing these partitions to disk?
Solution
100
(d) (0.5 pt) How many pages went into the largest partition?
Solution
50
½, 3/10, 1/10, 1/10 is the distribution function, meaning you get [50, 30, 10, 10] as the pages per bucket.
(e) (1 pt) How many more passes will it take to hash this partition, including the final build phase?
Solution
3
50 pages get split into 4 partitions of 13. Each partition of 13 is broken into 4 partitions of 4. An in-memory hash table is constructed on each of those 4-page partitions.
(f) (3 pt) How many I/Os will it take to hash this partition after it was initially created/written to disk?
Solution
346
50 pages get split into 4 partitions of 13. Each partition of 13 is broken into 4 partitions of 4. An in-memory hash table is constructed on each of those 4-page partitions. So our calculation would be ((50) + 13 * 4) + 4 * ((13 + 4 * 4) + 4 * (4 + 4)) = 346
(g) (0.5 pt) We’re done with calculating I/Os for hashing the first partition. Let’s move on to the second partition. How many pages will go into this partition?
Solution
30
½, 3/10, 1/10, 1/10 is the distribution function, meaning you get [50, 30, 10, 10] as the pages per bucket.
(h) (1 pt) How many more passes does it take to hash this partition, including the final build phase?
Solution
3
30 pages get split into 4 partitions of 8. Each partition of 8 gets split into 4 partitions of 2. An in-memory hash table is constructed on each of those 2-page partitions in the third pass.
(i) (3 pt) How many I/Os will it take to hash this partition after it was initially created/written to disk?
Solution
190
30 pages get split into 4 partitions of 8. Each partition of 8 gets split into 4 partitions of 2. An in-memory hash table is constructed on each of those 2-page partitions in the third pass. So our calculation would be (30 + 8 * 4) + 4 * (8 + 2 * 4 + 4 * (2 + 2)) = 190
(j) (0.5 pt) We’re done with calculating the I/Os for the second partition. Now, let’s deal with the remaining partitions. How many pages go into each of the remaining partitions?
Solution
10
(k) (1 pt) How many passes does it take to hash one of these partitions, including the final build phase?
Solution
2
10 pages get split into 4 partitions of 3, and each of those mini-partitions has an in-memory hash table built on it.
(l) (2 pt) How many I/Os will it take to hash one of these partitions after it was initially created and written to disk at the end of pass 0?
Solution
46
10 pages get split into 4 partitions of 3, and each of those mini-partitions has an in-memory hash table built on it. Thus, our calculation is (10 + 3 * 4) + 4 * (3 + 3) = 46
(m) (1 pt) Now add them all up. How many total I/Os will it take to hash this file, from start to finish?
Solution
828
100 + 100 + 346 + 190 + 46 * 2 = 828
Fa21 Final Q5: Dead Week, Dead Computers (16 points)
Dylan knocked over 6 computers in Soda by accident. These computers stored tables of students’ name and major in each of 6 of UC Berkeley’s colleges — each computer stored one table corresponding to a college. The tables were in alphabetical order based on name. The computers are destroyed but their disk drives are fine, so Dylan hooks them up to the only working computer left. The computer’s operating system is unable to distinguish between each drive, so all the data pages have been mixed up into one heap file. However, the records remain in the same order on each page.
Here is information on each drive:
- Six drives: Optometry, Engineering, L&S, Haas, Law, Journalism
- Each drive has one file on it which represents the table, file sizes are:
- Optometry: 70 pages
- Engineering: 70 pages
- L&S: 70 pages
- Haas: 70 pages
- Law: 70 pages
- Journalism: 70 pages
- One page contains records for 10 students
(a) Restore Tables
The tables need to be restored before tomorrow so that they can be uploaded to the university website.
i. (4 pt) How can you use sorting and hashing to re-create the original six tables (before Dylan mixed them up)? Assume that you are able to access the major for a given name and that majors are unique to each college (i.e. the computer science major will only be in one college). Please be specific and clear about your approach.
Solution
Create a hash algorithm that uses major to identify which college the person is in. Use the external hash algorithm to partition names/majors into six buckets. Then sort each bucket individually. (Manually graded.)
ii. (2 pt) If you stated you’d use a hash function in your answer to the previous question, would the function have to be uniform? Select N/A if you don’t believe a hash function is necessary.
- Yes
- No
- N/A
Solution
No. The function itself isn’t uniform – if we had an uneven number of pages across colleges, we wouldn’t see uniform hashing.
(b) Major Grouping (10 points)
You would like to group students by major and decide to use hashing for this. There are 5 majors in each college. You have 31 buffer pages (B = 31). Assume all pages are full, and all majors are distinct.
i. (5 pt) What is the I/O cost to hash all of the students into partitions based on their majors? There should be one partition for each major. Assume you have access to any hash functions you need and students are evenly distributed across majors. Show your work.
Solution
1680
Note: The wording for this question was poor. It should’ve asked: what is the cost to hash all of the data pages? Additionally, since you can hash the majors into partitions such that each partition only has one major, you can skip the conquer phase – so we also gave full credit to 840 I/Os.
- Total # pages: 420
- Total # partitions: 30
- 420 / 30 = 30 partitions of 14 pages each
- Divide Phase: 420 (Read) + 420 (Write)
- Conquer Phase: 420 (Read) + 420 (Write)
- 1680 I/Os
ii. (5 pt) However, some students have two majors and thus need to be hashed into multiple buckets. Therefore, you have modified the original hashing algorithm to allow keys to hash to multiple buckets (your hash function will do this). Given that 300 students across all 6 colleges are doing double majors, what is the cost of hashing all the data pages? Assume 5 majors for each college, 31 buffer pages (B = 31), students are evenly distributed across majors (including second majors), and you have any hash functions you need. Show your work.
Solution
1770
Note: See note under previous question. For the same reasoning, 870 I/Os also received full credit here.
- Total # pages: 420
- Total # partitions: 30
- 300 students / 10 = 30 pages extra will be produced
- 420 / 30 = 30 partitions of 14 pages each, add 1 page to each partition for second major, so 15 pages per partition
- Divide Phase: 420 (Read) + 450 (Write)
- Conquer Phase: 450 (Read) + 450 (Write)
- 1770 I/Os
Sp21 Final Q9: Sorted Eggs and Hash Browns (16 points)
(a) I/Os and Dollars (3.5 points)
Your company is switching compute plans, and they need your help to figure out the most cost-efficient way to process your data. Each buffer page costs $10, but the cost of each IO is variable. You’re given a choice between three plans.
- Plan 1: 100 buffer pages, $0.01 per IO
- Plan 2: 50 buffer pages, $0.05 per I/O
- Plan 3: 10 buffer pages, $0.25 per I/O
Your company decides to go with a trial plan first and gives a small table to sort to see how it goes. With a file size of N=100 pages…
i. (1 pt) In sorting the file, how many I/Os are incurred under plan 1?
Solution
200
This will take 1 pass, so it will take 1 * 2 * 100 = 200 I/Os.
ii. (1 pt) How many I/Os are incurred under plan 2?
Solution
400
This will take 2 passes and will incur 2 * 2 * N = 400 I/Os.
iii. (1 pt) How many I/Os are incurred under plan 3?
Solution
600
This will take 3 passes (1 + ceil(log910) = 3) and will incur 3 * 2 * N = 600 I/Os.
iv. (0.5 pt) What plan is cheapest for the N=100 pages scenario?
- PLAN 1
- PLAN 2
- PLAN 3
Solution
PLAN 3
Plan 1 costs 1002 dollars, Plan 2 costs 520 dollars, and plan 3 costs 250 dollars. So plan 3 is cheapest.
(b) (3.5 points)
Your company is happy with the results and decides to now try sorting a file size of N=10,000 pages.
i. (1 pt) How many I/Os are incurred under plan 1?
Solution
60000
Number of passes: 1 + ceil(log99(10,000/100)) = 3. This will take 3 * 2 * 10,000 = 60,000 I/Os.
ii. (1 pt) How many I/Os are incurred under plan 2?
Solution
60000
Number of passes: 1 + ceil(log49(10,000/50)) = 3. This will take 3 * 2 * 10,000 = 60,000 I/Os.
iii. (1 pt) How many I/Os are incurred under plan 3?
Solution
100000
Number of passes: 1 + ceil(log9(10,000/10)) = 5. This will take 2 * 5 * 10,000 = 100,000 I/Os.
iv. (0.5 pt) What plan is cheapest for the N=10,000 pages scenario?
- PLAN 1
- PLAN 2
- PLAN 3
Solution
PLAN 1
Plan 1 costs $600 + $1000 = $1600. Plan 2 costs $3000 + $500 = $3500. Plan 3 costs $42,500 + $100, so the cheapest is easily Plan 1.
(c) (7 points)
Your company also inquires about hashing a smaller table of theirs, where N=100. Assume uniform hashing with a perfect hash function.
i. (2 pt) How many I/Os will this take to hash this table under Plan 1?
Solution
200
This takes 1 pass, so it will take 1 * 2 * 100 = 200 I/Os.
ii. (2 pt) How many I/Os does it take under Plan 2?
Solution
541
On the first pass, we read in 100 pages and hash it into 49 buckets each, so each bucket gets 3 pages (rounding up). This gives us 100 + 49 * 3 = 247 I/Os for pass 1. Pass 2 is where in-memory hash tables are built, so you read in and write out 49 * 3 pages each, which is 147 * 2 = 294 I/Os for pass 2. Thus, this takes 247 + 294 = 541 I/Os.
iii. (2 pt) How many I/Os does it take under Plan 3?
Solution
802
On the first pass, we read in 100 pages and hash it into 9 buckets. Each bucket gets 12 pages, so 100 + 9 * 12 = 208 I/Os for pass 1. The second pass takes in each partition of 12 pages and hashes it into 9 buckets, so each sub-partition gets 2 pages (we thus write out 18 pages for every 12 we read in). Thus, this pass takes 12 * 9 + 18 * 9 = 270 I/Os. The last pass is the in-memory hash table, so we read in and write out 18 * 9 pages each, giving us 2 * 18 * 9 = 324 I/Os. Thus, the total cost is 324 + 270 + 208 = 802 I/Os.
iv. (1 pt) What is the cheapest plan?
- PLAN 1
- PLAN 2
- PLAN 3
Solution
PLAN 3
Plan 1 costs 100 * 10 + 200 * 0.01 = $1002. Plan 2 costs 451 * 0.05
- 50 * 10 = $522.55. Plan 3 costs 802 * 0.25 + 10 * 10 = $300.50, so Plan 3 is the cheapest.
(d) True or False (2 points)
i. (2 pt) Mark all statements that are true.
- Hashing will always result in the same number of I/Os writing to disk as the I/Os reading from disk.
- Sorting will always result in the same number of I/Os writing to disk as the I/Os reading from disk.
- You can only build an in-memory hash table if you have B-1 pages as your partition file size, where B is the number of pages in the buffer.
- Assuming uniform data spread and no key skew, with B pages of memory and a file size of N pages, hashing a table will always be at least as costly as sorting it.
- None of the above
Solution
- Sorting will always result in the same number of I/Os writing to disk as the I/Os reading from disk.
- Assuming uniform data spread and no key skew, with B pages of memory and a file size of N pages, hashing a table will always be at least as costly as sorting it.