commands

^

Expand-DbaDbLogFile

Author Claudio Silva (@ClaudioESSilva)
Availability Windows, Linux, macOS

 

Synopsis

This command will help you to automatically grow your transaction log file in a responsible way (preventing the generation of too many VLFs).

Description

As you may already know, having a transaction log file with too many Virtual Log Files (VLFs) can hurt your database performance in many ways.

Example: Too many VLFs can cause transaction log backups to slow down and can also slow down database recovery and, in extreme cases, even impact insert/update/delete performance.

References: http://www.sqlskills.com/blogs/kimberly/transaction-log-vlfs-too-many-or-too-few/ http://blogs.msdn.com/b/saponsqlserver/archive/2012/02/22/too-many-virtual-log-files-vlfs-can-cause-slow-database-recovery.aspx http://www.brentozar.com/blitz/high-virtual-log-file-vlf-count/

In order to get rid of this fragmentation we need to grow the file taking the following into consideration:

  • How many VLFs are created when we perform a grow operation or when an auto-grow is invoked?

Note: In SQL Server 2014 this algorithm has changed (http://www.sqlskills.com/blogs/paul/important-change-vlf-creation-algorithm-sql-server-2014/)

Attention: We are growing in MB instead of GB because of known issue prior to SQL 2012: More detail here: http://www.sqlskills.com/BLOGS/PAUL/post/Bug-log-file-growth-broken-for-multiples-of-4GB.aspx and http://connect.microsoft.com/SqlInstance/feedback/details/481594/log-growth-not-working-properly-with-specific-growth-sizes-vlfs-also-not-created-appropriately or https://connect.microsoft.com/SqlInstance/feedback/details/357502/transaction-log-file-size-will-not-grow-exactly-4gb-when-filegrowth-4gb

Understanding related problems: http://www.sqlskills.com/blogs/kimberly/transaction-log-vlfs-too-many-or-too-few/ http://blogs.msdn.com/b/saponsqlserver/archive/2012/02/22/too-many-virtual-log-files-vlfs-can-cause-slow-database-recovery.aspx http://www.brentozar.com/blitz/high-virtual-log-file-vlf-count/

Known bug before SQL Server 2012 http://www.sqlskills.com/BLOGS/PAUL/post/Bug-log-file-growth-broken-for-multiples-of-4GB.aspx http://connect.microsoft.com/SqlInstance/feedback/details/481594/log-growth-not-working-properly-with-specific-growth-sizes-vlfs-also-not-created-appropriately https://connect.microsoft.com/SqlInstance/feedback/details/357502/transaction-log-file-size-will-not-grow-exactly-4gb-when-filegrowth-4gb

How it works? The transaction log will grow in chunks until it reaches the desired size. Example: If you have a log file with 8192MB and you say that the target size is 81920MB (80GB) it will grow in chunks of 8192MB until it reaches 81920MB. 8192 -> 16384 -> 24576 ... 73728 -> 81920

Syntax

Expand-DbaDbLogFile
    [-SqlInstance] <DbaInstanceParameter>
    [[-SqlCredential] <PSCredential>]
    [-Database <Object[]>]
    [[-ExcludeDatabase] <Object[]>]
    [-TargetLogSize] <Int32>
    [[-IncrementSize] <Int32>]
    [[-LogFileId] <Int32>]
    [-ExcludeDiskSpaceValidation]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

Expand-DbaDbLogFile
    [-SqlInstance] <DbaInstanceParameter>
    [[-SqlCredential] <PSCredential>]
    [-Database <Object[]>]
    [[-ExcludeDatabase] <Object[]>]
    [-TargetLogSize] <Int32>
    [[-IncrementSize] <Int32>]
    [[-LogFileId] <Int32>]
    [-ShrinkLogFile]
    [-ShrinkSize] <Int32>
    [[-BackupDirectory] <String>]
    [-ExcludeDiskSpaceValidation]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Expand-DbaDbLogFile -SqlInstance sqlcluster -Database db1 -TargetLogSize 50000

Grows the transaction log for database db1 on sqlcluster to 50000 MB and calculates the increment size.

Example: 2
PS C:\> Expand-DbaDbLogFile -SqlInstance sqlcluster -Database db1, db2 -TargetLogSize 10000 -IncrementSize 200

Grows the transaction logs for databases db1 and db2 on sqlcluster to 1000MB and sets the growth increment to 200MB.

Example: 3
PS C:\> Expand-DbaDbLogFile -SqlInstance sqlcluster -Database db1 -TargetLogSize 10000 -LogFileId 9

Grows the transaction log file with FileId 9 of the db1 database on sqlcluster instance to 10000MB.

Example: 4
PS C:\> Expand-DbaDbLogFile -SqlInstance sqlcluster -Database (Get-Content D:\DBs.txt) -TargetLogSize 50000

Grows the transaction log of the databases specified in the file 'D:\DBs.txt' on sqlcluster instance to 50000MB.

Example: 5
PS C:\> Expand-DbaDbLogFile -SqlInstance SqlInstance -Database db1,db2 -TargetLogSize 100 -IncrementSize 10 -ShrinkLogFile -ShrinkSize 10 -BackupDirectory R:\MSSQL\Backup

Grows the transaction logs for databases db1 and db2 on SQL server SQLInstance to 100MB, sets the incremental growth to 10MB, shrinks the transaction log to 10MB and uses the directory
R:\MSSQL\Backup for the required backups.

Required Parameters

-SqlInstance

The target SQL Server instance or instances.

Alias
Required True
Pipeline false
Default Value
-TargetLogSize

Specifies the target size of the transaction log file in megabytes.

Alias
Required True
Pipeline false
Default Value 0
-ShrinkLogFile

If this switch is enabled, your transaction log files will be shrunk.

Alias
Required True
Pipeline false
Default Value False
-ShrinkSize

Specifies the target size of the transaction log file for the shrink operation in megabytes.

Alias
Required True
Pipeline false
Default Value 0

Optional Parameters

-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported. For MFA support, please use Connect-DbaInstance.

Alias
Required False
Pipeline false
Default Value
-Database

The database(s) to process. Options for this list are auto-populated from the server. If unspecified, all databases will be processed.

Alias
Required False
Pipeline false
Default Value
-ExcludeDatabase

The database(s) to exclude. Options for this list are auto-populated from the server.

Alias
Required False
Pipeline false
Default Value
-IncrementSize

Specifies the amount the transaction log should grow in megabytes. If this value differs from the suggested value based on your TargetLogSize, you will be prompted to confirm your choice. This value will be calculated if not specified.

Alias
Required False
Pipeline false
Default Value -1
-LogFileId

Specifies the file number(s) of additional transaction log files to grow. If this value is not specified, only the first transaction log file will be processed.

Alias
Required False
Pipeline false
Default Value -1
-BackupDirectory

Specifies the location of your backups. Backups must be performed to shrink the transaction log. If this value is not specified, the SQL Server instance's default backup directory will be used.

Alias
Required False
Pipeline false
Default Value
-ExcludeDiskSpaceValidation

If this switch is enabled, the validation for enough disk space using Get-DbaDiskSpace command will be skipped. This can be useful when you know that you have enough space to grow your TLog but you don't have PowerShell Remoting enabled to validate it.

Alias
Required False
Pipeline false
Default Value False
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

Alias
Required False
Pipeline false
Default Value False
-WhatIf

If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.

Alias cf
Required False
Pipeline false
Default Value

 

Want to see the source code for this command? Check out Expand-DbaDbLogFile on GitHub.
Want to see the Bill Of Health for this command? Check out Expand-DbaDbLogFile.