Thursday, October 4, 2012

Shrinking Database Log Files.

Often many get confused when the log grows and don't know what to do?

So here it goes.

It is recommended that you back up the log immediately before switching the recovery model of your database From FULL TO Simple.

backup log blogs_db_log with truncate_only;
You still have a point in time of recovery if your database goes down during this process.

Step1: Change recovery model of your database to SIMPLE. it will truncate the log
Step2: Shrinking the LOG file to bare minimum.
Step3: Changing the recovery model to FULL.
Step4: If you taking periodic LogsBackups from sql agnet job take a FULL Database backup of your database because your LOG backup will fail since we changed recovery model from
Full>Simple>Full.

Check your logspace on the server
dbcc sqlperf (logspace)
Check Available Free space on all drives
xp_fixeddrives

Example to Shrink your Logfiles.
Step:1
use blogs_db
go
alter database blogs_db
set recovery simple
go
 I am shriking to 2MB You may even opt for 0
(It will shrink to the initial size of your Log file which you assigned while creating your Database log file)

Step:2
dbcc shrinkfile(glogs_db_log, 2)
go

Step:3
alter database blogs_db
set recovery full
go

Optional
Step4: backup database BLogs_DB to DISK
'C:\backups\BLogs_DB.BAK'