There are times where you need to massively rebuild indexes on some really large databases, after indicated by the relevant analysis of course.
However, rebuilding indexes, requires also the adequate amount of free disk space that will be used during the rebuild operation (mainly for the sorting process).
Usually the required space is the size of the indexes to be rebuilt plus some more space (more information on Index Disk Space can be found here).
An Example of a Heavy Index Rebuild Operation
For example, when you have a clustered index and the table size is 50 GB you will need between 50-55 GB of free disk space in order for the rebuild process to run properly.
Imagine a scenario where you need to rebuild 10 clustered indexes and each table to be 50 GB. If you just schedule the job without having in mind disk space you might find your machine running out of disk space very soon! This can have undesirable consequences to the O.S. and any other running processes.
To this end, before deploying an index rebuild operation you first need to make sure that you have an adequate amount of free disk space.
But which disk drive should you monitor? The TempDB drive? The user database drive?
When you design an index rebuild operation, you typically have the option to sort the rebuild results in the “TempDB” system database. If you use this option then you will need to make sure that there is enough disk space on the disk onto which the TempDB database is located.
If you do not choose to sort the rebuild results in the “TempDB” database, then you will need to make sure that there is enough disk space on the disk onto which the user database is located. Of course, you should always have disk space available (always allow for some GB to be available) for TempDB as it is used in most of the SQL Server operations. However, in this case you need to focus on the drive where the database is located on.
The Use of DBCC SHRINKDATABASE
DBCC SHRINKDATABASE (DBName, TRUNCATEONLY);
REBUILD INDEX 1 GO DBCC SHRINKDATABASE (DBName, TRUNCATEONLY); GO REBUILD INDEX 2 GO DBCC SHRINKDATABASE (DBName, TRUNCATEONLY); GO REBUILD INDEX 3 GO DBCC SHRINKDATABASE (DBName, TRUNCATEONLY); GO REBUILD INDEX N GO DBCC SHRINKDATABASE (DBName, TRUNCATEONLY); 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!
Other 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
- What are SQL Server Always On Availability Groups?
- How to Patch a SQL Server Failover Cluster
- How to Add a Database to a SQL Server Availability Group Using T-SQL
- Top 10 SQL Server DBA Daily Tasks List
- The “Public” Database Role in SQL Server
- Encrypting a SQL Server Database Backup
- Learn Azure Data Lake Analytics by Example
- Azure Cosmos DB: Learn by Example
- How to Create an Azure SQL Server Virtual Machine
- How to Backup a SQL Server Database from On-Premises to Azure Storage
- … all our SQL Server Administration Articles
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.