Backup compression is a long-awaited feature that was
added in SQL Server 2008. Prior to SQL Server 2008, you had to purchase
third-party tools in order to achieve backup compression. You can only
create a compressed backup using the Enterprise Edition of SQL Server
2008; however, restoring a compressed backup is supported in all
editions of SQL Server 2008.
By default, backup
compression is turned off at the server level. To change the default
configuration for backup compression, you can use the sp_configure stored procedure with the 'backup compression default' parameter. You can specify value of '1' to enable the default behavior for all backups to be compressed and specify '0' to disable default compression. You can run the code in Listing 1 to enable default compression.
Example 1. Code to Enable Default Compression
USE master GO
EXEC sp_configure 'backup compression default', '1'; RECONFIGURE WITH OVERRIDE;
|
You can override the default behavior for backup compression by specifying WITH COMPRESSION or WITH NO_COMPRESSION when issuing the backup statement. Listing 2 shows the syntax to create a backup on the AdventureWorks2008 database both with and without compression.
Example 2. Syntax to Create a Backup with and Without Compression
USE master GO
PRINT '----------AdventureWorks2008 With Compression----------'
--Create a full backup with compression BACKUP DATABASE AdventureWorks2008 TO DISK = 'C:\Backups\AdventureWorks2008_C.bak' WITH COMPRESSION
GO
PRINT Char(13) + '----------AdventureWorks2008 No Compression----------'
--Create a full backup with no compression BACKUP DATABASE AdventureWorks2008 TO DISK = 'C:\Backups\AdventureWorks2008_NC.bak' WITH NO_COMPRESSION
GO
|
As you can see in Figure 1,
a backup using compression completes more quickly because the backup
file is smaller and requires less IO. The compressed backup completed in
8.192 seconds averaging 22.045 MB per second, while the non-compressed
backup completed in 13.209 seconds averaging only 13.672 MB per second.
The increased backup speed
also increases the CPU required to process the backup, which could
impact other operations on the server while the backup is being
performed. If a compressed backup has a negative impact on concurrent
processes, you can use the Resource Governor to limit the CPU used by
the backup process.
There are a couple of factors
that determine the amount of space you will save by using backup
compression. For example, text data compresses a lot more than the other
data types, and encrypted data hardly compresses at all. Also, if you
are using compressed tables, you will not benefit much by using backup
compression. The backupset table in the msdb
holds the backup size and the compressed backup size, so you can
determine the amount of space you are actually saving by using backup
compression. Listing 3 shows a sample query you can use to determine the amount of compression you are achieving by using backup compression.
Example 3. Code to Determine the Compression Percentage per Backup
SELECT database_name,
backup_finish_date,
1 - (compressed_backup_size/backup_size) PercentCompressed
FROM msdb.dbo.backupset
WHERE backup_size > compressed_backup_size