Skip to content

Preserver fill factor doing index rebuild #672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
44 changes: 44 additions & 0 deletions IndexOptimize.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ALTER PROCEDURE [dbo].[IndexOptimize]
@DatabasesInParallel nvarchar(max) = 'N',
@ExecuteAsUser nvarchar(max) = NULL,
@LogToTable nvarchar(max) = 'N',
@PreserveFillFactor nvarchar(max) = 'N',
@Execute nvarchar(max) = 'Y'

AS
Expand Down Expand Up @@ -311,6 +312,7 @@ BEGIN
SET @Parameters += ', @DatabasesInParallel = ' + ISNULL('''' + REPLACE(@DatabasesInParallel,'''','''''') + '''','NULL')
SET @Parameters += ', @ExecuteAsUser = ' + ISNULL('''' + REPLACE(@ExecuteAsUser,'''','''''') + '''','NULL')
SET @Parameters += ', @LogToTable = ' + ISNULL('''' + REPLACE(@LogToTable,'''','''''') + '''','NULL')
SET @Parameters += ', @PreserveFillFactor = ' + ISNULL('''' + REPLACE(@PreserveFillFactor,'''','''''') + '''','NULL')
SET @Parameters += ', @Execute = ' + ISNULL('''' + REPLACE(@Execute,'''','''''') + '''','NULL')

SET @StartMessage = 'Date and time: ' + CONVERT(nvarchar,@StartTime,120)
Expand Down Expand Up @@ -1112,6 +1114,14 @@ BEGIN

----------------------------------------------------------------------------------------------------

IF @PreserveFillFactor NOT IN('Y','N') OR @PreserveFillFactor IS NULL
BEGIN
INSERT INTO @Errors ([Message], Severity, [State])
SELECT 'The value for the parameter @PreserveFillFactor is not supported.', 16, 1
END

----------------------------------------------------------------------------------------------------

IF @Execute NOT IN('Y','N') OR @Execute IS NULL
BEGIN
INSERT INTO @Errors ([Message], Severity, [State])
Expand Down Expand Up @@ -2040,6 +2050,40 @@ BEGIN
SET @CurrentStatisticsSample = NULL
SET @CurrentStatisticsResample = 'Y'
END

-- Check current fill factor
IF @PreserveFillFactor = 'Y'
BEGIN
IF @CurrentIndexID IS NOT NULL
AND @CurrentOnReadOnlyFileGroup = 0
AND EXISTS(SELECT * FROM @ActionsPreferred)
AND (EXISTS(SELECT [Priority], [Action], COUNT(*) FROM @ActionsPreferred GROUP BY [Priority], [Action] HAVING COUNT(*) <> 3) OR @MinNumberOfPages > 0 OR @MaxNumberOfPages IS NOT NULL)
BEGIN
SET @CurrentCommand = ''

IF @LockTimeout IS NOT NULL SET @CurrentCommand = 'SET LOCK_TIMEOUT ' + CAST(@LockTimeout * 1000 AS nvarchar) + '; '

IF @MeasureInternalFragmentation IS NOT NULL SET @CurrentCommand += 'SELECT @ParamFillFactor = CASE WHEN i.fill_factor = 0 THEN 100 ELSE i.fill_factor END FROM sys.dm_db_index_physical_stats(DB_ID(@ParamDatabaseName), @ParamObjectID, @ParamIndexID, @ParamPartitionNumber, ''SAMPLED'') ips INNER JOIN ' + @CurrentDatabaseName + '.sys.indexes i ON (ips.object_id = i.object_id) AND ips.index_id = i.index_id WHERE ips.alloc_unit_type_desc = ''IN_ROW_DATA'' AND ips.index_level = 0'

BEGIN TRY
EXECUTE sp_executesql @stmt = @CurrentCommand, @params = N'@ParamDatabaseName nvarchar(max), @ParamObjectID int, @ParamIndexID int, @ParamPartitionNumber int, @ParamFillFactor int OUTPUT', @ParamDatabaseName = @CurrentDatabaseName, @ParamObjectID = @CurrentObjectID, @ParamIndexID = @CurrentIndexID, @ParamPartitionNumber = @CurrentPartitionNumber, @ParamFillFactor = @CurrentFillFactor OUTPUT
SET @FillFactor = @CurrentFillFactor
END TRY
BEGIN CATCH
SET @ErrorMessage = 'Msg ' + CAST(ERROR_NUMBER() AS nvarchar) + ', ' + ISNULL(ERROR_MESSAGE(),'') + CASE WHEN ERROR_NUMBER() = 1222 THEN ' The index ' + QUOTENAME(@CurrentIndexName) + ' on the object ' + QUOTENAME(@CurrentDatabaseName) + '.' + QUOTENAME(@CurrentSchemaName) + '.' + QUOTENAME(@CurrentObjectName) + ' is locked. The page_count and avg_fragmentation_in_percent could not be checked.' ELSE '' END
SET @Severity = CASE WHEN ERROR_NUMBER() IN(1205,1222) THEN @LockMessageSeverity ELSE 16 END
RAISERROR('%s',@Severity,1,@ErrorMessage) WITH NOWAIT
RAISERROR(@EmptyLine,10,1) WITH NOWAIT

IF NOT (ERROR_NUMBER() IN(1205,1222) AND @LockMessageSeverity = 10)
BEGIN
SET @ReturnCode = ERROR_NUMBER()
END

GOTO NoAction
END CATCH
END
END

-- Create index comment
IF @CurrentIndexID IS NOT NULL
Expand Down