commands

^

Invoke-DbaBalanceDataFiles

Author Sander Stad (@sqlstad), sqlstad.nl
Availability Windows, Linux, macOS

 

Synopsis

Re-balance data between data files

Description

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

Syntax

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

 

Examples

 

Example: 1
PS C:\> Invoke-DbaBalanceDataFiles -SqlInstance sql1 -Database db1

This command will distribute the data in database db1 on instance sql1

Example: 2
PS C:\> Invoke-DbaBalanceDataFiles -SqlInstance sql1 -Database db1 | Select-Object -ExpandProperty DataFilesEnd

This command will distribute the data in database db1 on instance sql1

Example: 3
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

Example: 4
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.

Required Parameters

-SqlInstance

The target SQL Server instance or instances.

Alias
Required True
Pipeline false
Default Value

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.

Alias
Required False
Pipeline false
Default Value
-Table

The tables(s) of the database to process. If unspecified, all tables will be processed.

Alias Tables
Required False
Pipeline false
Default Value
-RebuildOffline

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

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

Shows what would happen if the command were to run

Alias wi
Required False
Pipeline false
Default Value
-Confirm

Prompts for confirmation of every step. For example: The server does not support online rebuilds of indexes. Do you want to rebuild the indexes offline? [Y] Yes [N] No [?] Help (default is "Y"):

Alias cf
Required False
Pipeline false
Default Value

 

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.