Skip to content

Commit 63c906f

Browse files
Set-DbaDbCompression - Add SortInTempDB parameter and fix views T-SQL bug (#10248)
1 parent 0ee03fc commit 63c906f

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

public/Set-DbaDbCompression.ps1

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ function Set-DbaDbCompression {
4444
Forces compression operations to use offline rebuilds instead of the default online rebuilds when possible. Online rebuilds keep tables accessible during compression but use more resources.
4545
Use this switch when you need to minimize resource usage during compression or when experiencing issues with online operations. Offline rebuilds will make tables unavailable during the compression process.
4646
47+
.PARAMETER SortInTempDB
48+
Specifies that intermediate sort operations during index rebuilds should use the tempdb database. This can speed up index creation and reduce space usage in the user database at the expense of tempdb.
49+
Use this switch when rebuilding large indexes to avoid filling the user database's log or data files. Requires sufficient space in tempdb for the sort operations.
50+
4751
.PARAMETER InputObject
4852
Accepts compression recommendations from Test-DbaDbCompression and applies those specific recommendations instead of running a new analysis.
4953
Use this when you want to review compression recommendations first, then apply only the ones you approve of. This approach gives you more control over which objects get compressed.
@@ -150,6 +154,7 @@ function Set-DbaDbCompression {
150154
[int]$MaxRunTime = 0,
151155
[int]$PercentCompression = 0,
152156
[switch]$ForceOfflineRebuilds,
157+
[switch]$SortInTempDB,
153158
$InputObject,
154159
[switch]$EnableException
155160
)
@@ -245,6 +250,7 @@ function Set-DbaDbCompression {
245250
$underlyingObj = $server.Databases[$obj.Database].Tables[$obj.TableName, $obj.Schema].Indexes[$obj.IndexName]
246251
($underlyingObj.PhysicalPartitions | Where-Object { $_.PartitionNumber -eq $obj.Partition }).DataCompression = $obj.CompressionTypeRecommendation
247252
$underlyingObj.OnlineIndexOperation = $CanDoOnlineOperation
253+
$underlyingObj.SortInTempdb = $SortInTempDB
248254
$underlyingObj.Rebuild()
249255
} catch {
250256
Stop-Function -Message "Compression failed for $instance - $db - table $($obj.Schema).$($obj.TableName) - index $($obj.IndexName) - partition $($obj.Partition)" -Target $db -ErrorRecord $_ -Continue
@@ -310,16 +316,19 @@ function Set-DbaDbCompression {
310316
## Once this UserVoice item is fixed the workaround can be removed
311317
## https://feedback.azure.com/forums/908035-sql-server/suggestions/34080112-data-compression-smo-bug
312318
if ($CompressionType -eq "None") {
313-
$query = "ALTER INDEX [$($index.Name)] ON $($index.Parent) REBUILD PARTITION = ALL WITH"
319+
$withOptions = @("DATA_COMPRESSION = $CompressionType")
314320
if ($CanDoOnlineOperation) {
315-
$query += "(DATA_COMPRESSION = $CompressionType, ONLINE = ON)"
316-
} else {
317-
$query += "(DATA_COMPRESSION = $CompressionType)"
321+
$withOptions += "ONLINE = ON"
322+
}
323+
if ($SortInTempDB) {
324+
$withOptions += "SORT_IN_TEMPDB = ON"
318325
}
326+
$query = "ALTER INDEX [$($index.Name)] ON $($index.Parent) REBUILD PARTITION = ALL WITH ($($withOptions -join ", "))"
319327
$Server.Query($query, $db.Name)
320328
} else {
321329
$($index.PhysicalPartitions | Where-Object { $_.PartitionNumber -eq $P.PartitionNumber }).DataCompression = $CompressionType
322330
$index.OnlineIndexOperation = $CanDoOnlineOperation
331+
$index.SortInTempdb = $SortInTempDB
323332
$index.Rebuild()
324333
}
325334
} catch {
@@ -357,15 +366,19 @@ function Set-DbaDbCompression {
357366
## Once this UserVoice item is fixed the workaround can be removed
358367
## https://feedback.azure.com/forums/908035-sql-server/suggestions/34080112-data-compression-smo-bug
359368
if ($CompressionType -eq "None") {
369+
$withOptions = @("DATA_COMPRESSION = $CompressionType")
360370
if ($CanDoOnlineOperation) {
361-
$query += "(DATA_COMPRESSION = $CompressionType, ONLINE = ON)"
362-
} else {
363-
$query += "(DATA_COMPRESSION = $CompressionType)"
371+
$withOptions += "ONLINE = ON"
372+
}
373+
if ($SortInTempDB) {
374+
$withOptions += "SORT_IN_TEMPDB = ON"
364375
}
376+
$query = "ALTER INDEX [$($index.Name)] ON $($index.Parent) REBUILD PARTITION = ALL WITH ($($withOptions -join ", "))"
365377
$Server.Query($query, $db.Name)
366378
} else {
367379
$($index.PhysicalPartitions | Where-Object { $_.PartitionNumber -eq $P.PartitionNumber }).DataCompression = $CompressionType
368380
$index.OnlineIndexOperation = $CanDoOnlineOperation
381+
$index.SortInTempdb = $SortInTempDB
369382
$index.Rebuild()
370383
}
371384
} catch {

tests/Set-DbaDbCompression.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Describe $CommandName -Tag UnitTests {
2020
"MaxRunTime",
2121
"PercentCompression",
2222
"ForceOfflineRebuilds",
23+
"SortInTempDB",
2324
"InputObject",
2425
"EnableException"
2526
)

0 commit comments

Comments
 (0)