Solved

Shrink Database/File 2005

Posted on 2010-09-01
4
550 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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

685 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