Monday, June 18, 2018

Stop Transaction Logs from Growing on a SQL Server

Problem: In a SQL server, you may have seen most of the time space becomes zero due to growth of the log.

Solution: In SQL Server, database transaction log keeps growing until you take a backup of the database. Once taken backup, log files will be truncate. It is not logical to delete transaction log files until you take a backup of the database ( mostly for production class database servers, for test environment transaction logs may not important as production).

Why ?

In a situation, database corruption or loss, you can recreate the database with restoring last database backup and restoring transaction logs on top of that with no loss.

(This is why database files and log files should be in different disks)

But in a test environment ( Other than production ) it is preferred to be automatically truncate the log files which will help to reduce the space used by the database. SQL server let us do this by changing the recovery model.

Set the recovery model = Simple

How to do 

Open SQL server Management studio --> select database --> Right click and select Properties --> Go to Option --> Change "recovery model" to Simple

you can do it in script using below

USE [master]
GO
ALTER DATABASE [myDB] SET RECOVERY SIMPLE WITH NO_WAIT
GO

You can check model with below script

SELECT name, recovery_model_desc 
   FROM sys.databases 
      WHERE name = 'myDB' ;


If Log files has grown, better to shrink after changing the recovery type.


No comments:

Post a Comment