Build a Database from Scratch
- 25Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
How does MySQL organize databases and tables on the file system?
How does MySQL organize databases and tables on the file system?
25 questions
- 1
How does MySQL organize databases and tables on the file system?
- A
It creates a single file for all databases and tables
- B
It creates a folder for each database and a file for each table inside that folder
- C
It creates a folder for each table inside a database folder
- D
It stores all data in memory without writing to disk
Correct answer
BIt creates a folder for each database and a file for each table inside that folder
Explanation
MySQL (or its storage engine) creates a folder for each database and a file for each table inside that folder, as described in the transcript.
- A
- 2
What is a major disadvantage of using CSV format to store a database table in a file?
- A
CSV format does not support variable-length strings
- B
CSV cannot store integer values
- C
CSV files can only store up to 255 rows
- D
CSV requires delimiters, making it impossible to jump directly to a specific row without linear scanning
Correct answer
DCSV requires delimiters, making it impossible to jump directly to a specific row without linear scanning
Explanation
CSV uses delimiters between columns and rows, so finding a specific row requires scanning byte by byte for delimiters, which is inefficient.
- A
- 3
When using fixed-size rows with a string column limited to 255 characters, how many bytes are allocated for the string's length?
- A
255 bytes
- B
2 bytes
- C
1 byte
- D
260 bytes
Correct answer
C1 byte
Explanation
One byte can store values up to 255, which is why the length field is 1 byte for strings up to 255 characters.
- A
- 4
What is the time complexity of searching for a primary key using fixed-size rows without an index?
- A
O(n2)
- B
O(logn)
- C
O(1)
- D
O(n)
Correct answer
DO(n)
Explanation
Without an index, searching requires scanning rows sequentially, which is O(n).
- A
- 5
What problem arises when using a normal binary tree as an index if primary keys are inserted in ascending order?
- A
The tree degrades into a linked list, making search O(n)
- B
The tree requires more memory than a B-Tree
- C
The tree becomes perfectly balanced
- D
The tree cannot be stored on disk
Correct answer
AThe tree degrades into a linked list, making search O(n)
Explanation
Inserting keys in ascending order into a normal binary tree results in a skewed tree (effectively a linked list), losing the O(logn) search advantage.
- A
- 6
In a B-Tree with a minimum children parameter C=2, what is the maximum number of keys each node can hold?
- A
2
- B
5
- C
3
- D
4
Correct answer
C3
Explanation
The maximum number of keys in an internal node is 2Cā1=3 when C=2.
- A
- 7
During B-Tree insertion, when a node becomes full, what is the first step in the self-balancing process?
- A
Split the node into two nodes
- B
Create a new root node
- C
Shift all keys to the right
- D
Move the median key to the parent node
Correct answer
DMove the median key to the parent node
Explanation
When a node is full, the median key is moved to the parent (or a new parent is created if at the root), and then the node is split into two nodes.
- A
- 8
Why are B-Tree nodes stored in disk blocks or pages instead of reading individual bytes?
- A
To reduce CPU usage
- B
To decrease the size of the index
- C
To make binary search possible
- D
Because disks can only read data in blocks (e.g., 4096 bytes at a time)
Correct answer
DBecause disks can only read data in blocks (e.g., 4096 bytes at a time)
Explanation
Disk I/O operates in blocks (e.g., 512 or 4096 bytes); reading a block at a time is more efficient than reading smaller units.
- A
- 9
What type of storage does SQLite use for tables?
- A
Index-organized storage where tables are stored as B-Trees sorted by primary key
- B
Flat file storage with CSV format
- C
Log-structured merge tree storage
- D
Heap storage with separate index files
Correct answer
AIndex-organized storage where tables are stored as B-Trees sorted by primary key
Explanation
SQLite uses index-organized storage: tables are stored in B-Trees indexed by the primary key, and each row is a cell in the B-Tree.
- A
- 10
In a slotted page, where are the variable-length records (cells) stored?
- A
In a separate overflow area
- B
At the end of the page, growing leftward
- C
At the beginning of the page, growing rightward
- D
In a fixed array in the middle of the page
Correct answer
BAt the end of the page, growing leftward
Explanation
Slotted pages store cells at the end of the page, growing leftward, while the slot array at the beginning grows rightward.
- A
- 11
What is the primary advantage of using a slot array in a slotted page for binary search?
- A
The slot array is sorted by key, allowing binary search with fixed-size entries
- B
The slot array eliminates the need for child pointers
- C
The slot array reduces the page size
- D
The slot array stores data in compressed format
Correct answer
AThe slot array is sorted by key, allowing binary search with fixed-size entries
Explanation
The slot array contains fixed-size offsets to cells, enabling binary search to find a key without scanning variable-length cell data.
- A
- 12
How does an external B-Tree index point to a row in an index-organized storage system?
- A
By storing the primary key associated with the search key
- B
By storing the row's exact byte offset in the file
- C
By storing a direct memory address
- D
By storing the row's page number and slot index
Correct answer
ABy storing the primary key associated with the search key
Explanation
In index-organized storage, external indexes store the search key and the corresponding primary key, which is then used to find the row in the table's B-Tree.
- A
- 13
When a row exceeds a certain threshold (e.g., 1/4 of page size), how does the database handle it?
- A
It rejects the insertion
- B
It increases the page size dynamically
- C
It splits the row into chunks and links them using overflow pages
- D
It stores the entire row in a separate overflow page
Correct answer
CIt splits the row into chunks and links them using overflow pages
Explanation
Rows larger than a threshold are split into chunks. The first chunk is stored in the B-Tree cell, and the last 4 bytes point to the next overflow page, forming a linked list.
- A
- 14
What challenge arises when implementing B-Trees with variable-length cells?
- A
The tree cannot be stored on disk
- B
The maximum number of keys per node is fixed, causing frequent splits
- C
There is no fixed upper bound on keys per node, and the balancing algorithm becomes more complex
- D
The tree becomes unbalanced automatically
Correct answer
CThere is no fixed upper bound on keys per node, and the balancing algorithm becomes more complex
Explanation
With variable-length cells, the number of keys per node varies, and standard balancing algorithms need modification (e.g., delaying splits by reorganizing keys).
- A
- 15
What is the first step in parsing an SQL statement according to the transcript?
- A
Tokenizing the input string into tokens
- B
Analyzing the context
- C
Optimizing the query plan
- D
Building an Abstract Syntax Tree
Correct answer
ATokenizing the input string into tokens
Explanation
Parsing begins with a tokenizer that converts the input string into tokens, which are then fed to the parser.
- A
- 16
In the iterator model for query plans, how does the top-level plan retrieve a tuple?
- A
It loads all tuples into memory before processing
- B
It sends a batch request to the disk
- C
It calls
.next()on its child, which recursively calls down to the scanner - D
It uses a callback function
Correct answer
CIt calls
.next()on its child, which recursively calls down to the scannerExplanation
The iterator model works by each node calling
.next()on its child until reaching the scanner, which reads one tuple at a time. - A
- 17
What is the purpose of the collect plan in a query?
- A
To scan the table sequentially
- B
To collect tuples into a fixed-size in-memory buffer and write to a temporary file if needed
- C
To apply projection on columns
- D
To filter tuples based on a condition
Correct answer
BTo collect tuples into a fixed-size in-memory buffer and write to a temporary file if needed
Explanation
The collect plan buffers tuples in memory; if the buffer overflows, it writes them to a temporary file, enabling sorting or aggregation on large datasets.
- A
- 18
In K-Way External Merge Sort, how does the number of passes scale with the number of input buffers K?
- A
The number of passes decreases logarithmically as K increases
- B
The number of passes decreases exponentially with K
- C
The number of passes is independent of K
- D
The number of passes increases linearly with K
Correct answer
AThe number of passes decreases logarithmically as K increases
Explanation
With more buffers, the run size multiplies by K each pass, reducing the number of passes logarithmically (e.g., pass 0: K-page runs, pass 1: K2-page runs).
- A
- 19
In the clock cache replacement algorithm, what does the clock pointer do when it encounters a page with its reference bit set to 1?
- A
It sets the dirty bit to 1 and moves to the next page
- B
It evicts the page immediately
- C
It clears the reference bit to 0 and moves to the next page
- D
It writes the page to disk and removes it from cache
Correct answer
CIt clears the reference bit to 0 and moves to the next page
Explanation
The clock algorithm clears the reference bit of a referenced page and advances the pointer, giving the page a second chance before eviction.
- A
- 20
How does the journal file mechanism support rollback?
- A
It logs all changes made after the transaction starts
- B
It stores the entire database before any transaction
- C
It records only the changes to primary key indexes
- D
It saves the original contents of pages before they are modified
Correct answer
DIt saves the original contents of pages before they are modified
Explanation
Before modifying pages, their original contents are written to the journal file; on rollback, these original pages are copied back to the database.
- A
- 21
What is the difference between a simple journal file and a Write-Ahead Log (WAL) as used in production databases?
- A
A WAL tracks changes per transaction and allows multiple writers simultaneously, while a journal file is simpler and only works with one transaction at a time
- B
A journal file supports multiple concurrent writers, while WAL does not
- C
A journal file stores changes after writing to disk, while WAL stores changes before writing
- D
There is no difference; they are the same
Correct answer
AA WAL tracks changes per transaction and allows multiple writers simultaneously, while a journal file is simpler and only works with one transaction at a time
Explanation
WAL logs each transaction's changes and supports concurrent writers, whereas the journal file approach described is simpler and assumes single-transaction access.
- A
- 22
According to the transcript, what aspect of the MKDB project involved the most development time?
- A
Implementing the B-Tree balancing algorithm for variable-length data
- B
Designing the network protocol
- C
Setting up the TCP server
- D
Writing the SQL parser
Correct answer
AImplementing the B-Tree balancing algorithm for variable-length data
Explanation
The transcript states the author spent about two months on the B-Tree balancing algorithm due to its complexity with variable-length cells.
- A
- 23
How does MKDB handle concurrency?
- A
It uses multiple threads for each connection
- B
It allows multiple concurrent transactions without isolation
- C
It uses optimistic concurrency control
- D
It uses a mutex to allow only one transaction at a time
Correct answer
DIt uses a mutex to allow only one transaction at a time
Explanation
MKDB uses a mutex, so only one transaction can occur at a time; other connections block until the transaction ends.
- A
- 24
Which production database's approach did MKDB copy for its network protocol?
- A
PostgreSQL
- B
MySQL
- C
Redis
- D
SQLite
Correct answer
CRedis
Explanation
The MKDB network protocol was inspired by the Redis protocol, which is simple and allows clients in any language.
- A
- 25
How does MKDB store a table that is too large to fit in RAM for sorting?
- A
It uses a B-Tree index to sort in place
- B
It loads all data into RAM and uses an in-memory sort
- C
It rejects queries that require sorting large datasets
- D
It uses the collect plan to write data to temporary files and then applies external merge sort
Correct answer
DIt uses the collect plan to write data to temporary files and then applies external merge sort
Explanation
MKDB uses a collect plan to buffer data; if it doesn't fit, it writes to temporary files, then applies external merge sort.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.