From bac601ed3a08a881bbfa23e199347feef91b8e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mich=C3=A8le=20Tahay?= <62601152+michele-tahay-kohera@users.noreply.github.com> Date: Thu, 5 Dec 2024 09:19:33 +0100 Subject: [PATCH 1/2] Update IndexOptimize.sql Included lock timeout setting on fetching details about indexes/statistics. Even with the transaction level to readuncommitted, I had an issue on my environment that locked this query for 15 hours because of another process taking that long (an issue on its own :) ). --- IndexOptimize.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/IndexOptimize.sql b/IndexOptimize.sql index 4065799..9f833f0 100644 --- a/IndexOptimize.sql +++ b/IndexOptimize.sql @@ -1549,7 +1549,8 @@ BEGIN -- Select indexes in the current database IF (EXISTS(SELECT * FROM @ActionsPreferred) OR @UpdateStatistics IS NOT NULL) AND (SYSDATETIME() < DATEADD(SECOND,@TimeLimit,@StartTime) OR @TimeLimit IS NULL) BEGIN - SET @CurrentCommand = 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + IF @LockTimeout IS NOT NULL SET @CurrentCommand = 'SET LOCK_TIMEOUT ' + CAST(@LockTimeout * 1000 AS nvarchar) + '; ' + SET @CurrentCommand = @CurrentCommand + 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + ' SELECT SchemaID, SchemaName, ObjectID, ObjectName, ObjectType, IsMemoryOptimized, IndexID, IndexName, IndexType, AllowPageLocks, IsImageText, IsNewLOB, IsFileStream, IsColumnStore, IsComputed, IsTimestamp, OnReadOnlyFileGroup, ResumableIndexOperation, StatisticsID, StatisticsName, NoRecompute, IsIncremental, PartitionID, PartitionNumber, PartitionCount, [Order], Selected, Completed' + ' FROM (' From 30a08e3464e408f7ec6a9840f6eef400344d4e5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mich=C3=A8le=20Tahay?= <62601152+michele-tahay-kohera@users.noreply.github.com> Date: Fri, 6 Dec 2024 09:10:41 +0100 Subject: [PATCH 2/2] Update IndexOptimize.sql variable @CurrentCommand should be initialized --- IndexOptimize.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/IndexOptimize.sql b/IndexOptimize.sql index 9f833f0..ccc43e7 100644 --- a/IndexOptimize.sql +++ b/IndexOptimize.sql @@ -1549,6 +1549,7 @@ BEGIN -- Select indexes in the current database IF (EXISTS(SELECT * FROM @ActionsPreferred) OR @UpdateStatistics IS NOT NULL) AND (SYSDATETIME() < DATEADD(SECOND,@TimeLimit,@StartTime) OR @TimeLimit IS NULL) BEGIN + SET @CurrentCommand = '' IF @LockTimeout IS NOT NULL SET @CurrentCommand = 'SET LOCK_TIMEOUT ' + CAST(@LockTimeout * 1000 AS nvarchar) + '; ' SET @CurrentCommand = @CurrentCommand + 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + ' SELECT SchemaID, SchemaName, ObjectID, ObjectName, ObjectType, IsMemoryOptimized, IndexID, IndexName, IndexType, AllowPageLocks, IsImageText, IsNewLOB, IsFileStream, IsColumnStore, IsComputed, IsTimestamp, OnReadOnlyFileGroup, ResumableIndexOperation, StatisticsID, StatisticsName, NoRecompute, IsIncremental, PartitionID, PartitionNumber, PartitionCount, [Order], Selected, Completed'