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.
Grows transaction log files using calculated increment sizes to prevent excessive Virtual Log File (VLF) fragmentation.
This function intelligently grows transaction log files to target sizes while minimizing Virtual Log File (VLF) fragmentation. It calculates optimal increment sizes based on your SQL Server version and target log size, then grows the log in controlled chunks instead of letting autogrowth create excessive VLFs.
Too many VLFs create serious performance problems: slow transaction log backups, delayed database recovery during startup, and in extreme cases, degraded insert/update/delete performance. This command helps you proactively size your log files or fix existing VLF fragmentation issues.
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 |
Sets the final size you want the transaction log to reach, specified in megabytes.
This should be large enough to handle your typical transaction volume plus growth buffer. Common values range from 1GB (1024MB) for smaller databases to 10GB+ for high-transaction systems.
Alias | |
Required | True |
Pipeline | false |
Default Value | 0 |
Shrinks the transaction log to the ShrinkSize before expanding it to the target size.
This removes excessive VLF fragmentation by first reducing the log, then growing it with optimal increment sizes. Requires transaction log backups and cannot be used with Simple recovery model
databases.
Alias | |
Required | True |
Pipeline | false |
Default Value | False |
Sets the intermediate size in megabytes to shrink the log file to before re-expanding.
This should be small enough to remove VLF fragmentation but large enough to handle active transactions. Typical values are 10-100MB depending on transaction activity.
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 |
Specifies which databases to expand transaction log files for. Accepts wildcards for pattern matching.
If not specified, all accessible databases on the instance will be processed. Use this when you need to target specific databases instead of processing the entire instance.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Specifies databases to skip during the expansion process when processing all databases on an instance.
Use this to exclude system databases, read-only databases, or databases with specific requirements from batch log file expansion operations.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Controls the size of each growth operation in megabytes during the expansion process.
If not specified, the function calculates an optimal increment size based on your target size and SQL Server version to minimize VLF fragmentation. Only specify this if you need to override the
intelligent defaults.
Alias | |
Required | False |
Pipeline | false |
Default Value | -1 |
Targets a specific transaction log file by its file ID number when databases have multiple log files.
Use this when you need to expand secondary log files instead of the primary log file. Get the file ID from sys.database_files or SSMS properties.
Alias | |
Required | False |
Pipeline | false |
Default Value | -1 |
Sets the directory path where transaction log backups will be created during the shrink process.
Transaction log backups are required to shrink log files, so this directory must be accessible to the SQL Server service account. Defaults to the instance's default backup directory if not specified.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Skips the automatic disk space validation that normally ensures sufficient free space exists before expanding log files.
Use this when you're confident about available disk space but PowerShell remoting isn't available to check drive capacity, or when working with network storage that may not report correctly.
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 |