Author | Sander Stad (@sqlstad), sqlstad.nl |
Availability | Windows, Linux, macOS |
Want to see the source code for this command? Check out Invoke-DbaBalanceDataFiles on GitHub.
Want to see the Bill Of Health for this command? Check out Invoke-DbaBalanceDataFiles.
Re-balance data between data files
When you have a large database with a single data file and add another file, SQL Server will only use the new file until it's about the same size.
You may want to balance the data between all the data files.
The function will check the server version and edition to see if the it allows for online index rebuilds.
If the server does support it, it will try to rebuild the index online.
If the server doesn't support it, it will rebuild the index offline. Be carefull though, this can cause downtime
The tables must have a clustered index to be able to balance out the data.
The function does NOT yet support heaps.
The function will also check if the file groups are subject to balance out.
A file group would have at least have 2 data files and should be writable.
If a table is within such a file group it will be subject for processing. If not the table will be skipped.
Note: this command does not perform a disk space check for non-Windows machines so make sure you have enough space on the disk.
Invoke-DbaBalanceDataFiles
[-SqlCredential <PSCredential>]
[-Database <Object[]>]
[-Table <Object[]>]
[-RebuildOffline]
[-EnableException]
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Invoke-DbaBalanceDataFiles -SqlInstance <DbaInstanceParameter[]>
[-SqlCredential <PSCredential>]
[-Database <Object[]>]
[-Table <Object[]>]
[-RebuildOffline]
[-EnableException]
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
PS C:\> Invoke-DbaBalanceDataFiles -SqlInstance sql1 -Database db1
This command will distribute the data in database db1 on instance sql1
PS C:\> Invoke-DbaBalanceDataFiles -SqlInstance sql1 -Database db1 | Select-Object -ExpandProperty DataFilesEnd
This command will distribute the data in database db1 on instance sql1
PS C:\> Invoke-DbaBalanceDataFiles -SqlInstance sql1 -Database db1 -Table table1,table2,table5
This command will distribute the data for only the tables table1,table2 and table5
PS C:\> Invoke-DbaBalanceDataFiles -SqlInstance sql1 -Database db1 -RebuildOffline
This command will consider the fact that there might be a SQL Server edition that does not support online rebuilds of indexes.
By supplying this parameter you give permission to do the rebuilds offline if the edition does not support it.
The target SQL Server instance or instances.
Alias | |
Required | True |
Pipeline | false |
Default Value |
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.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The tables(s) of the database to process. If unspecified, all tables will be processed.
Alias | Tables |
Required | False |
Pipeline | false |
Default Value |
Will set all the indexes to rebuild offline.
This option is also needed when the server version is below 2005.
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 |
This will disable the check for enough disk space for the action to be successful.
Use this with caution!!
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Shows what would happen if the command were to run. No actions are actually performed.
Alias | wi |
Required | False |
Pipeline | false |
Default Value |
Prompts you for confirmation before executing any changing operations within the command.
Alias | cf |
Required | False |
Pipeline | false |
Default Value |