Query for Rebuild all Indexes with update statistics of Database at Single Click

TS R 0 Reputation points
2023-03-08T10:57:14.8066667+00:00

Hi All,

Can you please help me schedule (task scheduler ) below script on Azure Pass databases.

DECLARE @TableName VARCHAR(255)

DECLARE @sql NVARCHAR(500)

DECLARE @fillfactor INT

SET @fillfactor = 80

DECLARE TableCursor CURSOR FOR

SELECT OBJECT_SCHEMA_NAME([object_id])+'.'+name AS TableName

FROM sys.tables

OPEN TableCursor

FETCH NEXT FROM TableCursor INTO @TableName

WHILE @@FETCH_STATUS = 0

BEGIN

SET @sql = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')'

PRINT (@sql)

EXEC (@sql)

 

FETCH NEXT FROM TableCursor INTO @TableName

END

CLOSE TableCursor

DEALLOCATE TableCursor

Azure SQL Database
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Bjoern Peters 8,921 Reputation points
    2023-03-08T12:15:59.78+00:00

    Hi TS

    Welcome to Q&A Forum, this is a great place to get support, answers and tips.

    Thank you for posting your query, I'll be more than glad to help you out.

    As I understood, you want to run that script on a regular basis to rebuild all indexes of a given database, and you want to use the Windows Task Scheduler for that...

    Then you have to store that script as a *.sql file, write a PowerShell script which connects to your database and executes that sql-script (for example with commands from the module "dbatools"), then you create a task in task scheduler to trigger/run that PowerShell script every week (eg).

    https://docs.dbatools.io/Invoke-DbaQuery.html

    https://voiceofthedba.com/2015/12/09/running-powershell-with-task-scheduler/

    Or a different approach would be using Azure Automation to run that *.sql script as explained in this blog post:

    https://geeks.ms/davidjrh/2015/10/08/rebuilding-sql-database-indexes-using-azure-automation/

    but this post is already some kind of old and there where a lot of changes in the portal, so maybe service names had been changed and the design of the portal will look different. but the blog post and the explanation should lead you into the right direction.

    I hope my answer is helpful to you,

    Your

    Bjoern Peters

    If the reply was helpful, please upvote and/or accept it as an answer, as this helps others in the community with similar questions. Thanks!

    0 comments No comments

  2. Alberto Morillo 34,676 Reputation points MVP Volunteer Moderator
    2023-03-08T12:23:15.56+00:00

    You can put that maintenance script on a stored procedure, and then follow the steps outlined on this Microsoft article to schedule its execution on Azure Automation.

    0 comments No comments

  3. RahulRandive 10,486 Reputation points Volunteer Moderator
    2023-03-08T12:27:41.6833333+00:00

    Thanks for your question.

    You can also automate Azure SQL DB index and statistics maintenance using Azure Data Factory

    Here is a blog and detailed steps for reference.

    https://techcommunity.microsoft.com/t5/azure-database-support-blog/automating-azure-sql-db-index-and-statistics-maintenance-using/ba-p/3057821

    Please let us know if this helps!

    Thank you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.