Remove Transaction Log When 10% of Allocated Space Remained - sql-server

I have a limited space in the server and i have to remove transactions periodically. Moreover, I use below query that answered in this StackOverFlow question:How do you clear the SQL Server transaction log?
USE db;
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE db
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (db_Log, 1);
GO
-- Reset the database recovery model.
ALTER DATABASE db
SET RECOVERY FULL;
GO
However, i want this simple query to be much more advanced and put below conditions.
Remove Transaction Logs When 10% free space remained from the db_Log(100 mb free 900 mb used)
Transaction Logs file Must have a fixed size(1GB)

This is just an XYProblem. The problem isn't the transaction log size, it that's you aren't taking transaction log back ups and wondering why the transaction log is growing. It's growing because you aren't backing it up.
Either you need to add an agent task to regularly create transaction log back ups, or change the recovery model. Considering your statement "actually the transaction logs are not very important" I suggest the latter, and then set the max size of the file:
ALTER DATABASE db SET RECOVERY SIMPLE;
GO
ALTER DATABASE db
MODIFY FILE (NAME=db_Log, MAXSIZE = 1024MB);

Related

Sql-DBCC Shrink database not reducing database size

I am using SQL Server express edition with 10 GB Size limit, There some table having millions + rows and I need to perform data purging operation at the end of every month, so as to free up the space and make it available for new data storage
If I use the shrink through SSMS(Right click on database then task then shrink ) it works smoothly and free up the space as expected.
But the problem arises when I try to achieve the same with
ALTER DATABASE MyDatabase SET RECOVERY SIMPLE
GO
DBCC SHRINKDATABASE(MyDatabase)
GO
ALTER DATABASE MyDatabase SET RECOVERY FULL
GO
It literally show no effect, infact it increase the log file size but never freeing up the space.Is there any solution,it will be greatly appriciable
Thanks in advance
I would not switch the recovery model to SIMPLE to force shrinking.
When the recovery model is set to FULL you must execute a full backup and at least one transaction log backup to fulfil the recovery condition before you can shrink the database.

I have created database and initial log file size set to 15mb , i am shrinking the log file by using dbcc shrinkfile(khadar_LOG,5); GO

QRY
USE HEMA
ALTER DATABASE HEMA
SET RECOVERY SIMPLE;
GO
DBCC SHRINKFILE(HEMA_LOG,5);
GO
ALTER DATABASE HEMA
SET RECOVERY FULL
GO
after executing this query i have checked space by using dbcc sqlperf(logspace)
i found log space used(%) is keep on increasing
Please help what does it mean if log space used is keep on increasing ..
Make sure that your databases are NOT set to auto-shrink.Databases that are shrank at continuous intervals can encounter real performance problems or You can change the database to the Simple recovery model. see here: http://www.sqlserverlogexplorer.com/how-to-clear-transaction-with-dbcc-shrinkfile/
It is in FULL recovery mode so have a look at what is causing the log space not to be reused. Run the following:
USE db_name;
go
dbcc loginfo
Look at the status column. My guess is that it is status 2. This means that those portions of the log are awaiting a transaction log backup before the log space can be re-used.
You should set up transaction log backups to meet your recovery requirements. Anywhere from every 5 minutes to every x hours. The more often you run the log backups, the smaller the
log files will need to be sized. The size is dependent on the amount of insert/delete/update activity that goes on between each log backup.
The bottom line is that if you run backups of the transaction log you shouldn't be swapping to SIMPLE and shrinking.

How to split SQL Server Transaction Log

My database server has just ran out of disk space. The Transaction log is taking over 100GB space and I have only 30GB free space. Because of this some transactions are taking infinite amount of time to process resulting in 9002 error.
I would like to ask is there a way I can split this transaction log and delete one of them to get some free space. There is no way now that I can take a backup or increase the disk space by adding another disk.
If any one has a better solution than splitting up the transaction log, then that would be great.
There are lots of good resources about dealing with large logs linked to from here:
https://sqlblog.org/2009/07/27/oh-the-horror-please-stop-telling-people-they-should-shrink-their-log-files
Basically, you either need to get more space, backup the logs, or switch to simple recovery mode on your databases. Each approach has its ups and downs.
in Practice
Data, Logs & OS should be on different disks...
say C for OS, E for Data, and L for Logs.
and if your transaction logs doesnt shrink after a transaction backup then you might want to check you setting. FULL or Simple (doesnt shrink logs).
to force shrink a Log wherein database is set to simple is :
Alter database <databasename>set Recovery simple /* if database is set to FULL */
USE <databasename>
DBCC SHRINKFILE (<log_name>, 0, TRUNCATEONLY)
GO
Alter database <databasename> set Recovery full /* if database is set to FULL */
hope this helps

CheckPoint Vs DBCC Shrinkfile

i have million of records insertion and procession in a table. DB is in Full recovery Mode.
when Any insert command and group by etc.. executed it filled off ldf file(of size 1mb) to (120 GB).
Shld i use Checkpoint or Alter database set recovery simple and then shrinkfile etc.
i think as i am inserting data
Checkpoint will not help you in reducing transaction log size with Full recovery model. I see two option here:
truncate transaction log (BACKUP DATABASE [YourDBName] WITH NO_LOG), though this will not work with SQL 2008+ as this option is discontinued (read more details here)
switch to Simple recovery model (which is recommended way of freeing space in transaction log (see 'Transaction Log Truncation' section))
Both of the above option deal with freeing space. After using any of them you will have to shrink log file anyway.
Backup your transaction log on a very regular basis (e.g. every 5 minutes).
This will allow the transaction log to be reused and it won't grow as large.

SQL Server: How do I increase the size of the transaction log?

How do I increase the size of the transaction log? Is is also possible to temporarily increase the transaction log?
Let's say I have the following scenario. I have a Delete operation that's too big for the current transaction log. I wan't to:
Increase the transaction log (can I detect the current size?, can I tell how large I need the transaction log to be for my operation?)
(Perform my operation)
Backup the transaction log
Restore the size of the transaction log.
Short answer:
SQL 2k5/2k8 How to: Increase the Size of a Database (SQL Server Management Studio) (applies to log also), How to: Shrink a Database (SQL Server Management Studio)
SQL 2K How to increase the size of a database (Enterprise Manager), How to shrink a database (Enterprise Manager)
Long answer: you can use ALTER DATABASE ... MODIFY FILE to change the size of database files, including LOG files. You can look up master_files/sysfiles (2k) or <dbname>.sys.database_files (2k5/2k8) to get the logical name of the log. And you can use DBCC SHRINKFILE to shrink a file (if possible).
can I tell how large I need the
transaction log to be for my
operation?
It depends on a lot of factors (is this new data? is it an update? is it a delete? what recovery model? Do you have compression on SQL 2k8? etc etc) but is usually bigger than you expect. I would estimate 2.5 times the size of the update you are about to perform.
Update:
Missed you say is an DELETE. A rough estimate is 1.5 times the size of the data deleted (including all indexes).
The transaction log can be configured to expand as needed. You set the option to grow automatically.
However when the transaction log gets too big (running out of disk space) or making sql server unusable.
Back up transaction log. SQL will auto truncate inactive transactions
When you restore the transaction log will be reduced
To configure autogrow:
Right click on the database in management studio.
Select Properties
Update Autogrowth value
The most important part is the last line of your scenario: "Restore the size of the transaction log." You mean shrink the log back to its original size.
This is really dangerous for a lot of reasons, and we've covered them in a couple of stories over at SQLServerPedia:
http://sqlserverpedia.com/wiki/Shrinking_Databases
http://sqlserverpedia.com/blog/sql-server-backup-and-restore/backup-log-with-truncate_only-like-a-bear-trap/
http://sqlserverpedia.com/blog/sql-server-bloggers/i-was-in-the-pool-dealing-with-sql-shrinkage/

Resources