如果要重建多个索引,Azure SQL重建索引脚本将失败

如果要重建多个索引,Azure SQL重建索引脚本将失败,sql,sql-server,azure-sql-database,Sql,Sql Server,Azure Sql Database,我有一个脚本可以在Azure SQL server中重建/重新组织索引,如下所示。问题是,它会导致应用程序挂起,因为所有数据库会话都将从此脚本中获取。它在游标内调用rebuild sql语句,所以我希望它会一个接一个地重建索引。但是,似乎所有的索引重建都将并行运行,否则数据库就没有理由开始挂起,并且在此过程中无法建立连接。如果要重建的索引很少,那么就可以了。当大约有60个索引需要重建时,问题就开始了。有没有办法强制一个接一个地构建索引?顺序地,而不是并行地 CREATE PROCEDURE [d

我有一个脚本可以在Azure SQL server中重建/重新组织索引,如下所示。问题是,它会导致应用程序挂起,因为所有数据库会话都将从此脚本中获取。它在游标内调用rebuild sql语句,所以我希望它会一个接一个地重建索引。但是,似乎所有的索引重建都将并行运行,否则数据库就没有理由开始挂起,并且在此过程中无法建立连接。如果要重建的索引很少,那么就可以了。当大约有60个索引需要重建时,问题就开始了。有没有办法强制一个接一个地构建索引?顺序地,而不是并行地

CREATE PROCEDURE [dbo].[RebuildIndexes]
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @objectid int;
    DECLARE @indexid int;
    DECLARE @partitioncount bigint;
    DECLARE @schemaname nvarchar(130);
    DECLARE @objectname nvarchar(130);
    DECLARE @indexname nvarchar(250);
    DECLARE @partitionnum bigint;
    DECLARE @partitions bigint;
    DECLARE @frag float;
    DECLARE @command nvarchar(4000);
    -- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function
    -- and convert object and index IDs to names.
    SELECT
        object_id AS objectid,
        index_id AS indexid,
        partition_number AS partitionnum,
        avg_fragmentation_in_percent AS frag
    INTO #indexes_to_build
    FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED')
    WHERE avg_fragmentation_in_percent > 10.0 AND index_id > 0 and page_count > 200
    ORDER BY avg_fragmentation_in_percent DESC;

    -- Declare the cursor for the list of partitions to be processed.
    DECLARE partitions CURSOR FOR SELECT * FROM #indexes_to_build;

    -- Open the cursor.
    OPEN partitions;

    -- Loop through the partitions.
    WHILE (1=1)
        BEGIN;
            FETCH NEXT
               FROM partitions
               INTO @objectid, @indexid, @partitionnum, @frag;
            IF @@FETCH_STATUS < 0 BREAK;
            SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(s.name)
            FROM sys.objects AS o
            JOIN sys.schemas as s ON s.schema_id = o.schema_id
            WHERE o.object_id = @objectid;
            SELECT @indexname = QUOTENAME(name)
            FROM sys.indexes
            WHERE  object_id = @objectid AND index_id = @indexid;
            SELECT @partitioncount = count (*)
            FROM sys.partitions
            WHERE object_id = @objectid AND index_id = @indexid;

    -- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding.
            IF @frag < 30.0
                SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REORGANIZE';
            IF @frag >= 30.0
                SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REBUILD WITH (ONLINE = ON)';
            IF @partitioncount > 1
                SET @command = @command + N' PARTITION=' + CAST(@partitionnum AS nvarchar(10));
            EXEC (@command);
            PRINT N'Executed: ' + @command;
        END;

    -- Close and deallocate the cursor.
    CLOSE partitions;
    DEALLOCATE partitions;

    -- Drop the temporary table.
    DROP TABLE #indexes_to_build;
    COMMIT;
END;

我的建议是使用Ola Hallemgren的存储过程名称索引优化,您可以从中下载。每个人都使用自己的脚本执行索引维护任务

您可以运行存储过程,如下所示:

EXECUTE dbo.IndexOptimize
@Databases = 'mydbnamehere',
@FragmentationLow = NULL,
@FragmentationMedium = 'INDEX_REORGANIZE,INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE',
@FragmentationHigh = 'INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE',
@FragmentationLevel1 = 5,
@FragmentationLevel2 = 30,
@UpdateStatistics = 'ALL',
@OnlyModifiedStatistics = 'Y'
有关更多信息,请访问网页

有没有办法强制一个接一个地构建索引

TSQL总是按顺序执行。您的alterindex命令也不例外

问题在于最后的提交。如前所述,该过程将仅在上运行隐式的_事务。通过循环后提交,在重建目标索引时,会在目标索引上累积独占锁Sch-M。在处理大量索引时,将阻止越来越多的其他会话

你不应该那样做。相反,不要在每次索引重建后使用隐式的_事务或提交。例如:

    IF @partitioncount > 1
        SET @command = @command + N' PARTITION=' + CAST(@partitionnum AS nvarchar(10));
    EXEC (@command);
    PRINT N'Executed: ' + @command;
    COMMIT;

谢谢你的帮助,这是一个非常有用的答案。因此,我将在每次索引重建后调用commit,并在循环后删除commit。再次感谢您的回复。我使用Azure Automation定期运行它。您只需要下载并在数据库上创建存储过程。