Write-Ahead Logging (WAL) Write-Ahead Logging (WAL)

Write-Ahead Logging (WAL)

 From: http://www.cnblogs.com/wenBlog/p/4423497.html

     WAL (Write-Ahead Logging) technology is used in SQL Server to ensure the ACID characteristics of transaction logs. And greatly reduce IO operations.

     The core idea of ​​WAL is: before data is written to the database, it is first written to the log, and then the log records are changed to the memory.

        Steps to Modify Data in SQL Server

     1. Write the "Begin Tran" record in the buffer log of SQL Server

     2. Write the information to be modified in the log page of the SQL Server buffer

     3. Write the data to be modified into the data page in the SQL Server buffer

     4. Write a "Commit" record in the SQL Server buffer log

     5. Write the log of the buffer to the log file

     6. Send confirmation information to the client (SMSS, ODBC, etc.)

     7. Write the pages in the buffer to disk (Lazy Writer or CheckPoint starts before this point)

     This article focuses on two processes for writing buffer data pages to disk: CheckPoint and Lazy Writer. 
     When the transaction encounters Commit, it just writes all the log pages of the buffer to the log file in the disk; and until the Lazy Writer or CheckPoint, the data page of the buffer is actually written to the disk file.

     CheckPoint: The system automatically issues checkpoints at intervals determined by the Recovery Interval SQL Server configuration option. The Checkpoint method issues a checkpoint immediately, regardless of the recovery interval setting.

      The CheckPoint interval is a server-level parameter. This can be configured via sp_config or in SSMS:image

It should be noted that the CHECKPOINT privilege will be granted to members of the system administrator (sysadmin)  fixed server role and  the db_owner  and  db_backupoperator  fixed database roles by default, and cannot be transferred. A shorter recovery interval means shorter recovery time and more disk IO, while a longer recovery interval results in less disk IO usage and longer recovery time.

Trigger condition of checkpoint

1. Lots of logs since last checkpoint

2. The service instance is shut down

3. Database full backup or differential backup (log backup will not trigger checkpoint)

4. When the database recovery model is the simple recovery model, when the log file usage exceeds 70%

Lazy Writer: lazywriter is a system process whose main task is to batch flush aged dirty buffers (referring to buffers containing changes that must be written back to disk for the buffer to be reused by other pages), and make It can be used by user processes.

Trigger condition of lazywriter

1. Insufficient free cache block data available in the cache

2. Windows system memory pressure

the difference:

      The purpose of Checkpoint is to reduce the recovery time of the database (recovery after the service crashes or restarts the service), while the purpose of the Lazy writer is to ensure that the SQL OS has free cache blocks and the system has a certain amount of available memory.

      Both Checkpoint and LazyWriter will write buffer dirty pages to disk;

      LazyWriter will update the buffer free and available list, but checkpoint will not;

      Checkpoint operations will be logged to the database log, but lazywriter will not;

      By specifying the parameters after CheckPoint, SQL Server will complete the CheckPoint process according to this time. If the specified time is short, SQL Server will use more resources to complete the CheckPoint process first.

      Typically, Lazy Writer does much more work than CheckPoint to write "dirty" pages to disk.

Summarize

    This paper briefly introduces the concept of WAL and the two methods to finally realize the actual modification of data. Introduced CheckPoint and Lazy Writer, and compared the mechanisms and trigger conditions of the two methods, which provides a good foundation for us to further explain the basic principles and recovery mechanisms of transaction logs in the future.

     WAL (Write-Ahead Logging) technology is used in SQL Server to ensure the ACID characteristics of transaction logs. And greatly reduce IO operations.

     The core idea of ​​WAL is: before data is written to the database, it is first written to the log, and then the log records are changed to the memory.

        Steps to Modify Data in SQL Server

     1. Write the "Begin Tran" record in the buffer log of SQL Server

     2. Write the information to be modified in the log page of the SQL Server buffer

     3. Write the data to be modified into the data page in the SQL Server buffer

     4. Write a "Commit" record in the SQL Server buffer log

     5. Write the log of the buffer to the log file

     6. Send confirmation information to the client (SMSS, ODBC, etc.)

     7. Write the pages in the buffer to disk (Lazy Writer or CheckPoint starts before this point)

     This article focuses on two processes for writing buffer data pages to disk: CheckPoint and Lazy Writer. 
     When the transaction encounters Commit, it just writes all the log pages of the buffer to the log file in the disk; and until the Lazy Writer or CheckPoint, the data page of the buffer is actually written to the disk file.

     CheckPoint: The system automatically issues checkpoints at intervals determined by the Recovery Interval SQL Server configuration option. The Checkpoint method issues a checkpoint immediately, regardless of the recovery interval setting.

      The CheckPoint interval is a server-level parameter. This can be configured via sp_config or in SSMS:image

It should be noted that the CHECKPOINT privilege will be granted to members of the system administrator (sysadmin)  fixed server role and  the db_owner  and  db_backupoperator  fixed database roles by default, and cannot be transferred. A shorter recovery interval means shorter recovery time and more disk IO, while a longer recovery interval results in less disk IO usage and longer recovery time.

Trigger condition of checkpoint

1. Lots of logs since last checkpoint

2. The service instance is shut down

3. Database full backup or differential backup (log backup will not trigger checkpoint)

4. When the database recovery model is the simple recovery model, when the log file usage exceeds 70%

Lazy Writer: lazywriter is a system process whose main task is to batch flush aged dirty buffers (referring to buffers containing changes that must be written back to disk for the buffer to be reused by other pages), and make It can be used by user processes.

Trigger condition of lazywriter

1. Insufficient free cache block data available in the cache

2. Windows system memory pressure

the difference:

      The purpose of Checkpoint is to reduce the recovery time of the database (recovery after the service crashes or restarts the service), while the purpose of the Lazy writer is to ensure that the SQL OS has free cache blocks and the system has a certain amount of available memory.

      Both Checkpoint and LazyWriter will write buffer dirty pages to disk;

      LazyWriter will update the buffer free and available list, but checkpoint will not;

      Checkpoint operations will be logged to the database log, but lazywriter will not;

      By specifying the parameters after CheckPoint, SQL Server will complete the CheckPoint process according to this time. If the specified time is short, SQL Server will use more resources to complete the CheckPoint process first.

      Typically, Lazy Writer does much more work than CheckPoint to write "dirty" pages to disk.

Summarize

    This paper briefly introduces the concept of WAL and the two methods to finally realize the actual modification of data. Introduced CheckPoint and Lazy Writer, and compared the mechanisms and trigger conditions of the two methods, which provides a good foundation for us to further explain the basic principles and recovery mechanisms of transaction logs in the future.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324781013&siteId=291194637