From f10de5092c61872b8c896bfa190ebd207aea5ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Herman?= <11869360+lukaszherman@users.noreply.github.com> Date: Tue, 23 Aug 2022 12:34:44 +0200 Subject: [PATCH 1/2] Update statistics based on rows count In most cases I would like to update statistics on small indexes daily and weekly do it on all big one. EXECUTE dbo.IndexOptimize @databases = 'USER_DATABASES', @FragmentationLow = NULL, @FragmentationMedium = NULL, @FragmentationHigh = NULL, @UpdateStatistics = 'ALL', @StatisticsSample = 100, @StatisticsRowsMin = 1000, @StatisticsRowsMax = 50000000, @Timelimit = 3600, @LogToTable = 'Y', @execute = 'N' --- IndexOptimize.sql | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/IndexOptimize.sql b/IndexOptimize.sql index 840e30e..0b15408 100644 --- a/IndexOptimize.sql +++ b/IndexOptimize.sql @@ -27,6 +27,8 @@ ALTER PROCEDURE [dbo].[IndexOptimize] @StatisticsModificationLevel int = NULL, @StatisticsSample int = NULL, @StatisticsResample nvarchar(max) = 'N', +@StatisticsRowsMin int = NULL, +@StatisticsRowsMax int = NULL, @PartitionLevel nvarchar(max) = 'Y', @MSShippedObjects nvarchar(max) = 'N', @Indexes nvarchar(max) = NULL, @@ -295,6 +297,8 @@ BEGIN SET @Parameters += ', @StatisticsModificationLevel = ' + ISNULL(CAST(@StatisticsModificationLevel AS nvarchar),'NULL') SET @Parameters += ', @StatisticsSample = ' + ISNULL(CAST(@StatisticsSample AS nvarchar),'NULL') SET @Parameters += ', @StatisticsResample = ' + ISNULL('''' + REPLACE(@StatisticsResample,'''','''''') + '''','NULL') + SET @Parameters += ', @StatisticsRowsMin = ' + ISNULL(CAST(@StatisticsRowsMin AS nvarchar),'NULL') + SET @Parameters += ', @StatisticsRowsMax = ' + ISNULL(CAST(@StatisticsRowsMax AS nvarchar),'NULL') SET @Parameters += ', @PartitionLevel = ' + ISNULL('''' + REPLACE(@PartitionLevel,'''','''''') + '''','NULL') SET @Parameters += ', @MSShippedObjects = ' + ISNULL('''' + REPLACE(@MSShippedObjects,'''','''''') + '''','NULL') SET @Parameters += ', @Indexes = ' + ISNULL('''' + REPLACE(@Indexes,'''','''''') + '''','NULL') @@ -942,6 +946,22 @@ BEGIN ---------------------------------------------------------------------------------------------------- + IF @StatisticsRowsMin < 0 + BEGIN + INSERT INTO @Errors ([Message], Severity, [State]) + SELECT 'The value for the parameter @StatisticsRowsMin is not supported.', 16, 1 + END + + ---------------------------------------------------------------------------------------------------- + + IF @StatisticsRowsMax < 0 + BEGIN + INSERT INTO @Errors ([Message], Severity, [State]) + SELECT 'The value for the parameter @StatisticsRowsMax is not supported.', 16, 1 + END + + ---------------------------------------------------------------------------------------------------- + IF @PartitionLevel NOT IN('Y','N') OR @PartitionLevel IS NULL BEGIN INSERT INTO @Errors ([Message], Severity, [State]) @@ -2016,6 +2036,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 (@StatisticsRowsMin IS NULL OR (NULLIF(@CurrentRowCount,0) => @StatisticsRowsMin)) + AND (@StatisticsRowsMax IS NULL OR (NULLIF(@CurrentRowCount,0) < @StatisticsRowsMax)) BEGIN SET @CurrentUpdateStatistics = 'Y' END From 9ae9bc0cd11d61b282c9faad4df0ec13476a5f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Herman?= <11869360+lukaszherman@users.noreply.github.com> Date: Fri, 26 Aug 2022 09:27:35 +0200 Subject: [PATCH 2/2] Update IndexOptimize.sql --- IndexOptimize.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IndexOptimize.sql b/IndexOptimize.sql index 0b15408..0d1b264 100644 --- a/IndexOptimize.sql +++ b/IndexOptimize.sql @@ -2036,7 +2036,7 @@ 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 (@StatisticsRowsMin IS NULL OR (NULLIF(@CurrentRowCount,0) => @StatisticsRowsMin)) + AND (@StatisticsRowsMin IS NULL OR (NULLIF(@CurrentRowCount,0) >= @StatisticsRowsMin)) AND (@StatisticsRowsMax IS NULL OR (NULLIF(@CurrentRowCount,0) < @StatisticsRowsMax)) BEGIN SET @CurrentUpdateStatistics = 'Y'