SQL Server Rebuild Index Scripts

Featured image for article on SQLEBooks.com with title "SQL Server Index Rebuild Script"
Share on Facebook3Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Reddit1Pin on Pinterest0

This article provides different SQL Server rebuild index 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

 

Administering SQL Server by SQLEBooks.com

 

Learn hundreds of SQL Server administration tips.

Get the eBook “Administering SQL Server” today!

 

 

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: SQLEBooks.com (https://www.sqlebooks.com)

© 2017 SQLEBooks.com


Recommended eBooks on SQL Server:

Developing with SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Developing SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
About artemakis 7 Articles
Artemakis Artemiou is a Senior SQL Server Architect, Author, Software Developer and a Microsoft Data Platform MVP. He has over 15 years of experience in the IT industry in various roles. Among other, via his initiative SQLEBooks.com, Artemakis authors and publishes eBooks on different topics 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). Additionally he is the founder of the SQLArtBits initiative that aims to provide the technical community with simple, yet powerful and high-quality SQL Server tools. Currently, the highlights of these tools are DBA Security Advisor and In-Memory OLTP Simulator. Artemakis's official website can be found at aartemiou.com. Artemakis's blogs can be found at: SQLNetHub.com and TechHowTos.com.