联机执行索引操作

适用于:SQL ServerAzure SQL 数据库Azure SQL 托管实例

本文章说明如何使用 SQL Server Management Studio 或 Transact-SQL 在 SQL Server 中在线创建、重新生成或删除索引。 ONLINE 选项允许并发用户在执行这些索引操作期间访问基础表或聚集索引数据和任何关联非聚集索引。 例如,一个用户正在重新生成聚集索引时,该用户和其他用户可以继续更新和查询基础数据。

当脱机执行数据定义语言 (DDL) 操作(例如,生成或重新生成聚集索引)时,这些操作对基础数据和关联索引持有排他 (X) 锁。 这样可以防止在索引操作未完成时对基础数据进行修改和查询。

注意

从表中删除大型对象列后,索引重新生成命令可能会在聚集索引上保留排他锁,即使联机执行也不例外。

支持的平台

在 SQL Server 的各版本中均不提供联机索引操作。 有关详细信息,请参阅 SQL Server 2022 的版本及其支持的功能

联机索引操作可用于 Azure SQL 数据库和 Azure SQL 托管实例。

限制

建议对于全天候运行的业务环境执行联机索引操作,在这些环境中,在执行索引操作期间必须有并发用户活动。

以下 Transact-SQL 语句中可以使用 ONLINE 选项。

有关联机创建、重新生成或删除索引的更多限制和局限性,请参阅联机索引操作指南

权限

要求具有对表或视图的 ALTER 权限。

使用 SQL Server Management Studio

  1. 在“对象资源管理器”中,选择加号以便展开包含您要联机重新生成索引的表的数据库。

  2. 展开 “表” 文件夹。

  3. 选择加号以展开您要联机重新生成索引的表。

  4. 展开 “索引” 文件夹。

  5. 右键单击要联机重新生成的索引,然后选择“属性”

  6. “选择页”下,选择 “选项”

  7. 选择 “允许联机 DML 处理”,然后从列表中选择 True

  8. 选择“确定”

  9. 右键单击要联机重新生成的索引,然后选择“重新生成”

  10. 在“重新生成索引”对话框中,确认正确的索引位于“要重新生成的索引”网格中,然后选择“确定”

使用 Transact-SQL

下面的示例在 AdventureWorks 数据库中重新生成现有联机索引。

ALTER INDEX AK_Employee_NationalIDNumber
    ON HumanResources.Employee
    REBUILD WITH (ONLINE = ON);

以下示例使用 NewGroup 子句联机删除一个聚集索引并将生成表(堆)移动到文件组 MOVE TO 。 在移动之前和之后,将查询 sys.indexessys.tablessys.filegroups 目录视图,以验证索引和表在文件组中的位置。

-- Create a clustered index on the PRIMARY filegroup if the index does not exist.
IF NOT EXISTS (SELECT name FROM sys.indexes WHERE name = 
            N'AK_BillOfMaterials_ProductAssemblyID_ComponentID_StartDate')
    CREATE UNIQUE CLUSTERED INDEX
        AK_BillOfMaterials_ProductAssemblyID_ComponentID_StartDate 
    ON Production.BillOfMaterials (ProductAssemblyID, ComponentID, 
        StartDate)
    ON 'PRIMARY';
GO

-- Verify filegroup location of the clustered index.
SELECT t.name AS [Table Name], i.name AS [Index Name], i.type_desc,
    i.data_space_id, f.name AS [Filegroup Name]
FROM sys.indexes AS i
    JOIN sys.filegroups AS f ON i.data_space_id = f.data_space_id
    JOIN sys.tables as t ON i.object_id = t.object_id
        AND i.object_id = OBJECT_ID(N'Production.BillOfMaterials','U')
GO

-- Create filegroup NewGroup if it does not exist.
IF NOT EXISTS (SELECT name FROM sys.filegroups
                WHERE name = N'NewGroup')
    BEGIN
    ALTER DATABASE AdventureWorks2022
        ADD FILEGROUP NewGroup;
    ALTER DATABASE AdventureWorks2022
        ADD FILE (NAME = File1,
            FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\File1.ndf')
        TO FILEGROUP NewGroup;
    END
GO

-- Verify new filegroup
SELECT * from sys.filegroups;
GO

-- Drop the clustered index and move the BillOfMaterials table to
-- the Newgroup filegroup.
-- Set ONLINE = OFF to execute this example on editions other than Enterprise Edition.
DROP INDEX AK_BillOfMaterials_ProductAssemblyID_ComponentID_StartDate 
    ON Production.BillOfMaterials 
    WITH (ONLINE = ON, MOVE TO NewGroup);
GO

-- Verify filegroup location of the moved table.
SELECT t.name AS [Table Name], i.name AS [Index Name], i.type_desc,
    i.data_space_id, f.name AS [Filegroup Name]
FROM sys.indexes AS i
    JOIN sys.filegroups AS f ON i.data_space_id = f.data_space_id
    JOIN sys.tables as t ON i.object_id = t.object_id
        AND i.object_id = OBJECT_ID(N'Production.BillOfMaterials','U');

有关详细信息,请参阅 ALTER INDEX (Transact-SQL)