Author | Claudio Silva (@ClaudioESSilva) |
Availability | Windows, Linux, macOS |
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.
This command will help you to automatically grow your transaction log file in a responsible way (preventing the generation of too many VLFs).
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:
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
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>]
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.
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.
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.
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.
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.
The target SQL Server instance or instances.
Alias | |
Required | True |
Pipeline | false |
Default Value |
Specifies the target size of the transaction log file in megabytes.
Alias | |
Required | True |
Pipeline | false |
Default Value | 0 |
If this switch is enabled, your transaction log files will be shrunk.
Alias | |
Required | True |
Pipeline | false |
Default Value | False |
Specifies the target size of the transaction log file for the shrink operation in megabytes.
Alias | |
Required | True |
Pipeline | false |
Default Value | 0 |
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 |
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 |
The database(s) to exclude. Options for this list are auto-populated from the server.
Alias | |
Required | False |
Pipeline | false |
Default Value |
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 |
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 |
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 |
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 |
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 |
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 |
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 |