首页 > 解决方案 > 如果要重建许多索引,Azure SQL 重建索引脚本会失败

问题描述

我有一个脚本来在 Azure SQL 服务器中重建/重组索引,如下所示。问题是它会导致应用程序挂起,因为所有数据库会话都将从该脚本中获取。它在游标内调用重建 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;

标签: sqlsql-serverazure-sql-database

解决方案


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

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

最后COMMIT才是问题所在。正如所写的那样,该过程将仅在 IMPLICIT_TRANSACTIONS 开启的情况下运行。在循环之后使用 COMMIT,您在重建目标索引时会在目标索引上累积排他锁 (Sch-M)。当您处理大量索引时,您将阻止越来越多的其他会话。

你不应该那样做。相反,不要在每次索引重建后使用 IMPLICIT_TRANSACTIONS 或 COMMIT。例如:

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

推荐阅读