Friday, August 31, 2012

How SQL Server writes data to disk ?


The transaction log is a serial record of all modifications that have occurred in the database. SQL Server writes changes to the log before it writes changes to the actual data file.
SQL Server maintains a buffer cache into which it reads data pages when data must be retrieved. Data modifications are not made directly to disk, but are made to the copy of the page in the buffer cache. The modification is not written to disk until a checkpoint occurs in the database, or the modification must be written to disk so the buffer can be used to hold a new page. Writing a modified data page from the buffer cache to disk is called flushing the page. A page modified in the cache, but not yet written to disk, is called a dirty page.
 At the time a modification is made to a page in the buffer, a log record is built in the log cache that records the modification. This log record must be written to disk before the associated dirty page is flushed from the buffer cache to disk. If the dirty page is flushed before the log record is written, the dirty page creates a modification on the disk that cannot be rolled back if the server fails before the log record is written to disk. SQL Server has logic that prevents a dirty page from being flushed before the associated log record is written. Log records are written to disk when the transactions are committed.
When users change data, SQL Server doesn't write that change directly to the data. Rather, SQL Server locates the appropriate data and then loads it into a special area of RAM called the data cache. Changes are made in RAM. Then, SQL Server copies changes waiting in RAM to the transaction log. Only then does SQL Server write changes to the actual data file.
This is called a write-ahead log because SQL Server writes changes to the log before it writes changes to the actual data file. This approach is quite a bit faster than writing directly to the data file.


Checkpoint in SQL Server

What is checkpoint?

Checkpoint is a process to write drity pages to disk. Dirty pages are pages which are in buffer pool and modified but not yet written to disk.

Syntax :

Checkpoint [ checkpoint duration ]

Check point duration is advanced option. Checkpoint duration is time is seconds and it's value should be interger and greater than zero. If you mention any value in checkpoint duration then SQL will ensure to maintain the amount of dirty pages in disk.
If SQL services restart then all the databases should recover with in the time which is mentioned in the checkpoint duration. If you write nothing in checkpoint duration then SQL Server will issue automatic checkpoint. What does automatic means ? If no value is mentioned in checkpoint duration then SQL Server will issue the checkpoint when data in dirty pages is greater than what SQL can recover in the recovery interval. SQL will ensure that databases should be online after restart in specific amount of time which is  mentioned in recovery interval in sp_configure.

What check point do ?

Batch up write processes to improve performance.
Reduces time required for crash recovery.

How checkpoint improves performance?

If data is written to disk each time pages get's modified hence write I\Os will increase on the server which in result will bring down performance on whole. Checkpoint batches up write processes and writes them to disk at certain duration.

How checkpoint reduces crash recovery ?

SQL keep monitoring the pages in buffer pool and checkpoint is issues as soon as number of dirty pages increases beyond what can be covered in specified recovery interval.