線上執行索引作業

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體

本文說明如何使用 SQL Server Management Studio 或 Transact-SQL,在 SQL Server 中在線建立、重建或卸除索引。 選項 ONLINE 可讓使用者在這些索引作業期間存取基礎表或叢集索引數據,以及任何相關聯的非叢集索引。 例如,當某個使用者正在重建叢集索引時,此使用者和其他人可以繼續更新和查詢基礎資料。

當您離線執行資料定義語言 (DDL) 作業時,例如建置或重建叢集索引,這些作業會保留基礎數據和相關聯索引的獨佔 (X) 鎖定。 這可避免在索引作業完成之前對基礎資料進行修改和查詢。

注意

即使在線執行,索引重建命令可能會在從數據表卸除大型對象數據行之後,在叢集索引上保留獨佔鎖定。

支援的平台

SQL Server 的每個版本都無法使用在線索引作業。 如需詳細資訊,請參閱 版本與 SQL Server 2022 支援的功能

線上索引作業可用於 Azure SQL Database 與 Azure SQL 受控執行個體。

限制

建議您針對全年無休的商務環境執行線上索引作業,在索引作業期間,這類環境的並行使用者活動需求相當重要。

此選項 ONLINE 可在下列 Transact-SQL 語句中使用。

如需有關在線建立、重建或卸除索引的更多限制,請參閱 在線索引作業的指導方針。

權限

必須具備資料表或檢視的 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)