SQL Server Index Rebuild Scripts

SQL Server Index Rebuild Scripts

SQL Server Index Rebuild Scripts

This article provides different SQL Server index rebuild scripts.

Script 1: Rebuild all indexes in a SQL Server database with specifying the Fill Factor:

--Rebuild all indexes in a database
--Note: You need to specify the fill factor
USE [DatabaseName]
GO
EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 90)";
GO

 

Script 2: Rebuild specific index online

ALTER INDEX [IndexName] ON [TableName]
REBUILD WITH (ONLINE=ON); 
GO

 

Script 3: Rebuild specific index offline (with default fill factor)

ALTER INDEX [IndexName] ON [TableName]
REBUILD WITH (ONLINE=OFF); 
GO

 

Subscribe to SQLNetHub Newsletter

 

Script 4: Rebuild all indexes in a table online (with default fill factor)

ALTER INDEX ALL ON [TableName]
REBUILD WITH (ONLINE=ON);
GO

 

Script 5: Rebuild all indexes in a table offline (with default fill factor)

ALTER INDEX ALL ON [TableName]
REBUILD WITH (ONLINE=OFF);
GO

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub.com

Artemakis Artemiou
Artemakis Artemiou is a Senior SQL Server and Software Architect, Author, and a former Microsoft Data Platform MVP (2009-2018). He has over 15 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub and TechHowTos.com. Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Artemakis's official website can be found at aartemiou.com.