Saturday, November 20, 2010

Shrink SQL Server Database

Background

Database tend to grow in size the more you use them even if the amount of data stored stays the same. This is mainly due to transaction log size constantly increasing. Also database engine tends to reserve more space for a database than it is in the given moment necessary. All this extra storage space can be retrieved which is especially useful in the event of sending the database files to some external device for backup purposes or such.

Solution

The commands to shrink the database that I've found most useful are:

        BACKUP LOG database_name WITH TRUNCATE_ONLY

        DBCC SHRINKDATABASE (database_name)

The database_name in the above commands needs to be replaced with the actual database name.

You can also try the shrink database commands available in the SQL Server Management studio UI. However they not always succeed to free as much space as it is technically possible.

No comments:

Post a Comment