Today's Offer - SQL Server Certification Training - Enroll at Flat 20% Off.

- SQL Server Blogs -

How to Clear SQL Server Transaction Log File with DBCC Shrinkfile

Introduction to SQL Server Transaction Log

In SQL, transaction log file is used to save records that are produced during logging process in an SQL Server database. The transaction log is basically an integral part of SQL Server database. It is not only integral but also a most important component of the SQL Server database especially when it is about disaster recovery. As in SQL, as soon as the database is modified the log record is written to the transaction log. Here all the changes are recorded in the transaction file in a sequential order. In this article, we are going to discuss SQL Server transaction log and its features.

Transaction Log Storage

SQL Server transaction log stores every transaction except a few ones like SELECT INTO and BULK IMPORT operations. Internally transaction log is divided into some smaller parts that are called Virtual Log Files or VLFs. There are more than one VLF and when one file is filled with log details than logs are written to the next available file. These transaction log files are represented in circular form so when one file reaches the end than it starts again from the beginning. Here, truncation process is used to mark the end of file or any unused part of the file so that it can be reused for storing information.

In the following cases, any log record is marked as unused in Transaction log:

  • The transaction of which it was a part has been committed
  • The database pages for which they have been created have written to the disk
  • It is not needed for full, differential, or log backup
  • It is not required for any feature that needs a log.

We can recover the database at any time when a disaster occurs. The user must regularly clean the transaction log to keep pace in the log file.

Why should we clear the Transaction log?

If any database change occurs than SQL Server transaction log grows. Regular maintenance of transaction log can prevent it from becoming full. Log truncation technique is used to keep the log empty or prevent it from being filled up. This process deletes the inactive log files from the logical transaction log and freed up space from logical transaction log that can be reused by the physical transaction log. If the log will not be truncated then it can fill all disk space allocated to the physical transaction log.

Read: Introduction to SQL Injection and Attacks

SQL Server Curriculum

Truncation process for the three recovery models of SQL is different and explained as below:

  • Simple Recovery Model: In this model Transaction log backup is not supported and truncation process executes automatically as a result space is freed up automatically
  • Bulk-logged Recovery Model: In this automatic truncation is not available and so regular backups are needed to mark unused space and hereby space is created for overwriting purpose. The size of the log file can be here reduced for bulk operations
  • Full Recovery Model: In this model again, the truncation process is not automatic and the chances for growing the log files are higher.

One can know the available and used transaction log space with the help of a single line command as shown in the following figure: SQL Server Transaction Log File with DBCC Shrinkfile

Now after getting the information, you can take action accordingly to clear the log file.

How to clear the Transaction Log File of SQL Server?

As it has been clear so far that log truncation process creates more free space in the transaction log file so is also called log clearing. A transaction log file is usually o small chunks that are also called Virtual Log Files or VLF. These VLFs are marked if are available for reuse and if not, then also they will be marked accordingly by using active and inactive tags respectively.

To clear the transaction log, one has to search these VLF files and make them free or available. To reduce the size of physical log files, Log Shrinking process is used as Log Truncation does not reduce the physical log storage size.

SQL Server Quiz

Read: How To Start Your Career As MSBI Developer?

Two steps are involved in the process of clearing SQL Server transaction log:

  1. TRUNCATE_ONLY option is used to perform a log backup
  2. DBCC SHRINKFILE function shrinks the file to the required size

Here BACKUP LOG TRUNCATE_ONLY option can also be used for this process but it empties all transaction log content without backing it up. As TRUNCATE_ONLY option is not available in advance version of SQL Server, so many users use this option along with DBCC SHRINKFILE function.

Simple recovery model can be utilized to generate the logs that will be used rather than opting truncation transaction logs. Like shown in the following window: SQL Server Transaction Log File with DBCC Shrinkfile

Here, two options can be used to shrink the log file:

  1. Shrinking of Log File by using SQL Server Management Studio or SSMS
  2. Shrink the transaction log file by using T-SQL

In the above-mentioned option, SSMS is a server management tool that is easier to use can instantly reduce or shrink the size of log files. Here the database for which the shrink operation has to be performed can be chosen and by the right-clicking on it Shrink operation can be performed as shown in the following figure: SQL Server Transaction Log File with DBCC Shrinkfile

Here in this tool, you may have to choose the file type as log: SQL Server Transaction Log File with DBCC Shrinkfile

If you want to shrink the file using T-SQL then the following command will have to be used:

DBCC SHRINKFILE (Log Filename, Desired Size in MB)

Read: Top 50 Informatica Interview Questions and Answers

In case of Full Recovery Model, when we are not bothered about losing data then the following commands can be used for shrinking the file:

ALTER DATABASE nameDB SET RECOVERY SIMPLE GO DBCC SHRINKFILE (Log Filename, Desired Size in MB) Go ALTER DATABASE nameDB SET RECOVERY FULL  

Another option is also available to shrink the SQL file and that is by using a command.

SQL Server training

BACKUP LOG nameDB TO Backup Device

In this way, anyone can clear the transaction log file of SQL Server to create free space in the log and reuse that space. As transactions are very much important and the user must keep track of them to make changes in the transaction and to keep the space available for further storing.

Conclusion

Here we can say that as transactions are an imperative and essential part of SQL Server so one must keep track of them. But as many transactions take place so to improve the efficiency of transaction processing it is must keep clean the transaction log files. Clear files will not only provide free space for information storage but also will improve the performance of SQL Server. As per the recovery model used by SQL Server, users can perform these operations. The user can clean the log in three ways choose the best one and keep the log clean and empty.

Read: What Is SQL Queries? List Of All SQL Queries With Examples

    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.


Trending Courses

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

7 days 14 Dec 2019

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

8 days 15 Dec 2019

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

8 days 15 Dec 2019

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

-1 day 06 Dec 2019

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

4 days 11 Dec 2019

Course for testing

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

17 days 24 Dec 2019

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

2 days 09 Dec 2019

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

-1 day 06 Dec 2019

SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

2 days 09 Dec 2019

Comments

Search Posts

Reset

Receive Latest Materials and Offers on SQL Server Course

Interviews