Skip to content

Commit ad1088f

Browse files
authored
Merge pull request #5 from misha-sql/master
Add support for columnstore indexes
2 parents 11309e4 + 84515c2 commit ad1088f

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,
@@ -297,6 +298,7 @@ BEGIN
297298
SET @Parameters += ', @MaxDOP = ' + ISNULL(CAST(@MaxDOP AS nvarchar),'NULL')
298299
SET @Parameters += ', @FillFactor = ' + ISNULL(CAST(@FillFactor AS nvarchar),'NULL')
299300
SET @Parameters += ', @PadIndex = ' + ISNULL('''' + REPLACE(@PadIndex,'''','''''') + '''','NULL')
301+
SET @Parameters += ', @CompressAllRowGroups = ' + ISNULL('''' + REPLACE(@CompressAllRowGroups,'''','''''') + '''','NULL')
300302
SET @Parameters += ', @LOBCompaction = ' + ISNULL('''' + REPLACE(@LOBCompaction,'''','''''') + '''','NULL')
301303
SET @Parameters += ', @UpdateStatistics = ' + ISNULL('''' + REPLACE(@UpdateStatistics,'''','''''') + '''','NULL')
302304
SET @Parameters += ', @OnlyModifiedStatistics = ' + ISNULL('''' + REPLACE(@OnlyModifiedStatistics,'''','''''') + '''','NULL')
@@ -894,6 +896,14 @@ BEGIN
894896
SELECT 'The value for the parameter @LOBCompaction is not supported.', 16, 1
895897
END
896898

899+
----------------------------------------------------------------------------------------------------
900+
901+
IF @CompressAllRowGroups NOT IN('Y','N') OR @CompressAllRowGroups IS NULL
902+
BEGIN
903+
INSERT INTO @Errors ([Message], Severity, [State])
904+
SELECT 'The value for the parameter @CompressAllRowGroups is not supported.', 16, 1
905+
END
906+
897907
----------------------------------------------------------------------------------------------------
898908

899909
IF @UpdateStatistics NOT IN('ALL','COLUMNS','INDEX')
@@ -1972,7 +1982,7 @@ BEGIN
19721982
-- Which actions are allowed?
19731983
IF @CurrentIndexID IS NOT NULL AND EXISTS(SELECT * FROM @ActionsPreferred)
19741984
BEGIN
1975-
IF @CurrentOnReadOnlyFileGroup = 0 AND @CurrentIndexType IN (1,2,3,4,5) AND (@CurrentIsMemoryOptimized = 0 OR @CurrentIsMemoryOptimized IS NULL) AND (@CurrentAllowPageLocks = 1 OR @CurrentIndexType = 5)
1985+
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))
19761986
BEGIN
19771987
INSERT INTO @CurrentActionsAllowed ([Action])
19781988
VALUES ('INDEX_REORGANIZE')
@@ -1982,7 +1992,7 @@ BEGIN
19821992
INSERT INTO @CurrentActionsAllowed ([Action])
19831993
VALUES ('INDEX_REBUILD_OFFLINE')
19841994
END
1985-
IF @CurrentOnReadOnlyFileGroup = 0
1995+
IF ( @CurrentOnReadOnlyFileGroup = 0
19861996
AND (@CurrentIsMemoryOptimized = 0 OR @CurrentIsMemoryOptimized IS NULL)
19871997
AND (@CurrentIsPartition = 0 OR @Version >= 12)
19881998
AND ((@CurrentIndexType = 1 AND @CurrentIsImageText = 0 AND @CurrentIsNewLOB = 0)
@@ -1991,6 +2001,14 @@ BEGIN
19912001
OR (@CurrentIndexType = 2 AND @Version >= 11))
19922002
AND (@CurrentIsColumnStore = 0 OR @Version < 11)
19932003
AND SERVERPROPERTY('EngineEdition') IN (3,5,8)
2004+
)
2005+
-- Modified for Columnstore (for 2019+ version)
2006+
OR (@CurrentOnReadOnlyFileGroup = 0
2007+
AND (@CurrentIsMemoryOptimized = 0 OR @CurrentIsMemoryOptimized IS NULL)
2008+
AND (@CurrentIsPartition = 0 OR @Version >= 12)
2009+
AND @CurrentIndexType IN (5,6) AND @Version >= 15
2010+
AND SERVERPROPERTY('EngineEdition') IN (3,5,8)
2011+
)
19942012
BEGIN
19952013
INSERT INTO @CurrentActionsAllowed ([Action])
19962014
VALUES ('INDEX_REBUILD_ONLINE')
@@ -2162,18 +2180,30 @@ BEGIN
21622180
SELECT 'MAX_DURATION = ' + CAST(DATEDIFF(MINUTE,SYSDATETIME(),DATEADD(SECOND,@TimeLimit,@StartTime)) AS nvarchar(max))
21632181
END
21642182

2165-
IF @CurrentAction IN('INDEX_REORGANIZE') AND @LOBCompaction = 'Y'
2183+
IF @CurrentAction IN('INDEX_REORGANIZE') AND @LOBCompaction = 'Y' AND @CurrentIsColumnStore = 0
21662184
BEGIN
21672185
INSERT INTO @CurrentAlterIndexWithClauseArguments (Argument)
21682186
SELECT 'LOB_COMPACTION = ON'
21692187
END
21702188

2171-
IF @CurrentAction IN('INDEX_REORGANIZE') AND @LOBCompaction = 'N'
2189+
IF @CurrentAction IN('INDEX_REORGANIZE') AND @LOBCompaction = 'N' AND @CurrentIsColumnStore = 0
21722190
BEGIN
21732191
INSERT INTO @CurrentAlterIndexWithClauseArguments (Argument)
21742192
SELECT 'LOB_COMPACTION = OFF'
21752193
END
21762194

2195+
IF @CurrentAction IN('INDEX_REORGANIZE') AND @CompressAllRowGroups = 'Y' AND @CurrentIsColumnStore = 1
2196+
BEGIN
2197+
INSERT INTO @CurrentAlterIndexWithClauseArguments (Argument)
2198+
SELECT 'COMPRESS_ALL_ROW_GROUPS = ON'
2199+
END
2200+
2201+
IF @CurrentAction IN('INDEX_REORGANIZE') AND @CompressAllRowGroups = 'N' AND @CurrentIsColumnStore = 1
2202+
BEGIN
2203+
INSERT INTO @CurrentAlterIndexWithClauseArguments (Argument)
2204+
SELECT 'COMPRESS_ALL_ROW_GROUPS = OFF'
2205+
END
2206+
21772207
IF EXISTS (SELECT * FROM @CurrentAlterIndexWithClauseArguments)
21782208
BEGIN
21792209
SET @CurrentAlterIndexWithClause = ' WITH ('

0 commit comments

Comments
 (0)