Sql-server – Index rebuild canceled because of .mdf growth

sql-server-2012

I have the Standard Edition of SQL Server 2012 and used a script to rebuild index. I ran the query and then I saw that the .mdf file for the database started the grow a lot.

I chose to cancel/stop my query that run the rebuild of the database.

I checked in database what happened with the following queries:

SP_who2 

select cmd,* 
from sys.sysprocesses 
where blocked > 0

I saw that a process where blocked so I chose to kill that one.

The query/spid became marked with cancel/rollback in SP_who2.

It took a lot of time to rollback the query.

The table that was rebuilding was a big with over 4 million rows and a fragmentation of 99%.

My questions are:

  1. Why did the .mdf grow that fast? About 1 gb/(10/sec)
  2. When I stopped my query, can the database get inconsistent in any way?
  3. Can the data in the database have been changed because of this?

Thanks in advance!

Best Answer

Your question is bit incomplete as you have not posted the query used to rebuild the index. A lot depends on what options you selected during index rebuild. Did you use the GUI with default options? The size of the mdf file would also be affected by the Fill Factor value chosen during index rebuild.

1.Why did the .mdf grow that fast? About 1 gb/(10/sec)

Gerenal answer would be "YES", MDF file does grow during index rebuild because an index rebuild requires creating a new index first, and that would be created on the same drive where the data file resides, unless you have used SORT_IN_TEMPDB option, which would force intermediate sorts to be done in tempdb instead. Only after the new index is created will the old one be dropped so you can now see why space grew. Also, the space created by dropping the old index would not be released immediately. To release space you would need to run dbcc shrinkfile, although it is not advised.

If space is an issue on the drive where the data file resides you can use sort_in_tempdb option while rebuilding indexes. Please note that the intermediate sort results that are used to build the index are stored in tempdb. If tempdb is on a different set of disks to the user database, this may reduce the time needed to create an index. However, this increases the total amount of disk space that is used during the index build.

2.When I stopped my query, can the database get inconsistent in any way?

No, the database won't be in an inconsistent state. Rebuild is considered a single transaction by SQL Server so either it completes fully or things are returned to what they were before the index rebuild.

3.Can the data in the database have been changed because of this?

No, rest assured data cannot change.

I strongly suggest you to read the Blog Why data file increased after index rebuild Please also read Misconceptions Around Index rebuild by Paul Randal

My other question would be did you try Ola Hallengren solution for index rebuild and stats updating? It's a good one and uses an intelligent script.