Query-1: SQL Server log file growing unexpectedly?
“We are wondering why transaction logs keep on increasing?? It become so gigantic, the transaction log size get increase from 135GB to 350GB and that’s so annoying. My drive containing data store is losing space. I am using Full Recovery Model and the logs are backed up hourly. Please help me out why this is happening and what should be done to stop growing it too big?”
SOLVED: SQL Server Log File Getting Too Big
SQL Server Transaction logs records all activities of transaction logs. Each database in SQL Server instance consists of logs that records all the modification done in the databases. It is crucial component of the databases. Any modification, deletion can be tracked down in transaction logs.
There are various reasons of transaction log file getting expanded. The possible reasons are listed below:
- Large transaction log file
- Transaction taking too long to complete
- Transaction operation fail & may start to rollback
- Issue in performance
- Blocking issue
- Database participating in AlwaysOn availability group
What to do for stopping growing log file too big
- Change the type of Recovery Model to Simple Recovery Model only if you are fine with not able to restore logs hence, failed point in time recovery. It is because if you are truncating your logs, you are breaking Transaction log LSN & if any disaster occur, you will not able to restore T-logs. Hence if you are fine with this situation, then change your Recovery model to Simple. This will not allow any extra growth of log file.
- Take T-log backup every hour in Full recovery Model, your transaction log will keep on growing until you take backup and truncate of it. While taking hourly T-log backup, the logs will grow until one hour, but after this it would truncate all commited transaction.
Sometimes it might happen that you are in Simple Recovery Model, but your logs are still growing!!! Reason being is long running transaction. If your transaction taking long time when deleting thousand records in one delete statement, then logs will not be able to truncate until delete operation is done.
You can save yourself by maintaining proper size of log file or monitoring the usage of transaction.
By doing this, you can cut down the size of Transaction log.
Query-2 How To Shrink Transaction logs File in SQL Server?
“My log file size is growing continuously. Why is this happening?? I am lacking my disk space. I want to shrink my Transaction log file. Can anyone tell me how to shrink transaction logs in SQL Server. What will be the after effects if I shrink the log file.
SOLVED: Reduce SQL Server Database Transaction Log File
Relax!! There are several reasons for transaction log file growth. Long Running transaction, lack of taking log backup are some of the reasons which result in transaction log growth. Databases running for business purposes or production requires appropriate recovery model to be chosen for log management.
Log truncation frees up the log file space so that logs can reuse it. Truncation occur automatically if the database is backed up in Simple Recovery Model or after log backup when database is in Full Recovery Model.
Here I will resolve your shrink issue by two methods: By using SQL Server Management Studio and by using T-SQL.
Shrink By SQL Server Management Studio:
- Right Click on your Database–>Tasks–>Shrink—>Files.
- You will get the Shrink File Window, Change the File Type to Log.
- There are three different actions under Shrink Action option. Either release the unused space, or shrink file to reorganize pages before releasing unused space, or empty file by migrating the data to other files in the same filegroup. Choose it according to your need.
Shrink By T-SQL:
You can simply shrink transaction log file by running DBCC SHRINKFILE after taking log back. If you are using Simple Recovery Model, run below code:
DBCC SHRINKFILE (TestDB_log, 1)
If your database is in Full Recovery Model, first of all, set it to Simple Recovery Model, Then run DBCC SHRINKFILE and then set it to full.
Caution: Setting database again in Full Recovery Model leads to loss of data in the Log. So, if you dont care about losing your log data, then definitely go for it!
Tips when you plan to shrink database or file:
- Shrink operation found to be most effective after delete or drop table operation as it create lot of unused space.
- If you are repeatedly shrinking your database and you notice that the size of database grows again. Alert!! Shrinking database is a wasted operation, as most database require free space for regular day-to-day operation. This indicates that the space that was shrunk was a part of regular operation.
- You should not shrink your database or data file after rebuilding indexes. As shrinking increases fragmentation to extreme level.
- Do not ON Auto_Shrink option. (Unless you have very urgent requirement)
Is Shrinking database Bad?
After shrinking operation,you will definitely be able to reduce the size of the database. But Wait!! Check your Fragmentation level first. It is found to be way too much!!!
Shrink operation increases the value of the fragmentation way too much. And higher fragmentation costs you poor performance of the database.
While if you are thinking that you can reduce fragmentation by using rebuild index, let me alert you, this will definitely reduce your fragmentation level. But Let me check the size of the database The database size increases way higher than the original.
Query-3 How to check Deleted Records in SQL Server?
“I am working in an organization and my manager assigned me work to check deleted database records of my database. How can I check my deleted records in SQL Server? Please Help me out from this situation.”
SOLVED: Ways to Check Deleted Records in SQL Server
You can track down your deleted database records by reading Transaction logs. And you can read it by using fn_dblog function. You can track down any DML as well as DDL activity like insert, delete , update, create operation by checking your transaction logs.
Fn_dblog function enables users to read all the database activity happening in SQL database. The function require begin and end LSN number for a transaction. You can easily trackdown the information of a person who did changes to your database. As LSN number are not in human readable form, SQL Server provide fn_dblog function for easy reading of transaction logs.
You can simply check your database activity by implementing below code:
select [Current LSN],
How to Read Transaction Log Quickly
One quick way to read SQL Server database activity is to use SysTools SQL Log Viewer. It read & analyse operation like insert, update, delete. The software previews all .ldf activities like transaction name, table name, query etc. You have option to choose the database from different mode ie Online & Offline mode. You will able to fetch it from live databases. If your database is in Simple Recovery Mode, the tool is capable to recover deleted database records. You will get three different option for exporting. You can export it as in SQL Server Database , as CSV or as Compatible Script.
The blog covers various users queries about transaction logs along with its possible solution. I have discussed why transaction logs keep on increasing, how to track down your transaction details of your database and how to shrink transaction logs. I have also discussed some quick tips when you shrink your database.