< Back

Expand-DbaDbLogFile

Mon Jan 13, 2020 9:50 am

NAME Expand-DbaDbLogFile



SYNOPSIS

This command will help you to automatically grow your transaction log file in a responsible way (preventing the

generation of too many VLFs).





SYNTAX

Expand-DbaDbLogFile [-SqlInstance] <DbaInstanceParameter> [[-SqlCredential] <Pscredential>] [-Database

<System.Object[]>] [[-ExcludeDatabase] <System.Object[]>] [-TargetLogSize] <Int> [[-IncrementSize] <Int>]

[[-LogFileId] <Int>] [-ExcludeDiskSpaceValidation <Switch>] [-EnableException <Switch>] [<CommonParameters>]



Expand-DbaDbLogFile [-SqlInstance] <DbaInstanceParameter> [[-SqlCredential] <Pscredential>] [-Database

<System.Object[]>] [[-ExcludeDatabase] <System.Object[]>] [-TargetLogSize] <Int> [[-IncrementSize] <Int>]

[[-LogFileId] <Int>] [-ShrinkLogFile] <Switch> [-ShrinkSize] <Int> [[-BackupDirectory] <String>]

[-ExcludeDiskSpaceValidation <Switch>] [-EnableException <Switch>] [<CommonParameters>]





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 ... r-too-few/



http://blogs.msdn.com/b/saponsqlserver/ ... database-r

ecovery.aspx



http://www.brentozar.com/blitz/high-vir ... 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/imp ... rver-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/pos ... f-4GB.aspx



and



http://connect.microsoft.com/SqlInstanc ... cific-grow

th-sizes-vlfs-also-not-created-appropriately



or



https://connect.microsoft.com/SqlInstan ... -exactly-4

gb-when-filegrowth-4gb



Understanding related problems:



http://www.sqlskills.com/blogs/kimberly ... r-too-few/



http://blogs.msdn.com/b/saponsqlserver/ ... database-r

ecovery.aspx



http://www.brentozar.com/blitz/high-vir ... vlf-count/



Known bug before SQL Server 2012



http://www.sqlskills.com/BLOGS/PAUL/pos ... f-4GB.aspx



http://connect.microsoft.com/SqlInstanc ... cific-grow

th-sizes-vlfs-also-not-created-appropriately



https://connect.microsoft.com/SqlInstan ... -exactly-4

gb-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





PARAMETERS

-BackupDirectory [<String>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Database [<System.Object[]>]

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

databases will be processed.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-EnableException [<Switch>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ExcludeDatabase [<System.Object[]>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ExcludeDiskSpaceValidation [<Switch>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-IncrementSize [<Int>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-LogFileId [<Int>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ShrinkLogFile [<Switch>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ShrinkSize [<Int>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlCredential [<Pscredential>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlInstance [<DbaInstanceParameter>]

The target SQL Server instance or instances.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-TargetLogSize [<Int>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





Tags: Storage, Backup

Author: Claudio Silva (@ClaudioESSilva)



Website: https://dbatools.io

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



Requires: ALTER DATABASE permission

Limitations: Freespace cannot be validated on the directory where the log file resides in SQL Server 2005.

This script uses Get-DbaDiskSpace dbatools command to get the TLog's drive free space



-------------------------- 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.



RELATED LINKS

https://dbatools.io/Expand-DbaDbLogFile