SQL Server 2005
Performance Tuning
Best Practices

How to disable and rebuild an index?

 

Execute the following script in Query Editor to disable and rebuild a primary key index:

USE AdventureWorks;
GO
ALTER INDEX PK_SalesOrderDetail_SalesOrderID_SalesOrderDetailID ON Sales.SalesOrderDetail
DISABLE ;
GO

DECLARE @db_id SMALLINT;
DECLARE @object_id INT;

SET @db_id = DB_ID(N'AdventureWorks');
SET @object_id = OBJECT_ID(N'AdventureWorks.Sales.SalesOrderDetail');

IF @db_id IS NULL
BEGIN;
PRINT N'Missing database';
END;
ELSE IF @object_id IS NULL
BEGIN;
PRINT N'Missing database object';
END;
ELSE
BEGIN;
SELECT * FROM sys.dm_db_index_physical_stats(@db_id, @object_id, NULL, NULL , 'LIMITED');
END;
GO
USE AdventureWorks;
GO
ALTER INDEX PK_SalesOrderDetail_SalesOrderID_SalesOrderDetailID ON Sales.SalesOrderDetail
REBUILD;
GO
DECLARE @db_id SMALLINT;
DECLARE @object_id INT;

SET @db_id = DB_ID(N'AdventureWorks');
SET @object_id = OBJECT_ID(N'AdventureWorks.Sales.SalesOrderDetail');

SELECT * FROM sys.dm_db_index_physical_stats(@db_id, @object_id, NULL, NULL , 'LIMITED');
GO

 

The Best SQL Training in the World
 
 
SQLUSA.com Home Page