Solved

Shrink Database/File 2005

Posted on 2010-09-01
4
510 Views
Last Modified: 2012-06-27
Using SMSS I have started Shrink Database (selected database in question, right click, chose task --> shrink --> database) The problem is that it never finishes. The green activate wheel has been spinning for days as if it never activated. I have heard not to just stop a shrink, so should I do a restart with SQL Services. Also am I better off doing a shirnk file on the .mdf?
Thanks
0
Comment
Question by:SeTech
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Your best approach is never to use the GUI and especially for something like this.  First KILL the connection.  Do it as follows:
exec sp_who

Identify yourself and then use:
KILL x     -- Where x is the connection shrinking the database.

Once you have that resolved in order to shrink the data file I would do something like this:
DBCC SHRINKFILE('YourLogicalDataFileNameGoesHere',  10000)   -- Where 10000 = 10GB

You can stop it at any time.

If you are in Full Recovery Model, make sure you have plenty of space for your Transaction Log as it will use a lot.
0
 
LVL 9

Expert Comment

by:shalabhsharma
Comment Utility
Just detach and attach the database willl do the trick.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>Just detach and attach the database willl do the trick.<<
You may want to double check that.
0
 
LVL 1

Accepted Solution

by:
FrancoCarusi earned 500 total points
Comment Utility
Shrinking databases can be a bit tricky. The Gui is not the best approach.
You need to know why you are shrinking in the first place and which file you are shrinking (data or log) . Any database will grow as transactions are processed and data gets inputted into the database. Generally the log file grows out of control and the transactions do not get committed to the data file. this generally happens when the database is in full recovery mode and there are no transaction log backups. The rule here is simple. If you require transaction log backups then the db must be in full recovery mode and you MUST do transaction log backups regularly, if no transaction log backups are required then use simple recovery mode.
Shrinking the files regularly causes the physical files to become fragmented on the database so space allocation is a critical part of the database design.
Having said all that here are times that one would need to shrink a file in an emergency.
When shrinking the log file we need to move all the data to the beginning of the file and then shrink the file. SQL will only shrink the empty space to the last bit of data so if there are big holes then they will remain there unless we move that data.
I normally run a "backup log <dbname> with truncateonly" this will commit all the data to the data file that should be made persistent).
Then run "dbcc shrinkfile (<logicalfilename>, notruncate)" this will move all the remaining data to the beginning of the file filling all the empty spaces.
Then run "dbcc shrinkfile(<logicalfilename>, truncateonly)" this will release all data at the end of the file and release some space back to the OS.
I then run "dbcc shrinkfile(<logicalfilename>,<size = about 25% of the datafile>) just to trim  it all.

Again know why you are shrinking a file and don't make this a standard.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 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

14 Experts available now in Live!

Get 1:1 Help Now