Solved

Shrink Database/File 2005

Posted on 2010-09-01
4
545 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
ID: 33582741
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
ID: 33583006
Just detach and attach the database willl do the trick.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33583500
>>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
ID: 33583888
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query 2 34
where to find DTS instalation package for sql 2014 64 bit 3 11
SQLCMD Syntax 2 15
Testing for Null in Stored Proc Return Value 5 15
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

829 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