What is stored in a Database System?
- Relations
- Metadata about relations
- Relation Schemas
- Structure of relations
- Constraints
- Triggers
- View Definitions
- Indexes
- Derived information to speed up access to relations
- Can be a B+ tree, hash table, etc.
- Statistical information about relations for use by query optimizer
- Log files
- Maintained for data recovery
- Relation Schemas
Memory Hierarchy
- Primary Memory
- Register
- Static RAM (SRAM)
- Dynamic RAM (DRAM)
- Secondary Memory
- Magnetic Disk (HDD)
- Solid-State Disk (SSD)
- Tertiary Memory
- Optical disks
- Tapes
As we move from primary to secondary to tertiary memory, the following changes
- Capacity increases (For the same price)
- Cost decreases (For the same capacity)
- Access speed decreases
- Primary storage is volatile while secondary and tertiary storage are non-volatile
DBMS Storage
- DBMS stores data on non-volatile disk for persistence
- DBMS processes data in RAM
- Main disk access operations
- Read: Transfer data from disk to ram
- Write: Transfer data from ram to disk
How does a Magnetic Disk Work?
A hard disk consists of number of disks called platters. Each platter is divided into number of tracks. Each track is divided into number of sectors. Each sector is divided into number of blocks. Each block is usually 512 bytes.
When a hard disk reads / writes data, the following happens:
- The disk controller interpreters access command given by the OS. This incurs command processing time (C).
- The arms of the disk is moved to the correct track. This incurs seek time (S).
- The platter rotates and the read / write head moves to the correct sector. This incurs rotational delay (R).
- The read / write head reads / writes the data. This incurs transfer time (T).
Based on the steps above, we can calculate the total disk access time using the formula below:
access time = C (negligible) + S + R + T
C = Command Processing Time
S = Seek Time
R = Rotational Delay
T = Transfer Time
As Command processing time is usually negligible, we can simplify the formula to:
access time = S + R + T
S = Seek Time
R = Rotational Delay
T = Transfer Time
Seek time
As mentioned above, the seek time is the time taken for the arms of the disk head to move to the correct track.
The average seek time for a HDD is 5ms
to 6ms
.
Rotational Delay (or Rotational Latency)
Rotational delay depends on the rotation speed of the HDD. It is usually measured in revolutions per minute (RPM).
The rotational delay is the time taken for the disk to rotate to the correct sector.
We can calculate the average rotational delay by using the formula below:
R = 0.5(60s / RPM)
R = Average Rotational Delay
RPM = Revolutions Per Minute
For example
Given RPM = 7200
R = 0.5(60s / 7200)
= 0.5(0.00833s)
= 0.00417s
= 4.17ms
R = Average Rotational Delay
RPM = Revolutions Per Minute
Transfer Time
The transfer time is the time taken for the read / write head to read / write the data.
The average sector transfer time for a HDD is 100 - 200 microseconds.
The formula for calculating the average transfer time is:
T = n * (1/RPM) / (SpT)
n = number of requested sectors on the same track
SpT = Sectors Per Track
T = Transfer Time
Sequential I/O vs Random I/O
Sequential I/O
Sequential I/O refers to reading / writing data in a sequential manner. For example, reading / writing data to a file.
When the file is written to sequentially, the disk does the following:
- Interpret the access command (Time negligible)
- Move arm to position disk head on track (Seek time incurred)
- Wait for block to rotate under head (Rotational delay incurred)
- Move all the data to the disk Surface (Transfer time incurred)
Using the formulas we have above we will get the following:
Access Time = S + R + T
= S + 0.5(60s / RPM) + n * (1/RPM) / (SpT)
RPM = Revolutions Per Minute
S = Seek Time
R = Rotational Delay
T = Transfer Time
N = Number of Sectors to be written
SpT = Sectors Per Track
As the data for a file with n
chunks is all together, the seek time and rotational delay is only incurred once.
This way, we can save time by writing data sequentially.
Random I/O
Random I/O refers to reading / writing data in a random manner. For example, reading / writing data to a database.
When the database is written to randomly, the disk does the following:
- For each sector (for
n
sectors)- Interpret the access command (Time negligible)
- Move arm to position disk head on track (Seek time incurred)
- Wait for block to rotate under head (Rotational delay incurred)
- Move all the data to the disk Surface (Transfer time incurred)
Using the formula we discussed above, we will get the following:
Access Time = n(S + R + T)
= n(S + 0.5(60s / RPM) + n * (1/RPM) / (SpT))
RPM = Revolutions Per Minute
S = Seek Time
R = Rotational Delay
T = Transfer Time
n = Number of Sectors to be written
SpT = Sectors Per Track
As the data for a database with n
chunks is not all together, the seek time and rotational delay is incurred for each chunk.
We are pessimistic in our assumption here as we assume that the random data is neither on the same track nor on a nearby sector.
Solid State Drives (SSD)
SSD is a type of non-volatile memory that uses flash memory to store data. They are built using NAND flash memory.
Here are some comparisons between HDD and SSD:
Metric | HDD | SSD | Remarks |
---|---|---|---|
Random Read latency | 10ms | 0.1ms | |
Random Write latency | 10ms | 0.1ms | |
Data transfer rate | 100MB/s | 500MB/s to 3GB/s | 500MB/s for SATA and 3GB/s for NVME drives |
Power consumption | High | Low |
However, SSDs also have their own drawbacks
- They are more expensive than HDDs
- Updates to a page requires erasure of multiple pages (5ms) before overwriting page
- SSDs have a limited number of write cycles (100,000 to 1,000,000)