join the MSSQLTips community

Today's Site Sponsor


 

Find performance issues related to Analysis Services memory limits.
 


Exploring Database Backup Compression in SQL Server 2008
Written By: Edwin Sarmiento -- 6/5/2008 -- 0 comments -- printer friendly -- become a member



Free SQL Server Performance Dashboard & Screensaver

        Win SQL Books  -----  SharePoint Tips  -----  Live Webcast - SQL Backup Mistakes  -----  Bookmark and Share        

Problem
Database backups and maintenance windows for very large database are very frustrating especially if we need to generate full database backups to initialize a database mirroring or transaction log shipping session. As we are looking through the new features in SQL Server 2008 we found a potentially interesting one called Database Backup Compression which we can use to significantly reduce our backup and restore operations.  Can you give us a detailed explanation of how we go about using this one?

Solution
Database Backup Compression is a new feature in SQL Server 2008 which significantly reduces backup and restore operations. By default, backup compression is turned off on the server instance-level. Unlike other third-party software which provides different levels of compression, SQL Server 2008 provides only one level.

In this tip we are going to look at how to implement database backup compression and compare the time it takes to do backups and restore for a normal database backup versus the compressed one as well as the backup file size. Let's start by creating a full backup of the AdventureWorks database, which is about 178.75 MB in size. I have chosen to use a TSQL script to do the backups and restore so I can take note of the amount of time it takes for the processes to complete. I am running the test on a VMWare image with a dedicated RAM of 2GB so results may vary when running on a physical server

BACKUP DATABASE AdventureWorks 
TO DISK = 'C:\SQLData\AdventureWorks_uncompressed.bak'
WITH INITSTATS 10

The default, uncompressed backup database process took about 29.20 seconds on my instance while the backup size is about 132 MB.

Now, let's implement database backup compression on our AdventureWorks database.

BACKUP DATABASE AdventureWorks 
TO DISK = 'C:\SQLData\AdventureWorks_compressed.bak'
WITH COMPRESSIONINITSTATS 10

Notice that there is really nothing new in the BACKUP DATABASE command except for the inclusion of the COMPRESSION keyword in the WITH clause. This tells SQL Server to compress the database backup since compression is turned off by default. The compressed backup database process took about 20.47 seconds while the backup size is about 35.31 MB - quite a significant reduction in size and in process time as well.

You can compare the difference in processing time between performing a compressed versus an uncompressed backup using the script below

DECLARE @timeDiff DATETIME

SET @timeDiff GETDATE()
BACKUP DATABASE AdventureWorks 
TO DISK = 'C:\SQLData\AdventureWorks_uncompressed.bak'
WITH INIT

SELECT DATEDIFF(ms@timeDiffGETDATE()) AS uncompressed_time_in_ms

SET @timeDiff GETDATE()
BACKUP DATABASE AdventureWorks 
TO DISK = 'C:\SQLData\AdventureWorks_compressed.bak'
WITH COMPRESSIONINIT

SELECT DATEDIFF(ms@timeDiffGETDATE()) AS compressed_time_in_ms

You can examine the backupmediaset and the backupset tables in the msdb database for the new columns that pertain to backup compression. In the backupset table, you can see the compressed_backup_size column which pertains to the total byte count of the backup stored on disk. You can calculate the compression ratio by using the backup_size and the compressed_backup_size columns

SELECT backup_size/compressed_backup_size AS compression_ratio, * 
FROM msdb..backupset

In the backupmediaset table, the is_compressed column tells you whether or not the backup is compressed. Although you can rely on these columns to determine whether or not a backup media is compressed, it is recommended to name the backup media properly so as not to get confused on which media to use when disaster strikes. You can also use backup compression when creating database maintenance plans.

Let's compare the restore process for both the compressed and the uncompressed backup. I'll start restoring the uncompressed backup using a different name and the MOVE option so I don't overwrite the existing MDF and LDF files for the original AdventureWorks database.

USE master
GO

RESTORE DATABASE AdventureWorks_uncompressed 
  
FROM DISK = N'C:\SQLData\AdventureWork_uncompressed.bak'
  
WITH MOVE N'AdventureWorks_Data' TO N'C:\SQLData\AdventureWorks.mdf',      
  
MOVE N'AdventureWorks_Log' TO N'C:\SQLData\AdventureWorks_1.ldf',     
  
RECOVERY,  
  
STATS 10;
GO

The restore process for the uncompressed database took around 39.72 seconds.

We'll restore the compressed database backup this time. Note that it is the exact same RESTORE DATABASE command we used for an uncompressed database backup as SQL Server knows from the backup media metadata whether or not it is compressed.

USE master
GO

RESTORE DATABASE AdventureWorks_compressed   
  
FROM DISK = N'C:\SQLData\AdventureWorks_compressed.bak'
  
WITH MOVE N'AdventureWorks_Data' TO N'C:\SQLData\AdventureWorks_c.mdf',
  
MOVE N'AdventureWorks_Log' TO N'C:\SQLData\AdventureWorks_c.ldf',     
  
RECOVERY,  
  
STATS 10;
GO

The restore process for the compressed database took around 22.03 seconds. That's about 45% reduction in restore time.

Enabling Database Backup Compression on the server instance level

As database backup compression is turned off by default, you can choose to enable it on the server instance level by using the sp_configure system stored procedure.

USE master
GO

EXEC sp_configure 'backup compression default''1'
RECONFIGURE WITH OVERRIDE 

You can also do so by setting the server instance properties using SQL Server Management Studio. On the Database Settings section of the Server Properties window, just click the Compress Backup checkbox and this feature will be enabled on the server instance level.

NOTE: While database backup compression feature is only available in the Enterprise Edition, restoring a compressed database backup can be can be done on all editions of SQL Server 2008.

Next Steps

You have seen how you can implement database backup compression in SQL Server 2008 to reduce the duration of both backup and restore operations which can be very helpful in very large databases.

  • Download a copy of the latest Community Technology Preview of SQL Server 2008 from this site.  The above examples were created using the February 2008 CTP.
  • You can get the AdventureWorks database used in the sample here(click on AdventureWorksDB.msi).
  • To view the SQL Server 2008 Books Online content on Database Backup Compression, go to this site.
Readers Who Read This Tip Also Read Comment or Ask Questions About This Tip Twitter This Tip!


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

The SQL Toolbelt – all 13 Red Gate SQL Server Tools.

SQL Server Problems? We deliver innovative answers via our SQL Server Consulting Services

Prepare for your next SQL Server interview with CareerQandA.com

Valuable SQL Server web casts on Performance Tuning, Development, Administration, Disaster Recovery, Replication and more...

Become a member of the MSSQLTips community

Launch your SharePoint career here...

Free whitepaper - Developing Something for Nothing with SQL Server: A Closer Look at SQL Server Express


 

 



Red Gate Software - SQL Backup

Need to create smaller, more reliable backups? Ensure your backups are optimized for robustness and speed with Red Gate SQL Backup Pro. Compress your backups by up to 95% and minimize disruptions to your backups caused by flaky networks with new network resilience. 'Network resilience puts SQL Backup Pro 6 at the top of the list of backup tools. It’s the cherry on top, and I definitely recommend using SQL Backup over SQL Server 2008 native backups.' William Durkin, Development DBA. Download now.

Download now!

More SQL Server Tools
SQL Compare

SQL Data Generator

SQL Nitro

SQL secure

SQL diagnostic manager




Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.