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

 


Strengthen your SQL Server Administration Skills – Enroll to our Online Course!

Check our online course on Udemy titled “Essential SQL Server Administration Tips
(special limited-time discount included in link).

Via the course, you will learn essential hands-on SQL Server Administration tips on SQL Server maintenance, security, performance, integration, error handling and more. Many live demonstrations and downloadable resources included!

Essential SQL Server Administration Tips - Online Course with Live Demonstrations and Hands-on Guides
(Lifetime Access/ Live Demos / Downloadable Resources and more!)

Learn More


 

 

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

 

Featured Online Courses:

 

Read Also:

 

Check our other related SQL Server Performance articles.

Subscribe to our newsletter and stay up to date!

Check out our latest software releases!

Check out Artemakis’s eBooks!

 

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