Transaction Log Logical Architecture

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介:

引用:http://msdn.microsoft.com/en-us/library/ms180892(SQL.90).aspx

  The SQL Server 2005 transaction log operates logically as if the transaction log is a string of log records. Each log record is identified by a log sequence number (LSN). Each new log record is written to the logical end of the log with an LSN that is higher than the LSN of the record before it.

  Log records are stored in a serial sequence as they are created. Each log record contains the ID of the transaction that it belongs to. For each transaction, all log records associated with the transaction are individually linked in a chain using backward pointers that speed the rollback of the transaction.

  Log records for data modifications record either the logical operation performed or they record the before and after images of the modified data. The before image is a copy of the data before the operation is performed; the after image is a copy of the data after the operation has been performed.

注解:

  首先需要理解log record的概念,每一个log record都有一个LSN与之对应,我们可以将LSN看成是主键,用来标识每一条log record。sqlserver在修改数据页的时候,会产生相应的log record,这个record中保存有以下信息:产生这条log record的事务id,这条log record的LSN,修改前的状态,修改后的状态等内容。所以log record中保存的不是操作,而是数据的状态。每一个脏页(dirty page)至少有一个log record与之对应。并且脏页的header里面保存的是最后修改这个脏页的log record的LSN。每一个log record都会串行写到日志的逻辑结尾处,所以LSN是递增的。

实例

Query1.sql中执行一个事务

复制代码
--实验目的,证明lsn是递增的。
USE master
go
--step1:查看数据库信息,获取TESTDB2的dbid为12
sp_helpdb

--step2:通过DBCC LOG(<db_id>, <formart_id>)获取log信息
dbcc log(12,3)

use TESTDB2
--step3:创建表
create table student
(
    stuid int,
    stuname nvarchar(20)
);

--step4:插入表后查看日志信息,也可以在apexlog中查看这个操作的日志.
dbcc log(12,3)--105行日志记录

--step5:查看最老事务
dbcc opentran

--step6:新建事务tran1插入记录
begin tran
insert into student values(1,'zhangsan');
insert into student values(2,'lisi');

----step7:查看最老事务
dbcc opentran
复制代码

这时候使用APEXLOG查看相应的日志记录

Query2.sql中执行另外一个事务

USE TESTDB2
--step8:新建事务tran2插入记录
begin tran
insert into student values(3,'王五');

这时候使用APEXLOG查看相应的日志记录

回到Query1.sql中执行一下语句

--step9:在tran1中插入一条记录
insert into student values(4,'孙六');

这时候使用APEXLOG查看相应的日志记录

从上面的几张图可以发现,LSN是递增的,同样的一个事务可以有多个log record。

The steps to recover an operation depend on the type of log record:

  • Logical operation logged 
    • To roll the logical operation forward, the operation is performed again.
    • To roll the logical operation back, the reverse logical operation is performed.
  • Before and after image logged 
    • To roll the operation forward, the after image is applied.
    • To roll the operation back, the before image is applied.

Many types of operations are recorded in the transaction log. These operations include:

  • The start and end of each transaction.
  • Every data modification (insert, update, or delete). This includes changes by system stored procedures or data definition language (DDL) statements to any table, including system tables.
  • Every extent and page allocation or deallocation.
  • Creating or dropping a table or index.

Rollback operations are also logged. Each transaction reserves space on the transaction log to make sure that enough log space exists to support a rollback that is caused by either an explicit rollback statement or if an error is encountered. The amount of space reserved depends on the operations performed in the transaction, but generally is equal to the amount of space used to log each operation. This reserved space is freed when the transaction is completed.

回滚操作也会被写入到日志当中去,不过APEXLOG不记录回滚操作的日志记录。下面以一个实验证明rollback操作会被记录日志:

复制代码
--实验2:验证Rollback operations are also logged
use TESTDB2
--step1:查看当前记录
select * from student;

--step2:查看当前日志,好对比待会的操作.
dbcc log(12,3)

begin tran
--step3:插入一条记录
insert into student values(4,'lisi');

--step4:发现多了两条记录,LOP_BEGIN_XACT和LOP_INSERT_ROWS
dbcc log(12,3)

--step5:查看发现记录已经被插入
select * from student;

--step6:事务回滚
rollback

--step7:查看日志记录,发现又多了两条记录,LOP_DELETE_ROWS和LOP_ABORT_XACT
dbcc log(12,3)
--实验结论:上面的LOP_DELETE_ROWS是rollback的log record,LOP_BEGIN_XACT和LOP_ABORT_XACT是事务开始跟结束的标志
----------------------------------------------------------
复制代码

总结:

  • insert操作对应log record中的LOP_INSERT_ROWS,其rollback操作是LOP_DELETE_ROWS;
  • delete操作对应log record中的LOP_DELETE_ROWS,其rollback操作是LOP_INSERT_ROWS;
  • update操作对应log record中的LOP_MODIFY_ROW,其rollback操作是LOP_MODIFY_ROW

The section of the log file from the first log record that must be present for a successful database-wide rollback to the last-written log record is called the active part 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.

所谓活动事务日志,就是从最老未提交事务产生的log record开始,以及在它之后产生的所有日志记录。

如上图所示,Active Log就是从MinLSN开始到逻辑日志的结尾。

 

本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2012/07/13/2590424.html,如需转载请自行联系原作者


相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
SQL 人工智能 JSON
深度解析Delta Transaction Log
深度解析Delta Transaction Log
深度解析Delta Transaction Log
|
SQL 存储 数据库
处理令人心烦的数据库事务日志 (SQL Server Transaction Log Files)
经常, 我们会被过快增长的数据库事务日志Transaction Log而困扰, 如果我们没有正确及时的处理, 可能会造成数据库交易无法进行, 服务器磁盘空间占光等问题. 在SQL Server的使用过程中, 我经常帮助用户和数据库的维护人员处理日志Transaction Log太大后造成的系统瘫痪的问题. 其实这个问题很容易避免. 今天我给大家分享下, 是什么造成了日志增长过大的问题. 和如何避免这种问题再次发生. 该文章的语句适用于SQL Server 2015 及其以后的版本 每一个数据库至少有两个文件: 一个是数据文件(Data file), 一个是事务日志文件(Transaction
787 0
|
SQL 数据库 关系型数据库
|
SQL 数据库
通过Transaction Log(fn_dblog)取回被删除的数据
最近跟 James 讨论为何「ApexSQL Log」这个工具可以读到被删除的数据呢? 原来它是透过 Transaction Log 来读取数据的! 于是透过 Transaction Log 到网络上有找到「SQL Server – How to find Who Deleted What re...
1380 0
|
22天前
|
XML 安全 Java
【日志框架整合】Slf4j、Log4j、Log4j2、Logback配置模板
本文介绍了Java日志框架的基本概念和使用方法,重点讨论了SLF4J、Log4j、Logback和Log4j2之间的关系及其性能对比。SLF4J作为一个日志抽象层,允许开发者使用统一的日志接口,而Log4j、Logback和Log4j2则是具体的日志实现框架。Log4j2在性能上优于Logback,推荐在新项目中使用。文章还详细说明了如何在Spring Boot项目中配置Log4j2和Logback,以及如何使用Lombok简化日志记录。最后,提供了一些日志配置的最佳实践,包括滚动日志、统一日志格式和提高日志性能的方法。
166 30
【日志框架整合】Slf4j、Log4j、Log4j2、Logback配置模板