Solved

Shrink Database/File 2005

Posted on 2010-09-01
4
564 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

615 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