Skip to content

Commit 4b1795e

Browse files
authored
skip compressed tables for sparse column check
1 parent 6c7461b commit 4b1795e

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

sp_sizeoptimiser.sql

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,10 +1137,10 @@ BEGIN
11371137
N' USE ?;
11381138
BEGIN
11391139
DECLARE @schemaName SYSNAME
1140-
,@tableName SYSNAME
1141-
,@statName SYSNAME
1142-
,@colName SYSNAME
1143-
,@threshold_null_perc SMALLINT;
1140+
,@tableName SYSNAME
1141+
,@statName SYSNAME
1142+
,@colName SYSNAME
1143+
,@threshold_null_perc SMALLINT;
11441144
11451145
DECLARE @DBCCSQL NVARCHAR(MAX) = N'''';
11461146
DECLARE @DBCCStatSQL NVARCHAR(MAX) = N'''';
@@ -1158,34 +1158,31 @@ BEGIN
11581158
INNER JOIN [sys].[tables] AS [t] on t.object_id = s.object_id
11591159
INNER JOIN [sys].[schemas] AS [sch] on sch.schema_id = t.schema_id
11601160
INNER JOIN [sys].[all_columns] AS [ac] on ac.column_id = sc.column_id
1161-
AND [ac].[object_id] = [t].[object_id]
1162-
AND [ac].[object_id] = [sc].[object_id]
1161+
AND [ac].[object_id] = [t].[object_id]
1162+
AND [ac].[object_id] = [sc].[object_id]
11631163
INNER JOIN [sys].[types] AS [typ] ON [typ].[user_type_id] = [ac].[user_type_id]
1164-
LEFT JOIN [sys].[indexes] AS [i] ON i.object_id = t.object_id
1165-
AND i.name = s.name
1166-
LEFT JOIN [sys].[index_columns] AS [ic] ON [ic].[object_id] = [i].[object_id]
1167-
AND [ic].[column_id] = [ac].[column_id]
1168-
AND ic.index_id = i.index_id '
1164+
INNER JOIN [sys].[indexes] AS [i] ON [i].[object_id] = [t].[object_id] '
11691165
+ /* Special considerations for variable length data types */ +
11701166
N'INNER JOIN [#SparseTypes] AS [st] ON [st].[user_type_id] = [typ].[user_type_id]
1171-
AND (typ.name NOT IN (''DECIMAL'', ''NUMERIC'', ''DATETIME2'', ''TIME'', ''DATETIMEOFFSET''))
1172-
OR (typ.name IN (''DECIMAL'', ''NUMERIC'') AND st.precision = ac.precision AND st.precision = 1)
1173-
OR (typ.name IN (''DECIMAL'', ''NUMERIC'') AND ac.precision > 1 AND st.precision = 38)
1174-
OR (typ.name IN (''DATETIME2'', ''TIME'', ''DATETIMEOFFSET'') AND st.scale = ac.scale AND st.scale = 0)
1175-
OR (typ.name IN (''DATETIME2'', ''TIME'', ''DATETIMEOFFSET'') AND ac.scale > 0 AND st.scale = 7)
1167+
AND (typ.name NOT IN (''DECIMAL'', ''NUMERIC'', ''DATETIME2'', ''TIME'', ''DATETIMEOFFSET''))
1168+
OR (typ.name IN (''DECIMAL'', ''NUMERIC'') AND st.precision = ac.precision AND st.precision = 1)
1169+
OR (typ.name IN (''DECIMAL'', ''NUMERIC'') AND ac.precision > 1 AND st.precision = 38)
1170+
OR (typ.name IN (''DATETIME2'', ''TIME'', ''DATETIMEOFFSET'') AND st.scale = ac.scale AND st.scale = 0)
1171+
OR (typ.name IN (''DATETIME2'', ''TIME'', ''DATETIMEOFFSET'') AND ac.scale > 0 AND st.scale = 7)
11761172
WHERE [sc].[stats_column_id] = 1
11771173
AND [s].[has_filter] = 0
11781174
AND [s].[no_recompute] = 0
1179-
AND [ac].[is_nullable] = 1 ';
1180-
1175+
AND [ac].[is_nullable] = 1
1176+
AND NOT EXISTS (SELECT 1 -- Compressed tables not compatible with sparse cols
1177+
FROM [sys].[partitions] AS [p]
1178+
WHERE [p].[object_id] = [i].[object_id]
1179+
AND [p].[data_compression] > 0) ';
11811180
IF @HasTempStat = 1
11821181
BEGIN;
1183-
SET @CheckSQL = @CheckSQL + N'AND [s].[is_temporary] = 0 ';
1182+
SET @CheckSQL = @CheckSQL + N'AND [s].[is_temporary] = 0; ';
11841183
END;
11851184

1186-
SET @CheckSQL = @CheckSQL + N'AND ([ic].[index_column_id] = 1 OR [ic].[index_column_id] IS NULL)
1187-
AND ([i].[type_desc] =''NONCLUSTERED'' OR [i].[type_desc] IS NULL);
1188-
1185+
SET @CheckSQL = @CheckSQL + N'
11891186
OPEN [DBCC_Cursor];
11901187
11911188
FETCH NEXT FROM [DBCC_Cursor]

0 commit comments

Comments
 (0)