How to set Azure SQL to rebuild indexes automatically?

前端 未结 5 2003
一个人的身影
一个人的身影 2020-12-28 17:55

In on premise SQL databases, it is normal to have a maintenance plan for rebuilding the indexes once in a while, when it is not being used that much.

How can I set i

5条回答
  •  甜味超标
    2020-12-28 18:49

    As @TheGamiswar suggested, add a linked server, then create a stored procedure like this:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE [LinkedServerName].[RemoteDB].[dbo].[sp_RebuildReorganizIndexes]   
    AS  
    BEGIN  
        ALTER INDEX PK_MyTable ON MyTable REBUILD WITH (STATISTICS_NORECOMPUTE = ON, ONLINE=ON);
        ALTER INDEX IX_MyTable ON MyTable REBUILD WITH (STATISTICS_NORECOMPUTE = ON, ONLINE=ON); --Nonclustered index
    
        ALTER INDEX PK_MyTable ON MyTable REORGANIZE;
        ALTER INDEX IX_MyTable ON MyTable REORGANIZE;
    END
    

    Then on your linked server use "SQL Server Agent" to create a new job and a schedule:

    For details please see https://docs.microsoft.com/en-us/sql/ssms/agent/create-a-job?view=sql-server-2017

提交回复
热议问题