commands

^

Set-DbaDbCompression

Author Jason Squires (@js_0505), [email protected]
Availability Windows, Linux, macOS

 

Want to see the source code for this command? Check out Set-DbaDbCompression on GitHub.
Want to see the Bill Of Health for this command? Check out Set-DbaDbCompression.

Synopsis

Sets tables and indexes with preferred compression setting.

Description

This function sets the appropriate compression recommendation, determined either by using the Tiger Team's query or set to the CompressionType parameter.

Remember Uptime is critical for the Tiger Team query, the longer uptime, the more accurate the analysis is.
You would probably be best if you utilized Get-DbaUptime first, before running this command.

Set-DbaDbCompression script derived from GitHub and the tigertoolbox
(https://github.com/Microsoft/tigertoolbox/tree/master/Evaluate-Compression-Gains)

Syntax

Set-DbaDbCompression
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-Database] <String[]>]
    [[-ExcludeDatabase] <String[]>]
    [[-Table] <String[]>]
    [[-CompressionType] <String>]
    [[-MaxRunTime] <Int32>]
    [[-PercentCompression] <Int32>]
    [-ForceOfflineRebuilds]
    [[-InputObject] <Object>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Set-DbaDbCompression -SqlInstance localhost -MaxRunTime 60 -PercentCompression 25

Set the compression run time to 60 minutes and will start the compression of tables/indexes that have a difference of 25% or higher between current and recommended.

Example: 2
PS C:\> Set-DbaDbCompression -SqlInstance ServerA -Database DBName -CompressionType Page -Table table1, table2

Utilizes Page compression for tables table1 and table2 in DBName on ServerA with no time limit.

Example: 3
PS C:\> Set-DbaDbCompression -SqlInstance ServerA -Database DBName -PercentCompression 25 | Out-GridView

Will compress tables/indexes within the specified database that would show any % improvement with compression and with no time limit. The results will be piped into a nicely formatted GridView.

Example: 4
PS C:\> $testCompression = Test-DbaDbCompression -SqlInstance ServerA -Database DBName
PS C:\> Set-DbaDbCompression -SqlInstance ServerA -Database DBName -InputObject $testCompression

Gets the compression suggestions from Test-DbaDbCompression into a variable, this can then be reviewed and passed into Set-DbaDbCompression.

Example: 5
PS C:\> $cred = Get-Credential sqladmin
PS C:\> Set-DbaDbCompression -SqlInstance ServerA -ExcludeDatabase Database -SqlCredential $cred -MaxRunTime 60 -PercentCompression 25

Set the compression run time to 60 minutes and will start the compression of tables/indexes for all databases except the specified excluded database. Only objects that have a difference of 25% or
higher between current and recommended will be compressed.

Example: 6
PS C:\> $servers = 'Server1','Server2'
PS C:\> foreach ($svr in $servers) {
>> Set-DbaDbCompression -SqlInstance $svr -MaxRunTime 60 -PercentCompression 25 | Export-Csv -Path C:\temp\CompressionAnalysisPAC.csv -Append
>> }

Set the compression run time to 60 minutes and will start the compression of tables/indexes across all listed servers that have a difference of 25% or higher between current and recommended. Output
of command is exported to a csv.

Required Parameters

-SqlInstance

The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.

Alias
Required True
Pipeline true (ByValue)
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 - this list is auto populated from the server. If unspecified, all databases will be processed.

Alias
Required False
Pipeline false
Default Value
-ExcludeDatabase

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

Alias
Required False
Pipeline false
Default Value
-Table

The table(s) to process. If unspecified, all tables will be processed.

Alias
Required False
Pipeline false
Default Value
-CompressionType

Control the compression type applied. Default is 'Recommended' which uses the Tiger Team query to use the most appropriate setting per object. Other option is to compress all objects to either Row or
Page.

Alias
Required False
Pipeline false
Default Value Recommended
Accepted Values Recommended,Page,Row,None
-MaxRunTime

Will continue to alter tables and indexes for the given amount of minutes.

Alias
Required False
Pipeline false
Default Value 0
-PercentCompression

Will only work on the tables/indexes that have the calculated savings at and higher for the given number provided.

Alias
Required False
Pipeline false
Default Value 0
-ForceOfflineRebuilds

By default, this function prefers online rebuilds over offline ones.
If you are on a supported version of SQL Server but still prefer to do offline rebuilds, enable this flag

Alias
Required False
Pipeline false
Default Value False
-InputObject

Takes the output of Test-DbaDbCompression as an object and applied compression based on those recommendations.

Alias
Required False
Pipeline false
Default Value
-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
-WhatIf

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

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