Here's a question I got from someone who attended our database maintenance workshop at PASS last week (paraphrased):
I attended your pre-conference session on database maintenance and found it to be very informative. From what you told use though, I think I need to change my nightly backup procedure. I like to get my databases back to as small of a size as possible before backing them up, so I run the following commands to do this before taking the full database backup. Could you help me with a better way of doing this? We're on SQL Server 2005.
BACKUP LOG <mydbname> WITH NO_LOG
DBCC SHRINKDATABASE (<mydbname>)
And here's the answer I sent back:
How large is the database? And how long must you keep the backups around? If the cumulative size of the backups takes up a large proportion of your available storage space (and we're talking more than just a single direct-attached 100+GB drive), then it may be worth compressing the backups – otherwise you're likely causing yourself more trouble than its worth.
By doing BACKUP LOG WITH NO_LOG you're effectively throwing away log records and removing the possibility of doing any kind of point-in-time, or up-to-the-second recovery (see BACKUP LOG WITH NO_LOG – use, abuse, and undocumented trace flags to stop it). If you're running in the FULL recovery model, and you don't care about either of these features, then you should switch to the SIMPLE recovery model. If you really want to be in FULL, don't ever use WITH NO_LOG.
The amount of transaction log that a full backup requires cannot be changed by you truncating the log. The full backup will backup any log it requires to enable the restored database to be a transctionally consistent copy of the database. See Debunking a couple of myths around full database backups and More on how much transaction log a full backup includes.
Doing a DBCC SHRINKDATABASE (the same exact operation as a database auto-shrink) will cause massive index fragmentation, and cause file-system fragmentation of the data files, as they will likely need to grow again after you've squeezed all the space out of them. See Auto-shrink – turn it OFF! for more details on the effects.
If you're really concerned about backup sizes and space is at a premium, I recommend using a 3rd-party backup compression tool such as LiteSpeed or HyperBac so you're not affecting the actual database. Remember also that SQL Server 2008 has native backup compression too – see my blog post here for more details.
Hope this helps
6 Responses to Conference Questions Pot-Pourri #10: Shrinking the database before taking a backup
Recently on SSC there was a similar question which I answered like you did. One of the things I have learned form blogs like yours and other resources, don’t shrink! Of course that depends as well as a TX Log shrink after a one time unusually large edit
IMO is acceptable.
Keep up the writing, I for one enjoy reading your posts, even the ones that are over my head.
Hi Paul,
I’m a regular reader here. I’ve written a backup compression program for SQL Server 2005 up on Sourceforge that might be useful. I wouldn’t mind you taking a look at the code and giving feedback.
http://mssqlcompressed.sourceforge.net/
Cheers,
Clay
Hi
I agree with what’s been said, but have an interesting case where half may data is in
SQL 2000 and the other in pervasive. Each day we load the pervasive data into SQL
So the we have a full set of data. Done via DTS. This creates very large log files, if left they
Grow 80% bigger than the data file. What what should we do? Why does the database
clean the log file automotly?
Regards
Jack
Clay – looks interesting – I don’t have time to download the code and read through it but I’m guessing you’re using the VDI interface to get the data in/out of SQL. Good to see a freeware alternative to the pay-for tools available on 2005. Thanks
Hi Jack (not Jack Corbett),
If your transaction log file *has* to grow (sounds like you’ve got a single very large transaction to insert the new data, rather than splitting into smaller batches) then SQL Server will not shrink it down again once the transaction log records in it are no longer needed. Managing the size of your log files is a big topic – I’ve just written a feature length article on it for the Feb 2009 TechNet Magazine – I’ll blog when the article is available.
Thanks
[...] Shrink just moves pages around so won't make any difference. See my old blog post Conference Questions Pot-Pourri #10: Shrinking the database before taking a backup. And of course, shrink is evil. See A SQL Server DBA myth a day: (9/30) data file shrink does not [...]