Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interrogating the Transaction Log

Similar presentations


Presentation on theme: "Interrogating the Transaction Log"— Presentation transcript:

1 Interrogating the Transaction Log
Or, The Transaction Log: Why You Can’t Run Your Database Without It Interrogating the Transaction Log

2

3 What We’ll Cover ACID Properties Transactions Write-ahead Logging
Log Space Reservation Reading the Log with fn_dblog Identifying Active Transactions Finding Log Records Associated with Active Transactions The log file is the most important file in your database and I’ll explain the reasons why in the course of this presentation I’ll also explain how the log file works I will demonstrate methods for looking at the records contained in the transaction log Finally, I’ll show how to identify the log records that are associated with active transactions on the instance

4 Database 101 - ACID Properties
Atomicity Consistency Isolation Durability ACID properties are central to any relational database SQL Server uses the transaction log to adhere to the ACID properties Atomicity – transactions are all or nothing Consistency – Any transaction that modifies the database must leave the data in a consistent state, including triggers and constraints Isolation – No active transaction can affect another transaction; SQL Server uses a combination of transactions and resource locking to achieve isolation Durability – Any committed transaction must be maintained in the database in the event of a system crash

5 Human sacrifice, dogs and cats living together... mass hysteria!
A database that doesn’t adhere to ACID properties gets you… Human sacrifice, dogs and cats living together... mass hysteria!

6 Transactions and Write-Ahead Logging
What is a transaction? Explicit v. Implicit Transactions Write-ahead logging Roll back and roll forward Any activity the modifies the data or schema of a database will be contained in a transaction A block of code can be wrapped in a BEGIN TRANSACTION/COMMIT TRANSACTION, known as an explicit transaction For any statement or series of statements that modify the database not contained in a BEGIN/COMMIT, SQL Server will wrap them in a BEGIN/COMMIT Write-ahead logging guarantees that log records are written to disk prior to the associated data pages If the system crashes prior to the data pages being written, committed transactions will be rolled forward and and uncommitted transactions will be rolled back All transactional activity is recorded in the transaction log

7 Log Space Reservation SQL Server uses log space for transaction records Additional space is reserved for potential rollback activity Committed transaction releases reserved space To account for the possibility of a transaction roll back, SQL Server reserves space in the transaction log If a transaction rolls back, SQL Server generates an anti-operation for each log record Once a transaction completes, the reserve

8 Log Space Reservation Demo
SMALLER TRANSACTIONS ARE BETTER!

9 Reading the Transaction Log
Table-based function that allows the transaction log to be read Takes 2 parameters, starting and ending log sequence numbers (LSN) SELECT * FROM fn_dblog(NULL,NULL) returns the contents of the log fn_dblog is a table-based functions that allows you to select the contents of the transaction log It takes two input parameters, starting and ending lsn numbers Passing NULLs will return the entire contents of the log Passing in lsn values will limit the log records returned The transaction log acts as the stream of consciousness for the database; if you pass in a begin and end lsn, you will return all of the records for that range but may see multiple transactions

10 fn_dblog demo

11 sys.dm_tran_database_transactions
Returns information about active transactions Results contain the begin and end LSN for each transaction

12 Finding Log Records for Active Transactions – Attempt #1
Both the DMV and function contain a transaction_id The DMV id is hexadecimal and the function id is decimal, so converting one of them should work, right?

13 Image Source - http://4. bp. blogspot

14 Finding Log Records for Active Transactions – Failed Attempt #1
Converting the transaction IDs from either source did not result in a match After several attempts to make it work (octal, double and add 30, multiply by 9/5 and add 32) I wrote Paul Randal His response? “They don’t match. It’s annoying.”

15 Finding Log Records for Active Transactions – Attempt #2
Both the DMV and the function contain begin and end LSNs The function returns colon-delimited hex LSNs ( a:000027a5:0001) The DMV returns a decimal value ( )


Download ppt "Interrogating the Transaction Log"

Similar presentations


Ads by Google