Question
For the purpose of tuning your database that has begun to slow down for some queries, you are looking for the best plan to execute the following queries on the relation described below:
The relation you are dealing with is Employee with attributes ename, title, dname, and address; all are string fields of the same length.
The ename attribute is a candidate key.
The relation contains 10,000 pages.
There are 10 buffer pages.
(When answering the questions, make sure to describe the plan you have in mind.)
The first query is:
SELECT E.title, E.ename FROM Employee E WHERE E.title=‘Administrator’
Assume that only 10% of Employee tuples meet the selection condition.
Suppose that a clustered B+ tree index on ename is (the only index) available. What is the cost of the best plan?
Suppose that a clustered B+ tree index on title is (the only index) available. What is the cost of the best plan?
Explanation / Answer
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Vector; class BPlusTree { private static Node tree; private static int degree; private static boolean debug; private BPlusTree(int x) { // a B+ Tree must have an initial degree degree = x; // The initial type of Node for a B+Tree is a leaf tree = new LeafNode(degree); debug = false; } private static void executeCommand(Command c, BufferedWriter output) throws InvalidCommandException, IOException { // execute command, does as it says, calls the appropriate procedure to accomplish the command // There are also some debug options to help the user see what's going on switch( (int) c.getCommand() ) { case 'd': if( c.getXValue() == 1 && !debug) { debug = true; System.out.println("ENTERING DEBUG MODE"); } else if ( c.getXValue() == 0 && debug) { debug = false; System.out.println("EXITTING DEBUG MODE"); } else if (c.getXValue() != 0 || c.getXValue() != 1){ throw new InvalidCommandException("Invalid Operand with command d. Must be 0 or 1."); } case 'p': if(debug) { System.out.println("PRINTING TREE"); } printTree(output); break; case 's': if(debug) { System.out.println("SEARCHING TREE FOR x = " + c.getXValue()); } searchTree(c.getXValue(), output); break; case 'i': if(debug) { System.out.println("INSERTING x = " + c.getXValue() + " INTO THE TREE"); } insertIntoTree(new DataNode(c.getXValue())); break; } if(debug && (int)c.getCommand() != 'p') { printTree(new BufferedWriter(new PrintWriter(System.out))); System.out.println("--->OPERATION COMPLETE"); } } private static void insertIntoTree(DataNode dnode) { tree = tree.insert(dnode); } private static void searchTree(int x, BufferedWriter output) throws IOException { // search the tree starting from the top if( tree.search(new DataNode(x)) ) { output.write("FOUND" + System.getProperty("line.separator")); } else { output.write("NOT FOUND" + System.getProperty("line.separator")); } } @SuppressWarnings("unchecked") private static void printTree(BufferedWriter output) throws IOException { // create a vector to store all the nodes from each level as we Vector nodeList = new Vector(); // put the root of the tree onto the stack to start the process nodeList.add(tree); boolean done = false; while(! done) { // this vector will hold all the children of the nodes in the current level Vector nextLevelList = new Vector(); String toprint = ""; // for each node in the list convert it to a string and add any children to the nextlevel stack for(int i=0; i