Introduction to Transaction Log in SQL Server

What is Transaction Log in SQL Server?

In SQL Server, a transaction log is a type of file. The transaction log file saves all the transaction records that happened at the time of logging process in the database. Transaction log files in SQL Server databases are very helpful at the time of disaster or when the database corrupts.

After the completion of each transaction or modification in database, a log record is generated by the transaction log file. Transaction log files helps to rollback in the future if it needs. The transaction log provides the facility to undo all changes. For every SQL Server database must be a physical transaction log file.

What Does a Transaction Log File Store?

Transaction log file in SQL Server keeps all information about the SQL Server Database. Some log information is not stored by the log files, like minimal log information SELECT INTO or BULK IMPORT. These logs are partitioned in two parts these smaller parts are called Virtual Log Files (VLFs). The transaction log files can be represented as a circular file in the database.

After the end of logging, it starts from end only when all the conditions are true and the active part of the log is truncated. The truncation is necessary to mark all log files that can be used again and overwritten.

If the following conditions are true then a log record is no longer needed for the transaction log in SQL Server database.

  1. The transaction should be of same part, which is going to execute.
  2. The changes which are done should be maintained by a checkpoint.
  3. The log record is not needed for any type of backup.
  4. The transaction must be in a durable state, i.e. the transaction did not affect the other part of the database; all the changes made by a transaction are permanent.

Working of Transaction Log Files in SQL Server Database

The Microsoft SQL Server maintains a buffer related with the changes made in the database for the better performance. SQL Server writes every transaction immediately but doesn't make change in the primary data files. The technique that helps to make changes in the primary data files (MDF) is called Checkpoints; it shows that the changes made by the admin have been implemented successfully from buffer to primary data file.Transaction log in SQL Server prevents the loss of data that is in the buffer and not stored in the primary data file or master data file. MS SQL Server log file helps to track the illegal changes made by unauthorized person in the database also helps in tracking the source of problems when any threat occurs. Transaction log files are used for the database forensics purpose.

Working Condition: In the SQL Server it is not possible that the database work without transaction log files, because the working criteria of MS SQL Server database does not allow this.

The ACID property of SQL Server database also denies the working of database without transaction log file.

Atomicity:A transaction will be atomic if the transaction began, then it will be completely done neither it will not start.

Consistency:A transaction in the database will be in the consistent state only that time when the transaction doesn't affect the other related data. Means that the database stays is in valid state.

Isolation:Insures that the each and every transaction made by DBA is isolated, and concurrent data does not affect to other transaction.

Durability:The transaction performed by the DBA is in durable state and the database is in their proper working condition.

Growing the Transaction Log Files

The growing problem of transaction log files is normally seen in the databases. The growth of the log file is also an important aspect of the database that can't be ignored. So the maintenance of the log file becomes necessary. In the SQL Server databases we can modify the log file but only one log file can be modified at a time.

The MS SQL Server provides the three types of recovery models to maintain the size of the log files which are discussed as below.

  1. Simple Recovery Model: The simple recovery model allow the transaction log backup. This recovery model is the simplest recovery model among all the present recovery models in SQL Server. It maintains only a minimum amount of information in the SQL Server transaction log file. SQL Server, on its own, truncates the transaction log files (excluding logs from any open transactions) and removes the information related to transactions which have reached transaction checkpoints (data has been written to the data file) so that the space can be reused, leaving no transaction log entries for disaster recovery purposes.

  2. Bulk Recovery Model: The main advantage of using the bulk recovery model is that the transaction logs in SQL Server not grows super singly. Bulk recovery model does not allow the automated recovery of the transaction logs truncation. When no bulk operation is performed by the DBA then bulk recovery model behaves as a Full Recovery Model. The transaction logs may grow if the transaction logs backup is not created.
  3. Full Recovery Model: The full recovery model is supported by the transaction log file and it is recommended that it should be done regularly. Normally using the full recovery model, chances of data loss is minimal. The full recovery model is generally used for the production environments.

Importance of Transaction Log File in SQL Server Database

The transaction log file is one of the most important resources to recover database when it comes under a disaster scenario or the database gets corrupted. However, when we use the simple recovery model then the transaction log files are not needed, but the chances of data loss increases.

The transaction log backups are important because these backups mark the inactive virtual log files that can be used to write the new transactions.

Transaction Log file is also very helpful in three ways:

  1. The transaction log file is used for the recovery purpose because it keeps every transaction record made by the database administration.
  2. Transaction Log Files also help in the recovery of every insufficient data transaction when the server has just started working. This operation is used to access the recovery point while the database should be in a working condition.
  3. In case of an error, the transaction log file helps database administrator to renovate the database. When we perform this operation all the statics and info remains unchanged.

  4. One of the most important features of SQL Server transaction log file is that, this file is like the backup file of the database. These files are saved with the extension .bkp.

Maintaining the Transaction Log in SQL Server:

All the changes made in the database are written independently. Therefore, the maintenance of SQL Server Log file is also very important. The transaction log can be maintained using the DBCC SQLPREF statement.

DBCC SQLPERF (LOGSPACE); GO

The transaction logs should be backed up on the daily routine to avoide the auto growth and filling of the transaction log file.

Conclusion

The transaction logs in SQL Server are very necessary for every SQL Server Database. But often it is found that the database designers drop this. You can have multiple transaction log files in SQL Server database. The article describes the importance, working, and maintenance of SQL Server transaction log.

Previous
Next Post »

EmoticonEmoticon