In SQL, the transaction log file is used to save records that are produced during the logging process in a 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 sequential order. In this article, we are going to discuss the SQL Server transaction log and its features.
SQL Server transaction log stores every transaction except a few ones like SELECT INTO and BULK IMPORT operations. Internally transaction log is divided into 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, the 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:
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.
Learn SQL Server in the Easiest Way
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.
Truncation process for the three recovery models of SQL is different and explained as below:
One can know the available and used transaction log space with the help of a single line command as shown in the following figure:
Now after getting the information, you can take action accordingly to clear the log file.
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.
Register for the demo class now and learn all about the SQL Server Transaction log with our certified mentors!
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 Training & Certification
Two steps are involved in the process of clearing SQL Server transaction log:
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:
Here, two options can be used to shrink the log file:
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:
Here in this tool, you may have to choose the file type as log:
If you want to shrink the file using T-SQL then the following command will have to be used:
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.
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.
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 to choose the best one and keep the log clean and empty.
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
MS SQL Server
Receive Latest Materials and Offers on SQL Server Course