Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions indexes/dba_indexDefrag_sp.sql
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ CREATE PROCEDURE dbo.dba_indexDefrag_sp
/* time to wait between defrag commands */
, @debugMode BIT = 0
/* display some useful comments to help determine if/WHERE issues occur */
, @lockTimeout INT = 180
/* Specifies the number of seconds a statement waits for a lock to be released */
AS /*********************************************************************************
Name: dba_indexDefrag_sp

Expand Down Expand Up @@ -485,7 +487,8 @@ BEGIN
, @allowPageLockSQL NVARCHAR(4000)
, @allowPageLockSQL_Param NVARCHAR(4000)
, @allowPageLocks INT
, @excludeMaxPartitionSQL NVARCHAR(4000);
, @excludeMaxPartitionSQL NVARCHAR(4000)
, @lockTimoutCommand NVARCHAR(4000);

/* Initialize our variables */
SELECT @startdatetime = GETDATE()
Expand Down Expand Up @@ -723,6 +726,11 @@ BEGIN

IF @debugMode = 1 RAISERROR(@debugMessage, 0, 42) WITH NOWAIT;

IF @lockTimeout > 0
SET @lockTimoutCommand = N'SET LOCK_TIMEOUT ' + CAST(@lockTimeout * 1000 AS NVARCHAR(4000)) + N' ';
ELSE
SET @lockTimoutCommand = N'';

/* Begin our loop for defragging */
WHILE (SELECT COUNT(*)
FROM dbo.dba_indexDefragStatus
Expand Down Expand Up @@ -857,7 +865,7 @@ BEGIN
AND @allowPageLocks = 0
BEGIN

SET @sqlCommand = N'ALTER INDEX ' + @indexName + N' ON ' + @databaseName + N'.'
SET @sqlCommand = @lockTimoutCommand + N'ALTER INDEX ' + @indexName + N' ON ' + @databaseName + N'.'
+ @schemaName + N'.' + @objectName + N' REORGANIZE';

/* If our index is partitioned, we should always REORGANIZE */
Expand Down Expand Up @@ -890,7 +898,7 @@ BEGIN
ELSE
SET @rebuildCommand = @rebuildCommand + N')';

SET @sqlCommand = N'ALTER INDEX ' + @indexName + N' ON ' + @databaseName + N'.'
SET @sqlCommand = @lockTimoutCommand + N'ALTER INDEX ' + @indexName + N' ON ' + @databaseName + N'.'
+ @schemaName + N'.' + @objectName + @rebuildCommand;

END
Expand Down