diff --git a/IndexOptimize.sql b/IndexOptimize.sql index 3410ce7f..e35751f0 100644 --- a/IndexOptimize.sql +++ b/IndexOptimize.sql @@ -1,4 +1,4 @@ -SET ANSI_NULLS ON +SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO @@ -1930,6 +1930,18 @@ BEGIN END CATCH END + -- Find index size + IF @CurrentIndexID IS NOT NULL + AND @CurrentOnReadOnlyFileGroup = 0 + BEGIN + SET @CurrentCommand = '' + + IF @LockTimeout IS NOT NULL SET @CurrentCommand = 'SET LOCK_TIMEOUT ' + CAST(@LockTimeout * 1000 AS nvarchar) + '; ' + + SET @CurrentCommand += 'SELECT @ParamPageCount = SUM(reserved_page_count) FROM sys.dm_db_partition_stats ps WHERE ps.[object_id]=@ParamObjectID AND ps.[index_id]=@ParamIndexID AND ps.partition_number=@ParamPartitionNumber;' + EXECUTE @CurrentDatabase_sp_executesql @stmt = @CurrentCommand, @params = N'@ParamObjectID int, @ParamIndexID int, @ParamPartitionNumber int, @ParamPageCount bigint OUTPUT', @ParamObjectID = @CurrentObjectID, @ParamIndexID = @CurrentIndexID, @ParamPartitionNumber = @CurrentPartitionNumber, @ParamPageCount = @CurrentPageCount OUTPUT + END + -- Select fragmentation group IF @CurrentIndexID IS NOT NULL AND @CurrentOnReadOnlyFileGroup = 0 AND EXISTS(SELECT * FROM @ActionsPreferred) BEGIN @@ -2016,6 +2028,8 @@ BEGIN AND ((@UpdateStatistics = 'ALL' AND (@CurrentIndexType IN (1,2,3,4,7) OR @CurrentIndexID IS NULL)) OR (@UpdateStatistics = 'INDEX' AND @CurrentIndexID IS NOT NULL AND @CurrentIndexType IN (1,2,3,4,7)) OR (@UpdateStatistics = 'COLUMNS' AND @CurrentIndexID IS NULL)) AND ((@OnlyModifiedStatistics = 'N' AND @StatisticsModificationLevel IS NULL) OR (@OnlyModifiedStatistics = 'Y' AND @CurrentModificationCounter > 0) OR ((@CurrentModificationCounter * 1. / NULLIF(@CurrentRowCount,0)) * 100 >= @StatisticsModificationLevel) OR (@StatisticsModificationLevel IS NOT NULL AND @CurrentModificationCounter > 0 AND (@CurrentModificationCounter >= SQRT(@CurrentRowCount * 1000))) OR (@CurrentIsMemoryOptimized = 1 AND NOT (@Version >= 13 OR SERVERPROPERTY('EngineEdition') IN (5,8)))) AND ((@CurrentIsPartition = 0 AND (@CurrentAction NOT IN('INDEX_REBUILD_ONLINE','INDEX_REBUILD_OFFLINE') OR @CurrentAction IS NULL)) OR (@CurrentIsPartition = 1 AND (@CurrentPartitionNumber = @CurrentPartitionCount OR (@PartitionLevelStatistics = 1 AND @CurrentIsIncremental = 1)))) + AND (@CurrentPageCount >= @MinNumberOfPages OR @MinNumberOfPages = 0) + AND (@CurrentPageCount <= @MaxNumberOfPages OR @MaxNumberOfPages IS NULL) BEGIN SET @CurrentUpdateStatistics = 'Y' END diff --git a/MaintenanceSolution.sql b/MaintenanceSolution.sql index 48be1f80..1948e254 100644 --- a/MaintenanceSolution.sql +++ b/MaintenanceSolution.sql @@ -1,4 +1,4 @@ -/* +/* SQL Server Maintenance Solution - SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014, SQL Server 2016, SQL Server 2017, SQL Server 2019, and SQL Server 2022 @@ -8281,6 +8281,18 @@ BEGIN END CATCH END + -- Find index size + IF @CurrentIndexID IS NOT NULL + AND @CurrentOnReadOnlyFileGroup = 0 + BEGIN + SET @CurrentCommand = '' + + IF @LockTimeout IS NOT NULL SET @CurrentCommand = 'SET LOCK_TIMEOUT ' + CAST(@LockTimeout * 1000 AS nvarchar) + '; ' + + SET @CurrentCommand += 'SELECT @ParamPageCount = SUM(reserved_page_count) FROM sys.dm_db_partition_stats ps WHERE ps.[object_id]=@ParamObjectID AND ps.[index_id]=@ParamIndexID AND ps.partition_number=@ParamPartitionNumber;' + EXECUTE @CurrentDatabase_sp_executesql @stmt = @CurrentCommand, @params = N'@ParamObjectID int, @ParamIndexID int, @ParamPartitionNumber int, @ParamPageCount bigint OUTPUT', @ParamObjectID = @CurrentObjectID, @ParamIndexID = @CurrentIndexID, @ParamPartitionNumber = @CurrentPartitionNumber, @ParamPageCount = @CurrentPageCount OUTPUT + END + -- Select fragmentation group IF @CurrentIndexID IS NOT NULL AND @CurrentOnReadOnlyFileGroup = 0 AND EXISTS(SELECT * FROM @ActionsPreferred) BEGIN @@ -8367,6 +8379,8 @@ BEGIN AND ((@UpdateStatistics = 'ALL' AND (@CurrentIndexType IN (1,2,3,4,7) OR @CurrentIndexID IS NULL)) OR (@UpdateStatistics = 'INDEX' AND @CurrentIndexID IS NOT NULL AND @CurrentIndexType IN (1,2,3,4,7)) OR (@UpdateStatistics = 'COLUMNS' AND @CurrentIndexID IS NULL)) AND ((@OnlyModifiedStatistics = 'N' AND @StatisticsModificationLevel IS NULL) OR (@OnlyModifiedStatistics = 'Y' AND @CurrentModificationCounter > 0) OR ((@CurrentModificationCounter * 1. / NULLIF(@CurrentRowCount,0)) * 100 >= @StatisticsModificationLevel) OR (@StatisticsModificationLevel IS NOT NULL AND @CurrentModificationCounter > 0 AND (@CurrentModificationCounter >= SQRT(@CurrentRowCount * 1000))) OR (@CurrentIsMemoryOptimized = 1 AND NOT (@Version >= 13 OR SERVERPROPERTY('EngineEdition') IN (5,8)))) AND ((@CurrentIsPartition = 0 AND (@CurrentAction NOT IN('INDEX_REBUILD_ONLINE','INDEX_REBUILD_OFFLINE') OR @CurrentAction IS NULL)) OR (@CurrentIsPartition = 1 AND (@CurrentPartitionNumber = @CurrentPartitionCount OR (@PartitionLevelStatistics = 1 AND @CurrentIsIncremental = 1)))) + AND (@CurrentPageCount >= @MinNumberOfPages OR @MinNumberOfPages = 0) + AND (@CurrentPageCount <= @MaxNumberOfPages OR @MaxNumberOfPages IS NULL) BEGIN SET @CurrentUpdateStatistics = 'Y' END