Solved

Diskspace For Index Rebuild

Posted on 2013-01-09
2
361 Views
Last Modified: 2013-01-15
Hi ,

How to reduce .mdf file size after Index Rebuild (Offline)  in SQL Server 2005
Before doing index rebuild i changed recovery mode to bulk.
0
Comment
Question by:spkvijay
2 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38758789
To answer your question, I will tell you that the way to shrink a data file in SQL Server is with DBCC SHRINKFILE.

However, just because you CAN do it, doesn't mean you SHOULD do it.

It is generally accepted that running SHRINKFILE on a datafile is a bad idea.  It causes internal file fragmentation and can cause bad performance.

If the file grew to the size it currently is, it obviously needed the space and the next time you perform index rebuilds (if that was the reason the file grew), then it's only going to grow again.

A quick search produced any number of articles on why you shouldn't shrink your datafiles:

http://www.karaszi.com/sqlserver/info_dont_shrink.asp
http://dba.stackexchange.com/questions/17277/when-is-it-ok-to-shrink-a-database
http://ask.sqlservercentral.com/questions/1872/is-using-shrink-bad-for-your-database.html
http://www.brentozar.com/archive/2009/08/stop-shrinking-your-database-files-seriously-now/

(Brent Ozar's post also links to another half a dozen articles from people like Paul Randall, Kimberly Tripp and Tom LaRock saying the same thing).
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 38760421
>> the next time you perform index rebuilds (if that was the reason the file grew), then it's only going to grow again <<

Not necessarily.  And index rebuilds do indeed by default use some extra disk space in the db, although much less than in earlier SQL versions (SQL 2000 and before).

I suggest using option SORT_IN_TEMPDB = ON when doing index rebuilds.  I've found it lessens total disk space used in the main db (even more than MS's docs claim it will), and usually makes the rebuilt index more contiguous besides.  It also can help performance, esp. if you have tempdb on a separate disk/raid set.

That option does require that you have enough free space in tempdb to hold the entire index.  That's not normally an issue nowadays, as tempdb is usually large anyway; however, it could prevent an extremely large index from using that option.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now