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!
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:
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- The Philosophy and Fundamentals of Computer Programming
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to Data Science and SQL Server Machine Learning
- Introduction to Azure SQL Database for Beginners
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- How to Import and Export Data in SQL Server Databases
- Learn How to Install and Start Using SQL Server in 30 Mins
- A Guide on How to Start and Monetize a Successful Blog
- The Importance of Database Indexes
- Index Fragmentation in SQL Server (Reorganizing/Rebuilding Indexes)
- Where are temporary tables stored in SQL Server?
- SQL Server 2016: TempDB Enhancements
- tempdb growth
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:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
Artemakis Artemiou is a Senior SQL Server Architect, Author, a 9 Times Microsoft Data Platform MVP (2009-2018) and a Udemy Instructor. 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). Moreover, Artemakis teaches on Udemy, you can check his courses here.