1. Home
  2. Explore
  3. Computer Science
  4. Build a Database from Scratch

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Build a Database from Scratch

  • 25Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Database File Storage and Row Formats

How does MySQL organize databases and tables on the file system?

25 questions

  1. 1

    How does MySQL organize databases and tables on the file system?

    1. A

      It creates a single file for all databases and tables

    2. B

      It creates a folder for each database and a file for each table inside that folder

    3. C

      It creates a folder for each table inside a database folder

    4. D

      It stores all data in memory without writing to disk

    Correct answer
    B

    It 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.

  2. 2

    What is a major disadvantage of using CSV format to store a database table in a file?

    1. A

      CSV format does not support variable-length strings

    2. B

      CSV cannot store integer values

    3. C

      CSV files can only store up to 255 rows

    4. D

      CSV requires delimiters, making it impossible to jump directly to a specific row without linear scanning

    Correct answer
    D

    CSV 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.

  3. 3

    When using fixed-size rows with a string column limited to 255 characters, how many bytes are allocated for the string's length?

    1. A

      255 bytes

    2. B

      2 bytes

    3. C

      1 byte

    4. D

      260 bytes

    Correct answer
    C

    1 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.

  4. 4

    What is the time complexity of searching for a primary key using fixed-size rows without an index?

    1. A

      O(n2)O(n^2)O(n2)

    2. B

      O(log⁔n)O(\log n)O(logn)

    3. C

      O(1)O(1)O(1)

    4. D

      O(n)O(n)O(n)

    Correct answer
    D

    O(n)O(n)O(n)

    Explanation

    Without an index, searching requires scanning rows sequentially, which is O(n)O(n)O(n).

  5. 5

    What problem arises when using a normal binary tree as an index if primary keys are inserted in ascending order?

    1. A

      The tree degrades into a linked list, making search O(n)O(n)O(n)

    2. B

      The tree requires more memory than a B-Tree

    3. C

      The tree becomes perfectly balanced

    4. D

      The tree cannot be stored on disk

    Correct answer
    A

    The tree degrades into a linked list, making search O(n)O(n)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(log⁔n)O(\log n)O(logn) search advantage.

  6. 6

    In a B-Tree with a minimum children parameter C=2C = 2C=2, what is the maximum number of keys each node can hold?

    1. A

      2

    2. B

      5

    3. C

      3

    4. D

      4

    Correct answer
    C

    3

    Explanation

    The maximum number of keys in an internal node is 2Cāˆ’1=32C - 1 = 32Cāˆ’1=3 when C=2C=2C=2.

  7. 7

    During B-Tree insertion, when a node becomes full, what is the first step in the self-balancing process?

    1. A

      Split the node into two nodes

    2. B

      Create a new root node

    3. C

      Shift all keys to the right

    4. D

      Move the median key to the parent node

    Correct answer
    D

    Move 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.

  8. 8

    Why are B-Tree nodes stored in disk blocks or pages instead of reading individual bytes?

    1. A

      To reduce CPU usage

    2. B

      To decrease the size of the index

    3. C

      To make binary search possible

    4. D

      Because disks can only read data in blocks (e.g., 4096 bytes at a time)

    Correct answer
    D

    Because 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.

  9. 9

    What type of storage does SQLite use for tables?

    1. A

      Index-organized storage where tables are stored as B-Trees sorted by primary key

    2. B

      Flat file storage with CSV format

    3. C

      Log-structured merge tree storage

    4. D

      Heap storage with separate index files

    Correct answer
    A

    Index-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.

  10. 10

    In a slotted page, where are the variable-length records (cells) stored?

    1. A

      In a separate overflow area

    2. B

      At the end of the page, growing leftward

    3. C

      At the beginning of the page, growing rightward

    4. D

      In a fixed array in the middle of the page

    Correct answer
    B

    At 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.

  11. 11

    What is the primary advantage of using a slot array in a slotted page for binary search?

    1. A

      The slot array is sorted by key, allowing binary search with fixed-size entries

    2. B

      The slot array eliminates the need for child pointers

    3. C

      The slot array reduces the page size

    4. D

      The slot array stores data in compressed format

    Correct answer
    A

    The 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.

  12. 12

    How does an external B-Tree index point to a row in an index-organized storage system?

    1. A

      By storing the primary key associated with the search key

    2. B

      By storing the row's exact byte offset in the file

    3. C

      By storing a direct memory address

    4. D

      By storing the row's page number and slot index

    Correct answer
    A

    By 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.

  13. 13

    When a row exceeds a certain threshold (e.g., 1/4 of page size), how does the database handle it?

    1. A

      It rejects the insertion

    2. B

      It increases the page size dynamically

    3. C

      It splits the row into chunks and links them using overflow pages

    4. D

      It stores the entire row in a separate overflow page

    Correct answer
    C

    It 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.

  14. 14

    What challenge arises when implementing B-Trees with variable-length cells?

    1. A

      The tree cannot be stored on disk

    2. B

      The maximum number of keys per node is fixed, causing frequent splits

    3. C

      There is no fixed upper bound on keys per node, and the balancing algorithm becomes more complex

    4. D

      The tree becomes unbalanced automatically

    Correct answer
    C

    There 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).

  15. 15

    What is the first step in parsing an SQL statement according to the transcript?

    1. A

      Tokenizing the input string into tokens

    2. B

      Analyzing the context

    3. C

      Optimizing the query plan

    4. D

      Building an Abstract Syntax Tree

    Correct answer
    A

    Tokenizing 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.

  16. 16

    In the iterator model for query plans, how does the top-level plan retrieve a tuple?

    1. A

      It loads all tuples into memory before processing

    2. B

      It sends a batch request to the disk

    3. C

      It calls .next() on its child, which recursively calls down to the scanner

    4. D

      It uses a callback function

    Correct answer
    C

    It calls .next() on its child, which recursively calls down to the scanner

    Explanation

    The iterator model works by each node calling .next() on its child until reaching the scanner, which reads one tuple at a time.

  17. 17

    What is the purpose of the collect plan in a query?

    1. A

      To scan the table sequentially

    2. B

      To collect tuples into a fixed-size in-memory buffer and write to a temporary file if needed

    3. C

      To apply projection on columns

    4. D

      To filter tuples based on a condition

    Correct answer
    B

    To 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.

  18. 18

    In K-Way External Merge Sort, how does the number of passes scale with the number of input buffers KKK?

    1. A

      The number of passes decreases logarithmically as KKK increases

    2. B

      The number of passes decreases exponentially with KKK

    3. C

      The number of passes is independent of KKK

    4. D

      The number of passes increases linearly with KKK

    Correct answer
    A

    The number of passes decreases logarithmically as KKK increases

    Explanation

    With more buffers, the run size multiplies by KKK each pass, reducing the number of passes logarithmically (e.g., pass 0: KKK-page runs, pass 1: K2K^2K2-page runs).

  19. 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?

    1. A

      It sets the dirty bit to 1 and moves to the next page

    2. B

      It evicts the page immediately

    3. C

      It clears the reference bit to 0 and moves to the next page

    4. D

      It writes the page to disk and removes it from cache

    Correct answer
    C

    It 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.

  20. 20

    How does the journal file mechanism support rollback?

    1. A

      It logs all changes made after the transaction starts

    2. B

      It stores the entire database before any transaction

    3. C

      It records only the changes to primary key indexes

    4. D

      It saves the original contents of pages before they are modified

    Correct answer
    D

    It 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.

  21. 21

    What is the difference between a simple journal file and a Write-Ahead Log (WAL) as used in production databases?

    1. 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

    2. B

      A journal file supports multiple concurrent writers, while WAL does not

    3. C

      A journal file stores changes after writing to disk, while WAL stores changes before writing

    4. D

      There is no difference; they are the same

    Correct answer
    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

    Explanation

    WAL logs each transaction's changes and supports concurrent writers, whereas the journal file approach described is simpler and assumes single-transaction access.

  22. 22

    According to the transcript, what aspect of the MKDB project involved the most development time?

    1. A

      Implementing the B-Tree balancing algorithm for variable-length data

    2. B

      Designing the network protocol

    3. C

      Setting up the TCP server

    4. D

      Writing the SQL parser

    Correct answer
    A

    Implementing 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.

  23. 23

    How does MKDB handle concurrency?

    1. A

      It uses multiple threads for each connection

    2. B

      It allows multiple concurrent transactions without isolation

    3. C

      It uses optimistic concurrency control

    4. D

      It uses a mutex to allow only one transaction at a time

    Correct answer
    D

    It 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.

  24. 24

    Which production database's approach did MKDB copy for its network protocol?

    1. A

      PostgreSQL

    2. B

      MySQL

    3. C

      Redis

    4. D

      SQLite

    Correct answer
    C

    Redis

    Explanation

    The MKDB network protocol was inspired by the Redis protocol, which is simple and allows clients in any language.

  25. 25

    How does MKDB store a table that is too large to fit in RAM for sorting?

    1. A

      It uses a B-Tree index to sort in place

    2. B

      It loads all data into RAM and uses an in-memory sort

    3. C

      It rejects queries that require sorting large datasets

    4. D

      It uses the collect plan to write data to temporary files and then applies external merge sort

    Correct answer
    D

    It 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.

Question 1 of 25
Question 1 of 25

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent