MSSQL服务器中需要日志缓存吗?

sud*_*709 3 sql-server transaction-log

  • 如果每条事务记录都立即硬化在事务日志文件中,那么还需要日志缓存吗?
  • 如果当系统发生故障时,事务记录保留在日志缓存中,那么该记录将丢失并且数据库将不一致,对吧?
  • 拥有日志缓存还有其他原因或目的吗?

Sha*_*nky 7

如果每条事务记录都立即硬化在事务日志文件中,那么还需要日志缓存吗?

不,它不是“立即”强化的,而是在日志记录实际刷新到磁盘之前事务要经历一系列步骤。只需阅读SQL Server 和 WAL阅读“SQL Server 和 WAL”部分。

对于每个数据库,您都会获得一个日志缓存,一个连续的内存区域,用于保存日志信息并在事务提交时刷新到磁盘。日志缓存可以包含有关多个事务的信息,并且每次提交都会触发刷新到磁盘以强化日志记录。AFAIK 大小为 60K(新版本中可能已更改,不太确定大小)

如果当系统发生故障时,事务记录保留在日志缓存中,那么该记录将丢失并且数据库将不一致,对吧?

如果日志缓存记录没有刷新到磁盘/没有硬化,是的,它将丢失

拥有日志缓存还有其他原因或目的吗?

阅读我上面分享的博客。我引用上面博客中的例子

BEGIN TRANSACTION
 INSERT INTO tblTest VALUES (1)
COMMIT TRANSACTION
Run Code Online (Sandbox Code Playgroud)

接下来,将活动分解为简单的日志记录步骤,如下表所述。

Statement                          Actions performed
BEGIN TRANSACTION    **Written to the log cache area.** However, it is 
                     not necessary to flush to stable storage 
                     because the SQL Server has not made any 
                     physical changes.

INSERT INTO tblTest 
                 1. Data page 150 is retrieved into SQL Server data  
                  cache, if not already available.
                 2. The page is latched, pinned, and marked dirty, 
                 and appropriate locks are obtained.
                3. An Insert Log record is built and added to the 
                log cache.
                4. A new row is added to the data page.
                5. The latch is released.
                6. The log records associated with the transaction 
                 or page does not have to be flushed at this point 
                 because all changes remain in volatile storage.

COMMIT TRANSACTION  
                     1. A Commit Log record is formed and the log 
                     records associated with the transaction must be 
                     written to stable storage. The transaction is 
                     not considered committed until the log records 
                     are correctly assigned to stable storage.
                     2. Data page 150 remains in SQL Server data 
                     cache and is not immediately flushed to stable 
                     storage. When the log records are correctly 
                     secured, recovery can redo the operation, if it 
                     is necessary.
                     3. Transactional locks are released.

  [1]: https://learn.microsoft.com/en-US/troubleshoot/sql/admin/logging-data-storage-algorithms
Run Code Online (Sandbox Code Playgroud)

  • “如果日志缓存记录未刷新到磁盘/未硬化,是的,它将丢失”:如果发生这种情况,那将是在用户收到“提交”确认之前,所以是的,它将丢失,但不会导致结果数据库不一致,因为事务不会被提交 (3认同)