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
- Introduction to Azure SQL Database for Beginners
- SQL Server 2019: What’s New – New and Enhanced Features
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Administration Tips
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Development Tips for SQL Developers
- Working with Python on Windows and SQL Server Databases
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to SQL Server Machine Learning Services
- 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
Read Also:
- 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
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.
DBCC SHRINKDATABASE will also shrink the logfile. It would probably be better to just use DBCC SHRINKFILE with the TRUNCATEONLY option.
I will say that I've not had much luck with this option because there's frequently the data of other tables or indexes at the logical end of the file. It may, however, be just what the Doctor ordered for the individual files of partitioned tables.
Hi Jeff,
I agree that you can also use DBCC SHRINKFILE with TRUNCATEONLY.
However, as the index rebuild operations move pages inside the data file and they will require log space for storing the temp results (unless you use TempDB for that), DBCC SHRINKDATABASE will release space from both data and log files because, in this example, it runs right after the index rebuild for each database.