Categories
Database

Principles of Hard Drives and How SQL Server Leverages Them to Reduce I/O

Hard Drive Principles

Hard drives are the last mechanical storage component inside modern computers, operating at speeds significantly slower than memory or cache. However, due to their relatively low cost, they are well-suited for permanent storage. To mitigate the challenges posed by slower speeds, operating systems apply various optimizations. Let’s first understand the fundamental principles of hard drives.

A hard drive consists of spinning magnetic disks and a read-write head arm that moves across these disks to read or write data. This mechanical nature is why hard drives are referred to as mechanical components. The disk is divided into tracks radiating outward from the center. Each track is further subdivided into sectors, which represent the smallest unit of addressable storage. However, the smallest allocatable unit is a cluster, leading to discrepancies between a file’s actual size and its storage space.

Time Components of Disk Read/Write Operations

Data read/write operations on a hard drive involve three time components:

1. Seek Time

This is the time the read-write arm takes to move to the desired track. It includes:

● Time to start the arm's motion

● A constant factor multiplied by the number of tracks to move

● Both components depend on the hardware specifications of the drive.

2. Rotational Latency

This refers to the time taken for the desired sector to align under the read-write head. It is directly tied to the drive’s rotational speed.

Average rotational latency = 1 / (2 × rotations per second)

For instance, for a 7200 RPM drive, average rotational latency is approximately 4.17 ms.

3. Transfer Time

This is the time taken to read or write data.

Transfer time = Time = bytes to read or write / (rotational speed per second × bytes per sector)

Disk Scheduling Algorithms

To optimize disk performance, operating systems use disk scheduling algorithms to minimize seek times, as other parameters are largely hardware-dependent. Common algorithms include:

1. First-Come-First-Serve (FCFS)

Requests are processed in the order they arrive. While simple, it does not minimize seek time.

2. Shortest Seek Time First (SSTF)

Prioritizes requests closest to the current head position, minimizing average seek time. However, it can lead to starvation of distant requests.

3. SCAN (Elevator Algorithm)

Processes requests in one direction until the outermost or innermost track is reached, then reverses. This avoids starvation but may disadvantage requests at extremes.

4. C-SCAN (Circular SCAN)

Similar to SCAN but always moves in one direction (e.g., from the innermost to the outermost track), then resets. This ensures fair treatment of all requests.

Additional Optimizations Leveraged by SQL Server

Beyond scheduling algorithms, SQL Server applies techniques based on the principle of locality:

Locality Principle

Spatial Locality: Data near the current access point is likely to be accessed soon.

Temporal Locality: Recently accessed data is likely to be reused shortly.

Read-Ahead

SQL Server uses read-ahead to load not only the requested data but also adjacent data, leveraging spatial locality. This reduces I/O requests by preemptively fetching nearby data.

Delayed Write

Using temporal locality, modified data is not immediately written to disk. Instead, it is kept in memory (in the buffer pool) and marked as "dirty." It is later written to disk via mechanisms like checkpoints or lazy writer processes, reducing I/O operations.

Optimized Physical Layout

Placing related data in consecutive physical disk sectors minimizes head movement and seek time. SQL Server achieves this with clustered indexes, which arrange data physically based on primary key values.

By understanding and leveraging these principles, SQL Server efficiently reduces disk I/O, significantly improving performance.

By Jaxon Tisdale

I am Jaxon Tisdale. I will share you with my experience in Network, AWS, and databases.

Leave a Reply

Your email address will not be published. Required fields are marked *