I often see statements like sql server log records every transction and opeation.
But I am confused about what happens when a transaction is eventually rolled back.
Say an explicit transaction has 3 statements: statement A
, statement B
, statement C
, and finally a rollback
statement D
.
Now say when the execution has not reached the rollback statement D
, will the modifications resulting from statements A through C
get recorded to sql server log?
Understanding 1:
Statements A through D all get recorded. SQL Server records everything, no matter what.
Understanding 2:
Modifications are only stored somewhere in memory, and only recorded to log when SQL Server see a commit
statement. If it turns out to be a rollback
statement, SQL Server simply ignore the transacrion, no writing to log happens because it serves no purpose. In other words, SQL Server logs when there is a net result before and after the transactions.
Both seems logical, at least to me but they can't both be right. Thanks for any help.
Best Answer
Understanding 1 is correct. SQL Server records every operation that changes data to the transaction log. A rollback is a change to data so it also records that to the transaction log as well. As statement A run it will write data to the transaction log and will also reserve data in the transaction log in case statement A needs to be rolled back. The same is true for B and C. When you rollback the transaction more information will be written to the log.
There are plenty of ways to see this in action so below is a quick demo. Here is the query that I'll use to see what was written to the log:
My table:
Query A uses minimal logging:
After A:
Query B does not use minimal logging:
After B:
Query C changes less data:
After C:
Now I will issue a
ROLLBACK
and query the DMV while the rollback happens. Below is a table of a few snapshots:During the
ROLLBACK
, the used bytes increases and the reserved number of bytes decreases. That's because SQL Server is using the space it set aside before to undo the transaction. To undo the transaction it must change data so it writes more data to the log.