From 51a4f5d3fc67e445cbe72fc24780586534dce8e3 Mon Sep 17 00:00:00 2001 From: RShakes <50854578+AWildRShakes@users.noreply.github.com> Date: Tue, 31 Mar 2026 13:08:48 +0200 Subject: [PATCH] Added @IndexTypes parameter to dbo.IndexOptimize This change allows you to pass an @IndexTypes parameter to Ola Hallengren's dbo.IndexOptimize stored procedure to specifically target, ALL, CLUSTERED, or NONCLUSTERED indexes. --- IndexOptimize.sql | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/IndexOptimize.sql b/IndexOptimize.sql index 09e0e61..319bd86 100644 --- a/IndexOptimize.sql +++ b/IndexOptimize.sql @@ -31,6 +31,7 @@ ALTER PROCEDURE [dbo].[IndexOptimize] @PartitionLevel nvarchar(max) = 'Y', @MSShippedObjects nvarchar(max) = 'N', @Indexes nvarchar(max) = NULL, +@IndexTypes nvarchar(max) = 'ALL', -- ALL, CLUSTERED, NONCLUSTERED @TimeLimit int = NULL, @Delay int = NULL, @WaitAtLowPriorityMaxDuration int = NULL, @@ -54,7 +55,7 @@ BEGIN --// Source: https://ola.hallengren.com //-- --// License: https://ola.hallengren.com/license.html //-- --// GitHub: https://github.com/olahallengren/sql-server-maintenance-solution //-- - --// Version: 2025-12-20 18:52:13 //-- + --// Version: 2026-03-31 05:54:40 //-- ---------------------------------------------------------------------------------------------------- SET NOCOUNT ON @@ -304,6 +305,7 @@ BEGIN SET @Parameters += ', @PartitionLevel = ' + ISNULL('''' + REPLACE(@PartitionLevel,'''','''''') + '''','NULL') SET @Parameters += ', @MSShippedObjects = ' + ISNULL('''' + REPLACE(@MSShippedObjects,'''','''''') + '''','NULL') SET @Parameters += ', @Indexes = ' + ISNULL('''' + REPLACE(@Indexes,'''','''''') + '''','NULL') + SET @Parameters += ', @IndexTypes = ' + ISNULL('''' + REPLACE(@IndexTypes,'''','''''') + '''','NULL') SET @Parameters += ', @TimeLimit = ' + ISNULL(CAST(@TimeLimit AS nvarchar),'NULL') SET @Parameters += ', @Delay = ' + ISNULL(CAST(@Delay AS nvarchar),'NULL') SET @Parameters += ', @WaitAtLowPriorityMaxDuration = ' + ISNULL(CAST(@WaitAtLowPriorityMaxDuration AS nvarchar),'NULL') @@ -978,6 +980,14 @@ BEGIN ---------------------------------------------------------------------------------------------------- + IF @IndexTypes NOT IN ('ALL', 'CLUSTERED', 'NONCLUSTERED') OR @IndexTypes IS NULL + BEGIN + INSERT INTO @Errors ([Message], Severity, [State]) + SELECT 'The value for the parameter @IndexTypes is not supported.', 16, 1 + END + + ---------------------------------------------------------------------------------------------------- + IF @TimeLimit < 0 BEGIN INSERT INTO @Errors ([Message], Severity, [State]) @@ -1608,7 +1618,11 @@ BEGIN SET @CurrentCommand = @CurrentCommand + ' WHERE objects.[type] IN(''U'',''V'')' + CASE WHEN @MSShippedObjects = 'N' THEN ' AND objects.is_ms_shipped = 0' ELSE '' END - + ' AND indexes.[type] IN(1,2,3,4,5,6,7)' + + ' AND indexes.[type] IN(' + CASE + WHEN @IndexTypes = 'CLUSTERED' THEN '1,5' + WHEN @IndexTypes = 'NONCLUSTERED' THEN '2,3,4,6,7' + ELSE '1,2,3,4,5,6,7' + END + ')' + ' AND indexes.is_disabled = 0 AND indexes.is_hypothetical = 0' END @@ -2501,4 +2515,3 @@ BEGIN END GO -