Checkpoints and the Active Log

简介:

原文:http://msdn.microsoft.com/en-us/library/ms179355(SQL.90).aspx

检查点在数据库中的执行过程:

  1. 在日志文件中写入LOP_BEGIN_CKPT表示checkpoint开始、
  2. 记录MinLSN,MinLSN=min(LSN of the start of the checkpoint,LSN of the start of the oldest active transaction)
  3. 如果数据库使用的是简单恢复模式,则截断(truncate)MinLSN之前的所有VLFs。如果是完整恢复或者是大容量日志模式,不会截断任何事务日志。
  4. 将dirty log 和dirty page 写入磁盘。对于dirty page 来说,即使其中存在未提交的事务,也要写入的磁盘。因为已经记录了MinLSN,所以即使脏页写入了磁盘,也可以回滚。
  5. 在日志文件中写入LOP_END_CKPT表示checkpoint结束

Active Log

The section of the log file from the MinLSN to the last-written log record is called the active portion of the log, or the active log. This is the section of the log required to do a full recovery of the database. No part of the active log can ever be truncated. All log records must be truncated from the parts of the log before the MinLSN.

首先,Active Log是指从MinLSN开始,到当前最新的Log Record。而Active Log是不会被truncate的,Truncate日志必须从MinLSN之前开始。如果是simple模式,那么在发生checkpoint的时候MinLSN之前的所有日志被truncate掉。但是在full模式下,发生checkpoint的时候并不truncate日志,它只会在backup transaction log完成之后,truncate掉MinLSN之前的所有log。总而言之,如果发生truncate,那么MinLSN之前的所有日志必定全部被truncate。

The following illustration shows a simplified version of the end-of-a-transaction log with two active transactions. Checkpoint records have been compacted to a single record.

LSN 148 is the last record in the transaction log. At the time that the recorded checkpoint at LSN 147 was processed, Tran 1 had been committed and Tran 2 was the only active transaction. That makes the first log record for Tran 2 the oldest log record for a transaction active at the time of the last checkpoint. This makes LSN 142, the Begin transaction record for Tran 2, the MinLSN.

如上所述,在最后一个checkpoint(LSN=147)的地方,我们发现Tran1已经提交了,只剩下Tran2没有提交,所以MinLSN就是Tran2的起点,也就是LSN=142的地方。

数据库的还原

如上图所示,假如数据库在LSN=148的地方发生了崩溃,我们需要恢复数据库,那么我们将执行如下操作。

  1. 首先进行完整还原。
  2. 接着事务日志还原,我们需要将数据库恢复到数据库崩溃的那一刻,执行前滚操作。从MinLSN开始redo操作,直到日志末尾
  3. 最后让数据库处于一致状态,也就是进行回滚操作。从日志末尾一致到MinLSN,回滚所有没有commit的事务,比如这里的Tran2.


本文转自xwdreamer博客园博客,原文链接http://www.cnblogs.com/xwdreamer/archive/2012/07/16/2593474.html,如需转载请自行联系原作者
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
12月前
|
Oracle 前端开发 关系型数据库
log file sync 和 log file parallel write等待事件的区别和联系
log file parallel write 和log file sync这两个等待事件关系密切,很多人对这两个等待事件有一些误解,我们先来看看Oracle官方文档的解释:
|
关系型数据库 Oracle 数据库
|
存储 缓存 数据处理
完全揭秘log file sync等待事件
什么是log file sync等待事件呢?在一个提交(commit)十分频繁的数据库中,一般会出现log file sync等待事件,当这个等待事件出现在top5中,这个时侯我们需要针对log file sync等待事件进行优化,一定要尽快分析并解决问题,否则当log file sync等待时间从几毫秒直接到20几毫秒可能导致系统性能急剧下降,甚至会导致短暂的挂起。
完全揭秘log file sync等待事件
|
监控 调度