FIX: Slow performance when you recover a database if there are many VLFs inside the transaction log in SQL Server 2005, in SQL Server 2008 or in SQL Server 2008 R2

Article translations Article translations
Article ID: 2455009 - View products that this article applies to.
Microsoft distributes Microsoft SQL Server 2005, 2008 and 2008 R2 fixes as one downloadable file. Because the fixes are cumulative, each new release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2005, 2008 and 2008 R2 fix release.
Expand all | Collapse all

On This Page

Symptoms

Consider the following scenario:
  • You install Microsoft SQL Server 2005, Microsoft SQL Server 2008 or Microsoft SQL Server 2008 R2 on a computer.
  • One or more of the databases are in the full or bulk-logged recovery model.
  • The initial size of a transaction log file for the database is small. However, the transaction file grows large.
  • The autogrow growth increment of the transaction log file is configured to a small size. For example, the autogrow growth increment is configured to one megabyte or to one percent. Or, you make the transaction log file increase by using a small manual growth.
  • The transaction log file grows by the small growth increment. After a while, the transaction log file is large.

    For example, the transaction log file grows from one hundred megabytes to ten gigabytes. The transaction log file grows by one megabyte increment in each growth.
In this scenario, you may experience the following issues:
  • Database recovery is slow when the database is started if there is a large volume of work to recover.

    For example, a transaction is pending open for a long time. Or, lots of transactions that are small in size are not committed when the SQL Server is shut down or restarted. In this case, you may receive an error for a long estimated recovery time in the analysis phase, in the undo phase, or in the redo phase. The error resembles the following and is logged in the SQL Server 2005 error log file:
    Analysis of database 'mydatabase' (7) is 0% complete (approximately 1234 seconds remain). This is an informational message only. No user action is required.
  • Database recovery is slow when a full database backup file and an additional transaction log sequence are restored if the database is brought online.

    Note To bring the database online during the restore operation, use the WITH RECOVERY syntax or the WITH STANDBY = <options> syntax.
  • Database mirroring recovery is slow on a partner server in the mirroring pair.
  • The progress of the log reader agent for transactional replication is slow for the following operations:
    • The transaction log of a publisher database is parsed.
    • A command is added into a distribution database.
    • The transactions are replicated.

    Note For more information, visit the following Microsoft Developer Network (MSDN) website:
    949523 The latency of a transactional replication is high in SQL Server 2005 when the value of the "Initial Size" property and the value of the Autogrowth property are small
  • Slow performance occurs when a database snapshot is created if many transactions are logged or if the transaction logs stay active for a long time. This issue occurs because the snapshot has to run recovery and the transaction logs have to be rolled back.
  • Slow performance occurs when you use the DBCC CHECKDB syntax to create a hidden database snapshot that has to fully recover the database snapshot to run the consistency checks.

Cause

These issues occur because the Virtual Log Files (VLFs) in a transaction log file might cause slow performance when SQL Server scans through these VLFs during the following operations:
  • Database recovery
  • Database mirroring
  • Database snapshot
  • Database transactional replication log reader activities
When a transaction log file for a database is grown by small increments during extreme growth conditions, thousands to hundreds of thousands of VLFs are present inside a single set of transaction log (.ldf) files. However, large log growth by large increments creates only several VLFs by comparison. For example, several might be less than 100 VLFs.

Note The VLFs are the divisions or segments that are used internally by SQL Server in the transaction log file.

Resolution

Cumulative update information

SQL Server 2008 R2

The fix for this issue was first released in Cumulative Update 6. For more information about how to obtain this cumulative update package for SQL Server 2008 R2, click the following article number to view the article in the Microsoft Knowledge Base:
2489376 Cumulative Update package 6 for SQL Server 2008 R2
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2008 R2 fix release. We recommend that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
981356 The SQL Server 2008 R2 builds that were released after SQL Server 2008 R2 was released

SQL Server 2008 Service Pack 1

The fix for this issue was first released in Cumulative Update 12 for SQL Server 2008 Service Pack 1. For more information about this cumulative update package, click the following article number to view the article in the Microsoft Knowledge Base:
2467236 Cumulative update package 12 for SQL Server 2008 Service Pack 1
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2008 fix release. Microsoft recommends that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
970365 The SQL Server 2008 builds that were released after SQL Server 2008 Service Pack 1 was released
Microsoft SQL Server 2008 hotfixes are created for specific SQL Server service packs. You must apply a SQL Server 2008 Service Pack 1 hotfix to an installation of SQL Server 2008 Service Pack 1. By default, any hotfix that is provided in a SQL Server service pack is included in the next SQL Server service pack.

SQL Server 2008 Service Pack 2

The fix for this issue was first released in Cumulative Update 2 for SQL Server 2008 Service Pack 2. For more information about this cumulative update package, click the following article number to view the article in the Microsoft Knowledge Base:
2467239 Cumulative update package 2 for SQL Server 2008 Service Pack 2
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2008 fix release. Microsoft recommends that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
2402659 The SQL Server 2008 builds that were released after SQL Server 2008 Service Pack 2 was released
Microsoft SQL Server 2008 hotfixes are created for specific SQL Server service packs. You must apply a SQL Server 2008 Service Pack 2 hotfix to an installation of SQL Server 2008 Service Pack 2. By default, any hotfix that is provided in a SQL Server service pack is included in the next SQL Server service pack.

SQL Server 2005 Service Pack 3

The fix for this issue was first released in Cumulative Update 13 for SQL Server 2005 Service Pack 3. For more information about this cumulative update package, click the following article number to view the article in the Microsoft Knowledge Base:
2438344 Cumulative update package 13 for SQL Server 2005 Service Pack 3
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2005 fix release. Microsoft recommends that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
960598 The SQL Server 2005 builds that were released after SQL Server 2005 Service Pack 3 was released
Microsoft SQL Server 2005 hotfixes are created for specific SQL Server service packs. You must apply a SQL Server 2005 Service Pack 3 hotfix to an installation of SQL Server 2005 Service Pack 3. By default, any hotfix that is provided in a SQL Server service pack is included in the next SQL Server service pack.

SQL Server 2005 Service Pack 4

The fix for this issue was first released in Cumulative Update 1 for SQL Server 2005 Service Pack 4. For more information about this cumulative update package, click the following article number to view the article in the Microsoft Knowledge Base:
2464079 Cumulative update package 1 for SQL Server 2005 Service Pack 4
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2005 fix release. Microsoft recommends that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
2485757 The SQL Server 2005 builds that were released after SQL Server 2005 Service Pack 4 was released
Microsoft SQL Server 2005 hotfixes are created for specific SQL Server service packs. You must apply a SQL Server 2005 Service Pack 4 hotfix to an installation of SQL Server 2005 Service Pack 4. By default, any hotfix that is provided in a SQL Server service pack is included in the next SQL Server service pack.

Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

Workaround

Install this hotfix and restart SQL Server might improve the recovery performance. For example, the performance might be improved for the following operations after you install this hotfix and restart SQL Server:
  • Database recovery
  • Database mirroring
  • Database snapshot
  • Database transactional replication log reader activities

If you cannot install this hotfix, you can perform the following workarounds to mitigate an existing issue and to prevent a future occurrence.

Mitigate an existing issue

  • Wait for the restore or recovery operation to complete

    If you have a non-recovered database that is experiencing the slow performance when you restore or recovery the database, you may have to wait for the restore or recovery operation to be completed. For example, you might see the offline status or the recovering status in SQL Server Management Studio (SSMS) for a non-recovered database. Stopping SQL Server usually offers no relief for a slow recovery and may take more time to repeat the same recovery analysis phase, redo phase, or undo phase.
  • Avoid restoring the transaction log sequence that contains thousands of VLFs

    If you experience the slow performance while you restore and recover a database by using a backup file, you can avoid restoring the transaction log sequences that contain thousands of VLFs. To identify the backup file that has the most the virtual log files recorded, use the following statement to see the FirstLSN and LastLSN columns in the log backup files:
    RESTORE HEADERONLY FROM DISK='C:\folder\file.trn'

    You can decide to avoid restoring the log backup files. Or, you can use the STOP AT statement in the RESTORE commands to avoid the highly fragmented parts of the transaction logs. If you do not fully restore the log sequences up to the latest point in time during a failure recovery scenario, data loss occurs in your database SQL Server. This data loss occurs because not all transactions are being kept. Therefore, there is a business tradeoff decision. You can fully restore a highly fragmented transaction log. However, this operation may take many hours. Or, you can use the STOP AT statement in the recovery to stop the recovery before the highly fragmented part of the log. However, any missing transactions that you omit are lost.

    Note Without installing this hotfix, there is typically no safe recourse for expedited recovery after you restart SQL Server. SQL Server has to locate the list of VLFs to analyze the log files, to redo completed transactions, and then to undo incomplete transactions to finish recovery to bring the database online safely. You cannot safely skip transactions during recovery.

Prevent a future occurrence

  • Set the autogrow increment of the database to an appropriate size

    If the size of the autogrow increment is too small, there will be many Virtual Log Files (VLFs), and you may experience slow performance in SQL Server. If the size of the autogrow increment is too large, queries that make the transaction logs grow automatically may have to wait for a long time to finish a growth. Therefore, a time-out error may occur in SQL Server. To work around these issues, you can set the size of the autogrow increment for your database to an appropriate size.
  • Eliminate the large number of VLFs, and use a manual growth

    If many VLFs are in the transaction log, reduce the size of the transaction log, and make it increase before peak business to meet the demand by using a manual growth. For example, the transaction log increases back to a reasonable average size in a large increment or in a single manual growth. Therefore, the size of the transaction log reaches a peak capacity, and the log backup files are scheduled on a frequent and periodic basis. Additionally, the transaction log might be truncated, and the VLFs for the transaction log can be reused in a cycle.
  • How to shrink and increase the transaction log manually

    To correct a log that has too many VLFs, follow these steps to shrink the log and increase it again manually:
    1. If the database is in a full or bulk-logged recovery model, you have to back up the transaction log to allow for the active VLFs to be truncated and to be reused.
      BACKUP LOG databasename TO DISK='C:\folder\log_backupfile.trn'

      For more information about how to back up the transaction log file by using SSMS, visit the following Microsoft Developer Network (MSDN) website:
      How to back up the transaction log file by using SSMS
      For more information about how to back up the transaction log file by using Transact-SQL statements, visit the following MSDN website:
      How to back up the transaction log file by using Transact-SQL statements
    2. To determine the logical name of the transaction log file, run one of the following statements.
      Statement 1
      exec sp_helpfile 
      Statement 2
      select * from sys.sysfiles 
      To reduce the size of the transaction log file to the desired size, use the following code:
      DBCC SHRINKFILE(transactionloglogicalfilename, TRUNCATEONLY) 
    3. You can increase the size of the transaction log file to an appropriate size. We recommend that you let the size of the transaction log file grow to the peak regular size. Therefore, the autogrow increment is avoided. To set the size of the transaction log, use the Database Properties page in SSMS, or use the following ALTER DATABASE syntax:
      MODIFY FILE ( NAME = transactionloglogicalfilenae, SIZE=newtotalsize MB) 

      For more information about how to increase the size of a database in SSMS, visit the following MSDN website:
      How to increase the size of a database in SSMS
      For more information about the ALTER DATABASE MODIFY FILE syntax, visit the following MSDN website:
      General information about ALTER DATABASE MODIFY FILE syntax

More information

You can check the number of VLF segments by reviewing the SQL error log file and then by finding the log sequence number (LSN) in each transaction log backup file. The first digits before the colon symbol in the LSNs correspond to the number of the LSN.

For example, the first number in the first informational message for the LSN is 1. However, the first number in the second informational message for the LSN is 100001. In this scenario, there are 100,000 VLFs that are used between the time of the first informational message and of the second informational message. Therefore, the logged fragmented transaction log that has many Virtual Log Files (VLFs) resembles the following:

{Log was backed up. Database: mydbname, creation date(time): 2010/07/08(12:36:46), first LSN: 1:5068:70, last LSN: 1:5108:1, number of dump devices: 1, device information: (FILE=1, TYPE=DISK: {'C:\folder\logbackup1.trn'}). This is an informational message only. No user action is required.

Log was backed up. Database: mydbname, creation date(time): 2010/07/08(15:36:46), first LSN: 100001:5108:1, last LSN: 100002:5108:1, number of dump devices: 1, device information: (FILE=2, TYPE=DISK: {'C:\folder\logbackup2.trn'}). This is an informational message only. No user action is required.}

The following table provides more information about the products or tools that automatically check for the condition that is described in the "Symptoms" section in your instance of SQL Server and in the versions of SQL Server against which the rule is evaluated.

Collapse this tableExpand this table
Rule softwareRule titleRule descriptionProduct versions against which the rule is evaluated
System Center AdvisorSQL Server with Transactional Replication, the Log Reader Agent Performance may be impacted due to size of transaction log or number of VLFIn this SQL Server instance advisor detected the presence transactional replication with number of VLFs or TLOG size considerably greater. The Log Reader Agent Performance is negatively impacted by the size of the transaction log or number of VLF. Reduce the Transaction Log Size and the number of VLF to improve Log Reader Agent performance.SQL Server 2008

SQL Server 2008 R2

SQL Server 2012



References

For more information about the physical architecture of the transaction log, visit the following MSDN website:
General information about the physical architecture of the transaction log

For more information about log sequence numbers (LSN), visit the following MSDN website:
General information about log sequence numbers

For more information about the 1413 error when database mirroring starts, visit the following MSDN website:
General information about the 1413 error when database mirroring starts

For more information about how a log file structure can affect database recovery time, visit the following MSDN website:
How a log file structure can affect database recovery time

For more information about the transaction log VLFs, visit the following MSDN website:
General information about the transaction log file

For more information about how to create a database snapshot, visit the following MSDN website:
How to create a database snapshot
For more information about the Incremental Servicing Model for SQL Server, click the following article number to view the article in the Microsoft Knowledge Base:
935897 An Incremental Servicing Model is available from the SQL Server team to deliver hotfixes for reported problems
For more information about the naming schema for SQL Server updates, click the following article number to view the article in the Microsoft Knowledge Base:
822499New naming schema for Microsoft SQL Server software update packages
For more information about software update terminology, click the following article number to view the article in the Microsoft Knowledge Base:
824684 Description of the standard terminology that is used to describe Microsoft software updates

Properties

Article ID: 2455009 - Last Review: January 6, 2014 - Revision: 5.0
Applies to
  • Microsoft SQL Server 2005 Developer Edition
  • Microsoft SQL Server 2005 Enterprise Edition
  • Microsoft SQL Server 2005 Enterprise Edition for Itanium-based Systems
  • Microsoft SQL Server 2005 Enterprise X64 Edition
  • Microsoft SQL Server 2005 Evaluation Edition
  • Microsoft SQL Server 2008 Developer
  • Microsoft SQL Server 2008 Enterprise
  • Microsoft SQL Server 2008 Standard
  • Microsoft SQL Server 2008 Standard Edition for Small Business
  • Microsoft SQL Server 2008 Workgroup
  • Microsoft SQL Server 2008 R2 Developer
  • Microsoft SQL Server 2008 R2 Enterprise
  • Microsoft SQL Server 2008 R2 Standard
  • Microsoft SQL Server 2008 R2 Workgroup
Keywords: 
kbqfe kbfix kbexpertiseadvanced kbsurveynew KB2455009

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com