Halaman

Kamis, 11 April 2013

Shrinking the SQL Server log

To backup a database, we could instruct SQL Server to truncate of the transaction log, using T-SQL command: BACKUP LOG db_name WITH TRUNCATE_ONLY. Log truncation remove transactions from the log file and the space remains allocated to the file. Sometimes as in my case below, you need to shrink the log file to reclaim the space.

In my case, I receive a backup file from SQL Server 2008, probably a full backup. After restoring the file and create a new database on my laptop, the used space is 160MB for the data and 52GB for the log file. My local disk free space 18 GB left, I need to shrinking the log file.

1. Check the database size property:
Select name, filename, convert(decimal(12,2), round(a.size/128.000,2)) as SizeMB,
convert(decimal(12,2), round(fileproperty(a.name,'SpaceUsed')/128.000,2)) as SpcUsedMB, 
convert(decimal(12,2), round((a.size-fileproperty(a.name,'SpaceUsed'))/128.000,2)) as FreeSpcMB
from dbo.sysfiles a

name        filename            SizeMB    SpcUsedMB  FreeSpcMB 
-----------------------------------------------------------------
Student     D:\DATA\STUDENT.mdf 155.69    149.50     6.19
Student_log D:\DATA\STUDENT.ldf 50861.94  362.09     50499.84

The FreeSpcMB shows 51GB and I try to reclaim the space of the log file to OS.

2. Change the database recover mode to simple
   ALTER DATABASE Student SET RECOVERY SIMPLE;
   GO

3. Do the Shrink the log file to 1 MB
   DBCC SHRINKFILE (Student_log, 1);
   GO

3. Reset the database recovery model.
   ALTER DATABASE Student SET RECOVERY FULL;
   GO
   SELECT name, recovery_model_desc FROM sys.databases;
   GO


Now, the OS has 70GB free space.

2 komentar:

  1. Link exchange is nothing else except it iѕ just ρlacing the other person's web site link on your page at suitable place and other person will also do same for you.

    My webpage :: RPMPoker Bonus

    BalasHapus
  2. Ι ԁon't leave a leave a response, but I looked at a few of the comments here "Shrinking the SQL Server log". I actually do have 2 questions for you if it's allright.
    Ӏs іt sіmрly me or dο a few of the rеsponsеs apрeаr lіke they are writtеn bу braіn ԁead viѕіtors?

    :-P And, if you are ωгіting at additional social sites,
    I would like to follow you. Would уοu pоѕt a list
    of all of аll your social networking ѕіtes lіκе your twittеr feed, Facebook pаge
    or linkeԁin рrofile?

    Feel free to νisit my webpage :: Americas Cardroom Poker Offer

    BalasHapus