Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Seeking advice on database maintenance and transation log Expand / Collapse
Posted Monday, August 6, 2012 4:31 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 20, 2015 2:54 PM
Points: 2, Visits: 20
We have database maintenance procedures which, in addition to performing backups, remove records older than a user-specified time limit in order to lower the size of the database files on disk. I have a number of questions regarding improving the operation of the "pruning" tasks in regard to the transaction log. The database recovery model is currently set to Simple.

First, there is a requirement to allow the pruning/delete operations while the database is fully user-accessible. As a safety measure, I wrap these operations in a transaction. Is this the best approach? Should I be adamant that the database be placed in single user mode or taken offline?

Second, I find that the transaction log grows from about 3K to 60GB after the pruning is performed on a 4 GB database. I've tried batching the deletes into groups of around 10,000 records, and this improved matters, but the transaction log still ended up at around 40GB.

Here is a general outline of how the deletions proceed:
1. Delete from sub-table where timestamps in master table records are older than X.
(This is due to integrity constraints.)
2. Delete from master table where record timestamps are older than X.
3. Delete from other sub-tables where foreign key in master table is no longer found.
4. Delete from further sub-tables where timestamps are older than X.

I am using sub or nested queries in steps 1 and 3 (delete from sub-table where record ID in select... from master), (delete from sub-table where not in select ... from master).
Is there a more transaction-log-friendly way to go about this?

If the transaction log growth after deletion is expected behavior, I have noticed that I can back up the database after pruning and then shrink the file with DBCC SHRINKFILE.
Further questions:
1. The prune/backup/shrinkfile approach just mentioned above would necessitate locking down the database (single user mode or offline) to prevent the loss of user transactions during this set of operations, correct?
2. Would it be better to employ a Full Recovery model and start backing up/shrinking the transaction log instead?
Thank you for any advice.
Post #1340933
Posted Monday, August 6, 2012 4:55 PM



Group: General Forum Members
Last Login: 2 days ago @ 4:50 PM
Points: 23,515, Visits: 37,731
One, you really don't want to shrink your database on a regular basis, it will result in both index and file fragmentation will affect performance. regarding the transaction log, may I suggest reading this article:

Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1340942
Posted Monday, August 6, 2012 9:10 PM

Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, March 21, 2014 9:46 AM
Points: 387, Visits: 1,078
Shrinking Log file is not recommended until you run out of drive space. If your disk can hold 60GB log file, you can leave as of now.

Also when the pruning activity happens? Off -peak time?
Post #1340979
Posted Monday, August 6, 2012 9:40 PM

Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 10:54 AM
Points: 3,486, Visits: 2,016
You might consider looking into partitioning. Partition your table so that the rows you want to get rid of are in their own partition then use SWITCH to put them into a whole different table, then drop that table. I've read about doing this several times although right now I can't think where. You should be able to find it easily enough. You might consider looking at Kimberly Tripps blog, she does a lot on partitioning.

The last half of this article seems to do what you are looking for.

Kenneth Fisher
I strive to live in a world where a chicken can cross the road without being questioned about its motives.
For better, quicker answers on T-SQL questions, click on the following...
For better answers on performance questions, click on the following...

Link to my Blog Post -->
Post #1340984
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse