Skip to content

Commit

Permalink
Test-DbaDbCompression, returns ? for empty tables (do Compression) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
niphlod authored Oct 5, 2024
1 parent 4d9fc65 commit b58de53
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
56 changes: 31 additions & 25 deletions public/Test-DbaDbCompression.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ function Test-DbaDbCompression {
sys.schemas s ON t.schema_id = s.schema_id
WHERE objectproperty(t.object_id, 'IsUserTable') = 1
AND p.data_compression_desc = 'NONE'
AND p.rows > 0
$sqlSchemaWhere
$sqlTableWhere
GROUP BY
Expand Down Expand Up @@ -303,14 +302,15 @@ CREATE TABLE ##TestDbaCompression (
,[Partition] INT
,[IndexID] INT
,[IndexType] VARCHAR(25)
,[RowCounts] BIGINT
,[PercentScan] SMALLINT
,[PercentUpdate] SMALLINT
,[RowEstimatePercentOriginal] BIGINT
,[PageEstimatePercentOriginal] BIGINT
,[CompressionTypeRecommendation] VARCHAR(7)
,SizeCurrent BIGINT
,SizeRequested BIGINT
,PercentCompression NUMERIC(10, 2)
,[SizeCurrent] BIGINT
,[SizeRequested] BIGINT
,[PercentCompression] NUMERIC(10, 2)
);
CREATE TABLE ##tmpEstimateRow (
Expand Down Expand Up @@ -343,6 +343,7 @@ INSERT INTO ##TestDbaCompression (
,[Partition]
,[IndexID]
,[IndexType]
,[RowCounts]
,[PercentScan]
,[PercentUpdate]
)
Expand All @@ -353,6 +354,7 @@ INSERT INTO ##TestDbaCompression (
,p.partition_number AS [Partition]
,x.Index_ID AS [IndexID]
,x.type_desc AS [IndexType]
,p.rows AS [RowCounts]
,NULL AS [PercentScan]
,NULL AS [PercentUpdate]
FROM sys.tables t
Expand All @@ -362,7 +364,6 @@ INNER JOIN sys.partitions p ON x.object_id = p.object_id
AND x.Index_ID = p.Index_ID
WHERE OBJECTPROPERTY(t.object_id, 'IsUserTable') = 1
AND p.data_compression_desc = 'NONE'
AND p.rows > 0
$sqlSchemaWhere
$sqlTableWhere
ORDER BY [TableName] ASC;
Expand Down Expand Up @@ -516,27 +517,32 @@ AS (
,[Schema]
,IndexID
,CASE
WHEN [RowEstimatePercentOriginal] >= 100
AND [PageEstimatePercentOriginal] >= 100
THEN 'NO_GAIN'
WHEN [PercentUpdate] >= 10
THEN 'ROW'
WHEN [PercentScan] <= 1
AND [PercentUpdate] <= 1
AND [RowEstimatePercentOriginal] < [PageEstimatePercentOriginal]
THEN 'ROW'
WHEN [PercentScan] <= 1
AND [PercentUpdate] <= 1
AND [RowEstimatePercentOriginal] > [PageEstimatePercentOriginal]
THEN 'PAGE'
WHEN [PercentScan] >= 60
AND [PercentUpdate] <= 5
THEN 'PAGE'
WHEN [PercentScan] <= 35
AND [PercentUpdate] <= 5
WHEN [RowCounts] = 0
THEN '?'
ELSE 'ROW'
END
ELSE
CASE
WHEN [RowEstimatePercentOriginal] >= 100
AND [PageEstimatePercentOriginal] >= 100
THEN 'NO_GAIN'
WHEN [PercentUpdate] >= 10
THEN 'ROW'
WHEN [PercentScan] <= 1
AND [PercentUpdate] <= 1
AND [RowEstimatePercentOriginal] < [PageEstimatePercentOriginal]
THEN 'ROW'
WHEN [PercentScan] <= 1
AND [PercentUpdate] <= 1
AND [RowEstimatePercentOriginal] > [PageEstimatePercentOriginal]
THEN 'PAGE'
WHEN [PercentScan] >= 60
AND [PercentUpdate] <= 5
THEN 'PAGE'
WHEN [PercentScan] <= 35
AND [PercentUpdate] <= 5
THEN '?'
ELSE 'ROW'
END
END
FROM ##TestDbaCompression
)
UPDATE ##TestDbaCompression
Expand Down
21 changes: 15 additions & 6 deletions tests/Test-DbaDbCompression.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$null = $server.Query("Create Database [$dbname]")
$null = $server.Query("Create Schema test", $dbname)
$null = $server.Query(" select * into syscols from sys.all_columns
select * into test.sysallparams from sys.all_parameters
create clustered index CL_sysallparams on test.sysallparams (object_id)
create nonclustered index NC_syscols on syscols (precision) include (collation_name)
update test.sysallparams set is_xml_document = 1 where name = '@dbname'
$null = $server.Query(" select * into syscols from sys.all_columns;
select 1 as col into testtable where 1=0;
select * into test.sysallparams from sys.all_parameters;
create clustered index CL_sysallparams on test.sysallparams (object_id);
create nonclustered index NC_syscols on syscols (precision) include (collation_name);
update test.sysallparams set is_xml_document = 1 where name = '@dbname';
", $dbname)
}
AfterAll {
Expand All @@ -38,7 +39,7 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
}
$results.foreach{
It "Should suggest ROW, PAGE or NO_GAIN for $($PSitem.TableName) - $($PSitem.IndexType) " {
$PSitem.CompressionTypeRecommendation | Should BeIn ("ROW", "PAGE", "NO_GAIN")
$PSitem.CompressionTypeRecommendation | Should BeIn ("ROW", "PAGE", "NO_GAIN", "?")
}
It "Should have values for PercentScan and PercentUpdate $($PSitem.TableName) - $($PSitem.IndexType) " {
$PSitem.PercentUpdate | Should Not BeNullOrEmpty
Expand Down Expand Up @@ -81,4 +82,12 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
$results.Count | Should Be $resultCount
}
}
Context "Returns result for empty table (see #9469)" {
$table = 'testtable'
$results = Test-DbaDbCompression -SqlInstance $script:instance2 -Database $dbname -Table $table
It "Should get results for table:$table" {
$results | Should Not Be $null
$results[0].CompressionTypeRecommendation | Should Be '?'
}
}
}

0 comments on commit b58de53

Please sign in to comment.