GithubHelp home page GithubHelp logo

Comments (5)

olahallengren avatar olahallengren commented on July 1, 2024

Thank you. I have this high on the list of features to add.

from sql-server-maintenance-solution.

olahallengren avatar olahallengren commented on July 1, 2024

I have released a new version with support for online resumable index rebuilds.
https://ola.hallengren.com/versions.html

https://ola.hallengren.com/sql-server-index-and-statistics-maintenance.html#Resumable

It is setting the MAX_DURATION for the index rebuild, if you are using the @TimeLimit parameter.

EXECUTE dbo.IndexOptimize
@Databases = 'USER_DATABASES',
@Resumable = 'Y',
@TimeLimit = 3600

from sql-server-maintenance-solution.

fernandojncarvalho avatar fernandojncarvalho commented on July 1, 2024

Hi olahallengren,

thank you very much for your scripts, they are very useful.
I'm having a problem with the index rebuild operations when using the resumable option = 'Y'. It's a very cool new feature on SQL 2017 and the script uses it very well but, when its used in combination with the TimeLimit it breaks when reaches the max_duration (this is a expected behavior but when the script is inside a job it will report failure and then retry).
In my case I want the job to run for 3 hours and then resume the operations on another day. Because the job fails it will be retried and the actual execution is more than 3 hours...
Is it possible to not break the execution of the script when the index rebuild reaches the max_duration?

I hope you understand my question.
Thank you once again.

from sql-server-maintenance-solution.

philcart avatar philcart commented on July 1, 2024

@fernandojncarvalho I run the IndexOptimise in two SQL Agent jobs.

In the first job that runs weekly, I pull out all the indexes that need optimisation using the following,
EXEC [dbo].[IndexOptimize] @Databases = 'USER_DATABASES', @FragmentationLow = NULL, @FragmentationMedium = 'INDEX_REORGANIZE,INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE', @FragmentationHigh = 'INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE', @FragmentationLevel1 = 5, @FragmentationLevel2 = 30, @LogToTable = 'Y', @Execute = 'N';

This captures the Index rebuild/defrag commands in the CommandLog table. After running the IndexOptimise command, the "EndTime" column is set to null for all the records just inserted.

Then on a daily basis, within our maintenance window, the second job just uses a simple cursor to pull out each command that has an EndTime of NULL and run it if the time window hasn't elapsed.
SET NOCOUNT ON; SET QUOTED_IDENTIFIER ON; DECLARE @logID int; DECLARE @sqlCmd nvarchar(max); DECLARE @maxDuration int = 60; DECLARE @startTime datetime = GETDATE(); DECLARE @totalCmds int = (SELECT COUNT(1) FROM [dbo].[CommandLog] WHERE [EndTime] IS NULL); DECLARE @currCount int = 0; DECLARE @cmdSample nvarchar(100); IF @totalCmds > 0 BEGIN -- we have work to do DECLARE cmds CURSOR FAST_FORWARD FOR SELECT [ID],[Command] FROM [dbo].[CommandLog] WHERE [EndTime] IS NULL ORDER BY [ID] DESC OPEN cmds FETCH NEXT FROM cmds INTO @logID, @sqlCmd WHILE (@@FETCH_STATUS = 0 AND (DATEDIFF(MI,@startTime,GETDATE()) < @maxDuration)) BEGIN SET @currCount += 1; SET @cmdSample = LEFT(@sqlCmd,60)+'...'; UPDATE [dbo].[CommandLog] SET [StartTime] = GETDATE() WHERE [ID] = @logID; RAISERROR('Excuting IndexOptimize command for ID:%i (%i of %i) - %s',10,1,@logID, @currCount, @totalCmds,@cmdSample) WITH NOWAIT; EXEC sp_executeSql @command = @sqlCmd; UPDATE [dbo].[CommandLog] SET [EndTime] = GETDATE() WHERE [ID] = @logID; RAISERROR('Command complete for ID:%i (%i of %i) - %s',10,1,@logID, @currCount, @totalCmds,@cmdSample) WITH NOWAIT; FETCH NEXT FROM cmds INTO @logID, @sqlCmd END IF (@currCount < @totalCmds) BEGIN RAISERROR('IndexOptimize finishing due to elapsed time, executed %i commands out of %i',10,1,@currCount, @totalCmds) WITH NOWAIT; END ELSE BEGIN RAISERROR('All commands executed within allowed time window',10,1) WITH NOWAIT; END CLOSE cmds DEALLOCATE cmds END -- we have work to do ELSE BEGIN -- we have work to do RAISERROR('IndexOptimize has nothing to execute',10,1) WITH NOWAIT; END -- we have work to do

from sql-server-maintenance-solution.

fernandojncarvalho avatar fernandojncarvalho commented on July 1, 2024

from sql-server-maintenance-solution.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.