官术网_书友最值得收藏!

Transaction log

When SQL Server processes any transaction, it works in a way called two-phase commit. When a client starts a transaction by sending a single DML request or by calling the BEGIN TRAN command, SQL Server requests data pages from disk to memory called buffer cache and makes the requested changes in these data pages in memory. When the DML request is fulfilled or the COMMIT command comes from the client, the first phase of the commit is finished, but data pages in memory differ from their original versions in a data file on disk. The data page in memory is in a state called dirty.

When a transaction runs, a transaction log file is used by SQL Server for a very detailed chronological description of every single action done during the transaction. This description is called write-ahead-logging, shortly WAL, and is one of the oldest processes known on SQL Server.

The second phase of the commit usually does not depend on the client's request and is an internal process called checkpoint. Checkpoint is a periodical action that:

  • searches for dirty pages in buffer cache,
  • saves dirty pages to their original data file location,
  • marks these data pages as clean (or drops them out of memory to free memory space),
  • marks the transaction as checkpoint or inactive in the transaction log.

Write-ahead-logging is needed for SQL Server during recovery process. Recovery process is started on every database every time SQL Server service starts. When SQL Server service stops, some pages could remain in a dirty state and they are lost from memory. This can lead to two possible situations:

  • The transaction is completely described in the transaction log, the new content of the data page is lost from memory, and data pages are not changed in the data file
  • The transaction was not completed at the moment SQL Server stopped, so the transaction cannot be completely described in the transaction log as well, data pages in memory were not in a stable state (because the transaction was not finished and SQL Server cannot know if COMMIT or ROLLBACK will occur), and the original version of data pages in data files is intact

SQL Server decides these two situations when it's starting. If a transaction is complete in the transaction log but was not marked as checkpoint, SQL Server executes this transaction again with both phases of COMMIT. If the transaction was not complete in the transaction log when SQL Server stopped, SQL Server will never know what was the user's intention with the transaction and the incomplete transaction is erased from the transaction log as if it had never started.

The aforementioned described recovery process ensures that every database is in the last known consistent state after SQL Server's startup.

It's crucial for DBAs to understand write-ahead-logging when planning a backup strategy because when restoring the database, the administrator has to recognize if it's time to run the recovery process or not.

主站蜘蛛池模板: 兴安县| 邛崃市| 台东市| 哈密市| 东台市| 得荣县| 广安市| 庆云县| 鄂伦春自治旗| 麻城市| 嘉禾县| 南城县| 竹山县| 枣阳市| 滕州市| 卓资县| 三穗县| 沈阳市| 九江县| 桃园县| 肥城市| 久治县| 浙江省| 资中县| 沙田区| 卢龙县| 南平市| 雷山县| 靖边县| 克什克腾旗| 嘉义县| 栖霞市| 三明市| 石柱| 鲁甸县| 万安县| 正镶白旗| 响水县| 聂拉木县| 梓潼县| 九龙县|