How to read the transaction log for SQL Server

Source: Internet
Author: User

Brief introduction

This article describes what information is logged in the transaction log of SQL Server and how to read the information in these transaction logs. First, a Microsoft is not exposed to the function Fn_dblog, in the next part of the article mainly use this function to read the transaction log.

    1. Fn_dblog (@StartingLSN, @EndingLSN)
    2. [email protected]: Represents the starting LSN number, or null if the query starts from the first log record.
    3. [email protected]: Indicates the end of the LSN number, or null value indicates the query to tail log records.
    4. - It is important to note that the LSN we see here is hexadecimal, and the parameter here needs to be converted to decimal, as 00000021:00000077:0003 is converted to 33:119:3 when passed as a parameter to Fn_dblog .
Body
  1. --Create a test database
  2. Use [master];
  3. GO
  4. CREATE DATABASE TestDB;
  5. GO
  6. --Create a table
  7. Use TestDB;
  8. GO
  9. CREATE TABLE [Location] (
  10. [sr.no] intidentity,
  11. [Date] Datetimedefault GETDATE (),
  12. [City] CHAR (+) DEFAULT 'Xiamen');

A database named TestDB was created with the code above, and a table of three fields was created. Then take a look at the contents of the transaction log

  1. Use TestDB;
  2. GO
  3. Select [Current LSN],
  4. [Operation],
  5. [Transaction Name],
  6. [Transaction ID],
  7. [Transaction SID],
  8. [SPID],
  9. [BeginTime]
  10. from fn_dblog(null,null)

As you can see, a total of 195 rows of log records were generated, and I intercepted some of the results and recorded the actions of the corresponding LSN in the Operation column, where Lop_begin_xact represents the beginning of a transaction, Transaction name shows the name of the database being created, The Trasaction ID, however, records the corresponding transaction ID. Listed below are some of the more common and important values of operation

  • lop_begin_xact the beginning of a transaction
  • lop_lock_xact Get lock
  • Lop_modify_row Modify Rows ( specifically modified objects can view allocunitname)
  • lop_commit_xact Commit a transaction
  • lop_delete_rows Delete Data
  • lop_insert_rows Inserting Data

Next, insert the following into the table - data, and view the corresponding transaction log, the code is as follows:

  1. INSERT into location defaultvalues
  2. GO -
  3. GO
  4. SELECT
  5. [Current LSN],
  6. [Transaction ID],
  7. [Operation],
  8. [Transaction Name],
  9. [CONTEXT],
  10. [Allocunitname],
  11. [Page ID],
  12. [Slot ID],
  13. [BeginTime],
  14. [EndTime],
  15. [Number of Locks],
  16. [Lock Information]
  17. from sys. Fn_dblog (null,null)
  18. WHERE operation = 'lop_insert_rows' and allocunitname = 'dbo. Location '

Get the result as shown, the number of rows returned with US insert The same number of times, then take one of them Take a look at it once insert What actions are recorded in the transaction log.

    1. SELECT
    2. [Current LSN], [Transaction ID], [operation], [Transaction Name], [CONTEXT], [Allocunitname], [Page id], [Slot id], [ BeginTime], [EndTime], [number of Locks], [Lock information]
    3. from sys. Fn_dblog (null,null)
    4. WHERE [Transaction ID] = '0000:000002fc'

can see this transaction Details of the steps performed

    • in 2014/05/25 18:35:39:197 transaction start
    • < Span style= "font-family: the song Body;" > in heap table dbo. Location pageid 0001:0000004f insert data Span style= "Font-family:verdana;" >
    • in Span style= "Font-family:verdana;" >2014/05/24 18:35:39:200 COMMIT transaction

The following paragraph is my Lock Information field to copy out the content, to see in detail

    1. HoBt 72057594039042048:acquire_lock_ix object:6:245575913:0; Acquire_lock_ix page:6:1:79; Acquire_lock_x rid:6:1:79:0

let's verify by following the code that this one INSERT The lock information obtained by the statement

  1. DBCC TRACEON ( -1,3604)
  2. DBCC TRACEON ( -1,1200)--View the lock information for the current session
  3. BEGIN TRAN
  4. INSERT into location defaultvalues
  5. ROLLBACK TRAN
  6. DBCC Traceoff ( -1,1200)
  7. DBCC Traceoff ( -1,3604)
  8. /*
  9. Process acquiring IX lock on object:6:245575913:0 (class bit2000000 ref1) Result:ok
  10. Process acquiring IX lock on page:6:1:79 (class bit2000000 ref0) Result:ok
  11. Process acquiring X lock on rid:6:1:79:90 (class bit2000000 ref0) Result:ok
  12. */

can see Lock Information The information recorded in the field is - The information for the trace flag lock output is consistent.

also from the transaction log, you can see SQL Server some of the internal operations and see these operations for some specific information, such as the start time, the number of times, the steps of the operation, and so on. Next look at the page split action

  1. --Find the transaction of the page split action
  2. SELECT
  3. [Current LSN], [Transaction ID], [operation], [Transaction Name], [CONTEXT], [Allocunitname], [Page id], [Slot id], [ BeginTime], [EndTime], [number of Locks], [Lock information]
  4. from sys. Fn_dblog (null,null)
  5. WHERE [Transaction Name] = 'SplitPage'
  6. --view actions in specific transaction
  7. SELECT
  8. [Current LSN], [Transaction ID], [operation], [Transaction Name], [CONTEXT], [Allocunitname], [Page id], [Slot id], [ BeginTime], [EndTime], [number of Locks], [Lock information]
  9. from sys. Fn_dblog (null,null)
  10. WHERE [Transaction ID] = '0000:000002f8'

Conclusion

Knowing what is logged in the transaction log makes it easier for us to understand the execution of some of the operations that SQL Server does.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.