1 Demystifying the Transaction Log. 2 Rob Canzonire Advisory Database Administrator at Blue Cross Blue Shield of Louisiana Microsoft Certified Solutions.

Slides:



Advertisements
Similar presentations
Optimizing Procedural Code Understanding Logging, Recovery, Locking and Performance Presentation Addition Understanding/Optimizing VLFs Kimberly L. Tripp.
Advertisements

©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
Module 4 Working with Databases. Module Overview Overview of SQL Server Databases Working with Files and Filegroups Moving Database Files.
FlareCo Ltd ALTER DATABASE AdventureWorks SET PARTNER FORCE_SERVICE_ALLOW_DATA_LOSS Slide 1.
SQL Server Best Practices Keep Your Database In Top Performance Shape and Maintain Effective Backups September, 2007 Richard Kokoski.
Recap of Feb 27: Disk-Block Access and Buffer Management Major concepts in Disk-Block Access covered: –Disk-arm Scheduling –Non-volatile write buffers.
Harvard University Oracle Database Administration Session 5 Data Storage.
Proper Care and Feeding of your SQL MDB -Recommendations for General MDB Maintenance -Read the notes on the foils! -Revised October
Working with SQL and PL/SQL/ Session 1 / 1 of 27 SQL Server Architecture.
Oracle9i Database Administrator: Implementation and Administration
Database Configuration and Maintenance Database Configuration and Maintenance Exam / 30.
Transaction log grows unexpectedly
DBA Quick Start What do you do when you’ve fallen into a job as a DBA? Take a deep breath, Find out what you’ve got, And dive right in!
Building Highly Available Systems with SQL Server™ 2005 Vineet Gupta Evangelist – Data and Integration Microsoft Corp.
SQL Server 2008 Implementation and Maintenance Chapter 7: Performing Backups and Restores.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
Chapter 2: Designing Physical Storage MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)
Chapter Oracle Server An Oracle Server consists of an Oracle database (stored data, control and log files.) The Server will support SQL to define.
Module 3: Managing Database Files. Overview Introduction to Data Structures Creating Databases Managing Databases Placing Database Files and Logs Optimizing.
7202ICT Database Administration Lecture 7 Managing Database Storage Part 2 Orale Concept Manuel Chapter 3 & 4.
Architecture Rajesh. Components of Database Engine.
Page 1 SQL Server Myths XV ENCONTRO DA COMUNIDADE SQLPORT Rui Ribeiro MCITP 2011/08/16.
1 Chapter 14 DML Tuning. 2 DML Performance Fundamentals DML Performance is affected by: – Efficiency of WHERE clause – Amount of index maintenance – Referential.
Module 16: Performing Ongoing Database Maintenance
MS SQL by: Bryan Bankhead CIS 407. General Concepts  Backing up and Restoring databases and transaction logs is a way that SQL Server provides protection.
Pedro Azevedo Lopes Premier Field Engineer Microsoft Corporation.
Database structure and space Management. Database Structure An ORACLE database has both a physical and logical structure. By separating physical and logical.
Transactions and Locks A Quick Reference and Summary BIT 275.
March 23 & 28, Csci 2111: Data and File Structures Week 10, Lectures 1 & 2 Hashing.
March 23 & 28, Hashing. 2 What is Hashing? A Hash function is a function h(K) which transforms a key K into an address. Hashing is like indexing.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Transaction Log Internals Dharmendra Keshari President - KDSSG.
Backup and Recovery - II - Checkpoint - Transaction log – active portion - Database Recovery.
Data Disaster Recovery Planning Greg Fibiger 1/7/2016.
Oracle Architecture - Structure. Oracle Architecture - Structure The Oracle Server architecture 1. Structures are well-defined objects that store the.
# CCNZ What is going on here???
DBA Quick Start What do you do when you’ve fallen into a job as a DBA? Take a deep breath, Find out what you’ve got, And dive right in!
Agenda for Today  DATABASE Definition What is DBMS? Types Of Database Most Popular Primary Database  SQL Definition What is SQL Server? Versions Of SQL.
Hitting the SQL Server “Go Faster” Button Rob Douglas #509 | Brisbane 2016.
SQL Server Storage Inside. About Hemantgiri S. Goswami Hemantgiri S. Goswami is a Lead Database Consultant for Pythian, a company head quartered in Ottawa,
SQL Backups for Beginners by Mark Gordon
Database Administration
Transaction Log Fundamentals
Inside transaction logging
Module 11: File Structure
SharePoint Solutions Architect, Protiviti
Hitting the SQL Server “Go Faster” Button
Finding more space for your tight environment
Chapter Overview Understanding the Database Architecture
SQL Backups for Beginners by Mark Gordon
The Vocabulary of Performance Tuning
Hitting the SQL Server “Go Faster” Button
The Vocabulary of Performance Tuning
Oracle9i Database Administrator: Implementation and Administration
Backup and Restore your SQL Server Database
Inside transaction logging
Turbo-Charged Transaction Logs
Transaction Log Fundamentals
The Vocabulary of Performance Tuning
Transaction Log Internals and Performance David M Maxwell
A Beginners Guide to Transactions
A Beginners Guide to Transactions
Transaction Log Performance Tuning
A Beginners Guide to Transactions
A Beginners Guide to Transactions
The Vocabulary of Performance Tuning
A Beginners Guide to Transactions
Accelerated DATABASE RECOVERY
Presentation transcript:

1 Demystifying the Transaction Log

2 Rob Canzonire Advisory Database Administrator at Blue Cross Blue Shield of Louisiana Microsoft Certified Solutions Expert - Data Platform SQL Server MCSE Microsoft Certified Solutions Associate- Data Platform SQL Server MCSA Microsoft Database Administrator SQL Server MCITP Microsoft Certified Technology Specialist SQL Server 2008 – MCTS PMI – Project Management Professional – PMP Sybase -Adaptive Server Enterprise Administrator Processional Sybase – Adaptive Server Enterprise 15.0 Administrator Associate

3 What do I want to cover ?? Why Does the Transaction Log Exist How the Transaction Log Works How Are Log Records Written to the Transaction Log How the Transaction Log Can Become an IO Bottleneck How to Deal with Transaction Log Bottlenecks and Boost Performance (Quick Overview)

4 Transaction Logs The transaction log stores a record of all the data modifications performed in a database. In the event of an unexpected shut-down, the data in the transaction log can be used during recovery to roll forward any transactions completed, but not written to the database file on disk at the time of the shut-down. In addition, any uncompleted transactions that were partially been written to the database file on disk before the failure can be rolled back during recovery. Both of these actions ensure database integrity and the ACID properties of transactions. This is the reason the Transaction Log exists.

5 How do we think of database files ? MDF File - (ignore NDF files) LDF File

6 Looking at the MDF File 64 KB Extent 8 K Pages SQL Server always works with data (reads and writes) at the page level. Some data pages may be full, others partially full, and others empty.

7 LDF File in Detail LDF files are not divided into pages like MDF files. Instead, they are divided into virtual log files (VLFs), often of uneven sizes from small to very large. They are units of truncation for log backups. SQL Server creates the VLFs and determines their sizes (although we can influence this). LDF files are circular, and don’t grow if they don’t have to. Unlike database pages, which hold rows of data, VLFs store log records. Log records are written sequentially in the log file, into available VLFs as needed. In other words, the log records for a single transaction can overflow from one VLF to another.

8 What are the Log Records Every modification to the database creates multiple log records. Log records describe what is being changed and what the change was. A log record’s size will vary, depending on the nature of the change. Log records are written sequentially (although mixed with other log records) to the log file and stored in VLFs, as previously discussed. Besides log records, extra space in the log is reserved in case a roll back occurs and compensation log records need to be written to the log. This is one reason why logging takes up more space that the actual modification. Log records are assigned LSNs(Log Sequence Number), an ever-increasing, three-part number that uniquely defines the position of a log record within the transaction log, they are critical to identify which log records need to be rolled back or rolled forward.

9 DBCC LOG ( Database_Name,Option) Show you the records in the transaction log. 0 = Minimal information providing the LSN, Operation, Context, Transaction ID and if a Log Block was generated 1 = Same data as output type 0 plus the record length, previous LSN, and a description of the log record 2 = Same data as output type 1 plus the Allocation Unit ID, object name, SQL Server Page, Slot location within the page, and locking information 3 = Full log record 4 = Similar data as output type 1 including a hex dump of the log record

10 DBCC OPENTRAN() DBCC SQLPERF(LOGSPACE) DBCC LOGINFO DBCC OPENTRAN() - Displays information about the oldest active transaction and the oldest distributed and non distributed replicated transactions. Results are displayed only if there is an active transaction or if the database contains replication information. An informational message is displayed if there are no active transactions DBCC SQLPERF(LOGSPACE) - Returns the current size of the transaction log and the percentage of log space used for each database. DBCC LOGINFO - displays information about the number, sizes, and status of the virtual log files internally to the transaction log. It is an undocumented command and provides you information only.

11 DEMO DBCC LOG (Database_Name,2) DBCC SQLPERF (LOGSPACE) DBCC Loginfo

12 VLF can be in One of Four States Start of Physical Log File End of Physical Log File A VLF can be in any one of four different states: Active: Includes at least one active log record that is part of the active log. Active records still need to be used for some purpose. (e.g. active transaction, replication, mirroring) Recoverable: No active log records, but not available. Space cannot be reused until transaction log backup occurs. Reusable: Inactive log records. Space can be reused. Unused: No log records have ever been recorded in it. VLF is zeroed out

13 Simple Transaction Log Example Start Physical File End Physical File Recoverable Active Unused Logical Log Let’s assume the transaction log is in the above state. The logical log is the part of the log that includes the first active VLF through the last active VLF

14 Log Records Are Added Start Physical File End Physical File Recoverable Active Logical Log Let’s assume that data has been added and the transaction log has grown to the full size of the physical LDF file. What would happen if the transaction log needed more space?

15 Transaction Log Backup is Performed Start Physical File End Physical File Reusable Active Logical Log Let’s assume that a transaction log backup was performed. Notice how recoverable VLFs (1-2) change to reusable VLFs. Note that VLF 3 is still active, as it still has active log records

16 Log Records Added After Log Backup Start Physical File End Physical File Active Logical Log Let’s assume that more log records have been added. Notice that the transaction log wraps around from end to front. What would happen if more data were added now?

17 DEMO LOG Backup

18 Virtual Log Files: Too Many When the number of VLFs in a transaction log become too many, performance can suffer greatly. This includes DML, backup, and recovery performance. At a point of about VLFs or so, performance can begin to degrade. To find out how many VLFs you have in a log file, run this code: “DBCC LOGINFO (database_name)”. The number of rows returned is the number of VLFs in the database’s log file.

19 Virtual Log Files: Too Many If the number of VLFs exceeds (depending on how large the log file is), consider manually shrinking the log file, and then manually growing it to create the “optimal” number of VLFs for your environment. By default, the number of VLFs created is: Amounts less than 64 MB = 4 VLFs Amounts of 64 MB to less than 1 GB = 8 VLFs Chunks of 1 GB or larger = 16 VLFs The main reason there are too many VLFs is because autogrowth is used to grow the LDF file.

20 Virtual Log Files: Too Few If the log file will be 8 GB or larger, it is recommended that they are manually grown 4 GB at a time to ensure that the VLFs are not so large that it takes a long time for a transaction log to be cleared. In other words, you can have too few VLFs, which can prevent VLFs from being freed up on a timely basis during transaction log backups. In large databases, try to keep VLFs no larger than 500 MB or so.

21 Pre allocate Transaction Log File Size Pre-sizing your log files to their expected size offers several benefits as it prevents auto growth from kicking in, which can cause problems. Some of the benefits include: Reduces physical file fragmentation. Prevents too many VLFs from being created. Prevents unexpected time delays (blocking) as the transaction log is growing.

22 Separate Data and Log Files You have probably heard this advice a hundred times, but it is important to separate your data (MDF, NDF) and log (LDF) files on separate arrays in order to reduce contention. This is one of the easiest things you can do, and is effective in reducing IO contention between data and log files. In a perfect world with an unlimited budget, each log file should be on its own array, as mixing multiple log files on the same array will introduce a lot of random reads and writes. The more log files there are on the same shared array, the bigger this problem becomes. At the very least, put high-performance, mission critical logs on their own array.

23 Use Standard Performance Tuning Techniques Only perform as much DML or DDL as required. Keep transactions short. Minimize the possibility that a transaction has to be rolled back. Rollback sooner than later. Eliminate redundant or unused indexes (including indexed views), as they all need to maintained, and maintenance requires log activity. Take steps to minimize page splitting. For example, use a monotonically increasing clustered index key for each of your tables, and select a fill factor that minimizes page splitting.

24 Perform Transaction Log Backups Often Every time a transaction log backup is made, unused portions of it are cleared out and can be used for new logging. (Do it a alot) The more often you perform transaction log backups, the quicker they take, helping to reduce overhead on the transaction log. If you don’t back up the transaction logs often, and they grow large, there will be a larger IO hit during the transaction log backup operation. So, smaller and more frequent transaction log backups are preferred over larger and less frequent transaction log backups.

25 Use Minimally Logged Operations If Appropriate While all database modifications are logged in SQL Server, some database modifications can be minimally logged, which incur less IO overhead than fully logged operations. These include BCP, BULK INSERT, INSERT INTO, SELECT INTO, among several others. For these operations to be minimally logged, the database has to be using the bulk-logged or simple recovery models.

26 DEMO

27 Conclusion Transaction log has a critical role in SQL Server, you need to pay attention to it. By using best practices, you can help reduce or eliminate any transaction log bottlenecks and boost performance Transaction logs can become an IO bottleneck for busy databases because all activity is logged. If you don’t have a transaction log IO bottleneck, don’t be complacent. Be a proactive DBA and implement as many of the above strategies to mitigate any potential future transaction log bottlenecks.

28 Rob Canzonire Website