Skip to content

Commit 84515c2

Browse files
authored
Merge pull request #1 from misha-sql/IndexOptimize---columnstore-support
Update IndexOptimize.sql - support columnstore
2 parents af934ef + b980985 commit 84515c2

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

IndexOptimize.sql

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ALTER PROCEDURE [dbo].[IndexOptimize]
1818
@MinNumberOfPages int = 1000,
1919
@MaxNumberOfPages int = NULL,
2020
@SortInTempdb nvarchar(max) = 'N',
21+
@CompressAllRowGroups nvarchar(max) = 'Y',
2122
@MaxDOP int = NULL,
2223
@FillFactor int = NULL,
2324
@PadIndex nvarchar(max) = NULL,
@@ -289,6 +290,7 @@ BEGIN
289290
SET @Parameters += ', @MaxDOP = ' + ISNULL(CAST(@MaxDOP AS nvarchar),'NULL')
290291
SET @Parameters += ', @FillFactor = ' + ISNULL(CAST(@FillFactor AS nvarchar),'NULL')
291292
SET @Parameters += ', @PadIndex = ' + ISNULL('''' + REPLACE(@PadIndex,'''','''''') + '''','NULL')
293+
SET @Parameters += ', @CompressAllRowGroups = ' + ISNULL('''' + REPLACE(@CompressAllRowGroups,'''','''''') + '''','NULL')
292294
SET @Parameters += ', @LOBCompaction = ' + ISNULL('''' + REPLACE(@LOBCompaction,'''','''''') + '''','NULL')
293295
SET @Parameters += ', @UpdateStatistics = ' + ISNULL('''' + REPLACE(@UpdateStatistics,'''','''''') + '''','NULL')
294296
SET @Parameters += ', @OnlyModifiedStatistics = ' + ISNULL('''' + REPLACE(@OnlyModifiedStatistics,'''','''''') + '''','NULL')
@@ -886,6 +888,14 @@ BEGIN
886888
SELECT 'The value for the parameter @LOBCompaction is not supported.', 16, 1
887889
END
888890

891+
----------------------------------------------------------------------------------------------------
892+
893+
IF @CompressAllRowGroups NOT IN('Y','N') OR @CompressAllRowGroups IS NULL
894+
BEGIN
895+
INSERT INTO @Errors ([Message], Severity, [State])
896+
SELECT 'The value for the parameter @CompressAllRowGroups is not supported.', 16, 1
897+
END
898+
889899
----------------------------------------------------------------------------------------------------
890900

891901
IF @UpdateStatistics NOT IN('ALL','COLUMNS','INDEX')
@@ -1943,7 +1953,7 @@ BEGIN
19431953
-- Which actions are allowed?
19441954
IF @CurrentIndexID IS NOT NULL AND EXISTS(SELECT * FROM @ActionsPreferred)
19451955
BEGIN
1946-
IF @CurrentOnReadOnlyFileGroup = 0 AND @CurrentIndexType IN (1,2,3,4,5) AND (@CurrentIsMemoryOptimized = 0 OR @CurrentIsMemoryOptimized IS NULL) AND (@CurrentAllowPageLocks = 1 OR @CurrentIndexType = 5)
1956+
IF @CurrentOnReadOnlyFileGroup = 0 AND @CurrentIndexType IN (1,2,3,4,5,6) AND (@CurrentIsMemoryOptimized = 0 OR @CurrentIsMemoryOptimized IS NULL) AND (@CurrentAllowPageLocks = 1 OR @CurrentIndexType IN (5,6))
19471957
BEGIN
19481958
INSERT INTO @CurrentActionsAllowed ([Action])
19491959
VALUES ('INDEX_REORGANIZE')
@@ -1953,7 +1963,7 @@ BEGIN
19531963
INSERT INTO @CurrentActionsAllowed ([Action])
19541964
VALUES ('INDEX_REBUILD_OFFLINE')
19551965
END
1956-
IF @CurrentOnReadOnlyFileGroup = 0
1966+
IF ( @CurrentOnReadOnlyFileGroup = 0
19571967
AND (@CurrentIsMemoryOptimized = 0 OR @CurrentIsMemoryOptimized IS NULL)
19581968
AND (@CurrentIsPartition = 0 OR @Version >= 12)
19591969
AND ((@CurrentIndexType = 1 AND @CurrentIsImageText = 0 AND @CurrentIsNewLOB = 0)
@@ -1962,6 +1972,14 @@ BEGIN
19621972
OR (@CurrentIndexType = 2 AND @Version >= 11))
19631973
AND (@CurrentIsColumnStore = 0 OR @Version < 11)
19641974
AND SERVERPROPERTY('EngineEdition') IN (3,5,8)
1975+
)
1976+
-- Modified for Columnstore (for 2019+ version)
1977+
OR (@CurrentOnReadOnlyFileGroup = 0
1978+
AND (@CurrentIsMemoryOptimized = 0 OR @CurrentIsMemoryOptimized IS NULL)
1979+
AND (@CurrentIsPartition = 0 OR @Version >= 12)
1980+
AND @CurrentIndexType IN (5,6) AND @Version >= 15
1981+
AND SERVERPROPERTY('EngineEdition') IN (3,5,8)
1982+
)
19651983
BEGIN
19661984
INSERT INTO @CurrentActionsAllowed ([Action])
19671985
VALUES ('INDEX_REBUILD_ONLINE')
@@ -2133,18 +2151,30 @@ BEGIN
21332151
SELECT 'MAX_DURATION = ' + CAST(DATEDIFF(MINUTE,SYSDATETIME(),DATEADD(SECOND,@TimeLimit,@StartTime)) AS nvarchar(max))
21342152
END
21352153

2136-
IF @CurrentAction IN('INDEX_REORGANIZE') AND @LOBCompaction = 'Y'
2154+
IF @CurrentAction IN('INDEX_REORGANIZE') AND @LOBCompaction = 'Y' AND @CurrentIsColumnStore = 0
21372155
BEGIN
21382156
INSERT INTO @CurrentAlterIndexWithClauseArguments (Argument)
21392157
SELECT 'LOB_COMPACTION = ON'
21402158
END
21412159

2142-
IF @CurrentAction IN('INDEX_REORGANIZE') AND @LOBCompaction = 'N'
2160+
IF @CurrentAction IN('INDEX_REORGANIZE') AND @LOBCompaction = 'N' AND @CurrentIsColumnStore = 0
21432161
BEGIN
21442162
INSERT INTO @CurrentAlterIndexWithClauseArguments (Argument)
21452163
SELECT 'LOB_COMPACTION = OFF'
21462164
END
21472165

2166+
IF @CurrentAction IN('INDEX_REORGANIZE') AND @CompressAllRowGroups = 'Y' AND @CurrentIsColumnStore = 1
2167+
BEGIN
2168+
INSERT INTO @CurrentAlterIndexWithClauseArguments (Argument)
2169+
SELECT 'COMPRESS_ALL_ROW_GROUPS = ON'
2170+
END
2171+
2172+
IF @CurrentAction IN('INDEX_REORGANIZE') AND @CompressAllRowGroups = 'N' AND @CurrentIsColumnStore = 1
2173+
BEGIN
2174+
INSERT INTO @CurrentAlterIndexWithClauseArguments (Argument)
2175+
SELECT 'COMPRESS_ALL_ROW_GROUPS = OFF'
2176+
END
2177+
21482178
IF EXISTS (SELECT * FROM @CurrentAlterIndexWithClauseArguments)
21492179
BEGIN
21502180
SET @CurrentAlterIndexWithClause = ' WITH ('

0 commit comments

Comments
 (0)