This article, discusses about, how you can rebuild all indexes online for a SQL Server database and provides useful T-SQL scripts that can help you perform this task.
The below script makes use of the undocumented SQL Server stored procedure “sp_MSforeachtable” and with the proper syntax, it rebuilds all SQL Server indexes online for all tables in a database, along with keeping the default Fill Factor for each index.
Rebuild all indexes online along with keeping up the the default fill factor:
USE [DATABASE_NAME]; GO EXEC sp_MSforeachtable @command1="print 'rebuilding indexes in table: ?'", @command2="ALTER INDEX ALL ON ? REBUILD WITH (ONLINE=ON)"; GO
Note: The above script assumes that your current edition of SQL Server supports online index rebuild. In a different case you can modify the script to perform the index rebuild offline.
By modifying the above script, you can have different variations of the rebuild statement (i.e. run the rebuild offline, set the Fill Factor, etc.).
Here are some more examples/variations of the above script.
Rebuild all indexes online along with setting up the fill factor value:
USE [SampleDB1]; GO EXEC sp_MSforeachtable @command1="print 'rebuilding indexes in table: ?'", @command2="ALTER INDEX ALL ON ? REBUILD WITH (ONLINE=ON, FILLFACTOR=[FILL_FACTOR_PERCENTAGE])"; GO
Rebuild all indexes offline along with keeping up the default fill factor value:
USE [SampleDB1]; GO EXEC sp_MSforeachtable @command1="print 'rebuilding indexes in table: ?'", @command2="ALTER INDEX ALL ON ? REBUILD WITH (ONLINE=OFF)"; GO
Rebuild all indexes offline along with setting up the fill factor value:
USE [SampleDB1]; GO EXEC sp_MSforeachtable @command1="print 'rebuilding indexes in table: ?'", @command2="ALTER INDEX ALL ON ? REBUILD WITH (ONLINE=OFF, FILLFACTOR=[FILL_FACTOR_PERCENTAGE])"; GO
I have written an article on SQLNetHub, on which you can find more info and examples. Check out the article here.
Learn more SQL Server Administration tips like this! Get the Online Course!
Check our online course on Udemy titled “Essential SQL Server Administration Tips” (special limited-time discount included in link).
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!
Recommended Online Courses:
- SQL Server 2022: What’s New – New and Enhanced Features
- Introduction to Azure Database for MySQL
- Working with Python on Windows and SQL Server Databases
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- A Guide on How to Start and Monetize a Successful Blog
- Data Management for Beginners – Main Principles
Read Also:
- What can you do using SQL Server?
- How to Get Started with SQL Server – First Steps
- How to Create Databases in SQL Server
- What are SQL Server Stored Procedures?
- Benefits of Primary Keys in Database Tables
- Welcome to {essentialDevTips.com}!
- How to Write a “Hello World” App in Visual C++
- How to Write a “Hello World” App in C#
- How to Fix: Slow WiFi Internet Connection on Windows 10 Laptop
- What’s the Best Allocation Unit Size when Formatting a USB Flash Drive?
- SyntaxError: invalid syntax when using IF in Python – How to Resolve it
- What is SQL Server Management Studio?
- Difference Between SQL and SQL Server
- Securing your SQL Server Instances
- What are T-SQL Snippets in SSMS?
- Software Review: SQLNetHub’s Snippets Generator
Reference: {essentialDevTips.com} (https://www.essentialdevtips.com/)
© essentialDevTips.com
Rate this article:
Artemakis Artemiou is a Senior SQL Server Architect, Author, a 9 Times 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 {essentialDevTips.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.