tags:

views:

378

answers:

4

Hi I have a very large database (40 gig) and have run the procedure

sp_space_used 

and found that there is 10 gig of unallocated space. Obviously this is a lot and the .mdf file is taking up most of the disk. I have looked into running

DBCC SHRINKDATABASE (db, TRUNCATEONLY);

Do I also need to shrink the transaction log or will shrinkdatabase take care of this? What are the negative implications of running this procedure? Can I run this while the db is in use? Please help! I've tried running the shrinkdatabase command but still have lots of unallocated space...

database_size   unallocated space
49575.06 MB   8393.49 MB

reserved       data        index_size   unused
42170328 KB 22704672 KB 19099160 KB  366496 KB

NOTE: the db has a simple recovery model so i'm guessing i don't need to backup the log at all...

What is the difference between running

DBCC SHRINKFILE (datafile, TRUNCATEONLY)

to

DBCC SHRINKDATABSE (db, TRUNCATEONLY)?
A: 

You will need to backup the transaction log FIRST, then shrink it, otherwise the transaction log wont free up the space.

Creating the backups will still guarantee you can rollback to previous version of the database.

Use can use DBCC SHRINKFILE 2005 after you backup the transaction log

kevchadders
so are you saying run DBCC shrinkfile (logfile) first before running dbcc shrinkdatabase?
jeff
normally i would use Management Studio. Right Click on database > Tasks > Backup > Choose Transaction Log in backup type etc
kevchadders
and what are the implications of running this in a live environment where the db is being hit?
jeff
i've run dbcc shrinkdatabasebut unallocated space is still 8408 MB... how can i free this up?
jeff
Why would this person need to backup and then shrink the transaction log if the unallocated data is in the .mdf file?
John Sansom
aplogoies, my suggestion wqas purly around freeing up the transaction log. I misread the question.
kevchadders
A: 

First before making any structural changes to your database take a FULL Database Backup.

If, as you say, the majority of the unallocated space resides within your SQL Server data file (.mdf) then you should look to use DBCC SHRINKFILE, rather than DBCC SHRINKDATABASE.

So for example:

USE UserDB;
GO
DBCC SHRINKFILE (LogicalDataFileName, target_sizeInMB );
GO

Additionally consider using the TRUNCATEONLY option to free available space from the end of the data file only. This is less resource intensive option but may not free as much space. Keep in mind target_size is ignored if specified with TRUNCATEONLY.

You can run this maintenance on a production server however you may experience increased blocking and so should look to schedule the maintenance during a period of low activity if downtime is not an option to you. Users can however still query the database.

In light of your edit:

DBCC SHRINKDATABASE will endeavour to shrink all files within your database, that is both data and log files.

DBCC SHIRNKFILE on the other hand provides a finer level of control in that it applies to a specific file.

John Sansom
i've edited orignal post, dbcc shrinkfile doesn't seem to be doing anything, does SQL need diskspace to actually run the command properly? I think there is a few 100mb but that is it...
jeff
Then perhaps there is no free space at the end of the file in which case you will need to run the statement without the TRUNCATEONLY option.
John Sansom
ok, well i still can't seem to free up that 8.5 gig... I think it maybe because a reindex job failed and it has allocated that space for autogrowth... not sure though.
jeff
Did you use DBCC SHRINKFILE?
John Sansom
A: 

Hi Jeff,

The sp_spaceused command includes the log file free space as well. Even though your database log model is simple, SQL uses your log file to track transactions and the log file grows and it does not shrink until you run a backup. I know that you mentioned that the mdf file is taking up most of the disk, but have you check your ldf file size? Have you checked your clustered indexes fragmentation as well?

If you find that the log file is big too, try the following script to see if your database shrinks. It worked for me with simple model logs.

use [db_name]
backup log [db_name] with truncate_only
dbcc shrinkfile ([db_log_filename], 1)

This statement would not run an actual backup, it will just truncate the log and allow the shrinkfile remove the free space that the log file is using. If you are planning to run it in Production, I would recommend you to do it in an off peak time. Even though it does not take long. This only affects the log file size.

Hope this helps and that is clear why I would recommend you to check the log file as well.

Jose Chama
i have already run those commands, the log file is only 7mb...
jeff
You mentioned that the reindex job failed. Do you know the reason why the job failed? Did it run out of space?
Jose Chama
A: 

i ended up using DBCC SHRINKDATABASE (db, %); specifying a target percentage which seemed to solve my problem. Is there anyway of not specifying anything and just getting it to free up the maximum amount of space, or do I always have to guess how much space I want it to free up? Thanks everyone for your help.

jeff