Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shrink Database/File 2005

Posted on 2010-09-01
4
Medium Priority
?
571 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

722 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