T-SQL Script to Check the Native Compressed backup file size and Backup Compression Ratio


First of all make sure backup compression is enabled on the SQL Server. You can execute below query to check the backup compression.

select name,[description],value_in_use from sys.configurations where name like '%backup%'

Execute below script to check the compressed backup file size and backup compression ratio.

Declare @FromDate as datetime
-- Specify the from date value
set @FromDate = GETDATE() -1

SELECT  
   CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS SQLServerName, 
   msdb.dbo.backupset.database_name,  
   CASE msdb..backupset.type  
       WHEN 'D' THEN 'Database'  
       WHEN 'L' THEN 'Log'  
       WHEN 'I' THEN 'Differential'  
   END AS backup_type,  
   msdb.dbo.backupset.backup_start_date,  
   msdb.dbo.backupset.backup_finish_date, 
   msdb.dbo.backupset.expiration_date, 
   DATEDIFF (SECOND, msdb.dbo.backupset.backup_start_date, msdb.dbo.backupset.backup_finish_date) 'Backup Elapsed Time (sec)',
   msdb.dbo.backupset.compressed_backup_size AS 'Compressed Backup Size in KB',
  (msdb.dbo.backupset.compressed_backup_size/1024/1024) AS 'Compress Backup Size in MB',
   CONVERT (NUMERIC (20,3), (CONVERT (FLOAT, msdb.dbo.backupset.backup_size) /CONVERT (FLOAT, msdb.dbo.backupset.compressed_backup_size))) 'Compression Ratio',
   CASE msdb..backupset.type  
       WHEN 'D' THEN 'Database'  
       WHEN 'L' THEN 'Log'  
   END AS backup_type,  
   msdb.dbo.backupset.backup_size,  
   msdb.dbo.backupmediafamily.logical_device_name,  
   msdb.dbo.backupmediafamily.physical_device_name,   
   msdb.dbo.backupset.name AS backupset_name, 
   msdb.dbo.backupset.description 
FROM   msdb.dbo.backupmediafamily  
   INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id 
WHERE 
CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= @FromDate
AND msdb.dbo.backupset.backup_size > 0 
ORDER BY  
   msdb.dbo.backupset.database_name, 
   msdb.dbo.backupset.backup_finish_date

Advertisements

10 thoughts on “T-SQL Script to Check the Native Compressed backup file size and Backup Compression Ratio

  1. ser

    Thanks a lot this is helpful. I have one question: Is there a way to tell if compression is used or not…Like if we can have one column that tells compressed or not (1 or 0)

    Reply
  2. Pingback: SQL SCRIPTS DECEMBER 24, 2014 How to restart an Interrupted Database Restore in SQL Server? Steps to restart an Interrupted Database Restore in SQL Server There may scenario when you are restoring VLDB database backup and the restore database command inte

  3. Pingback: SQL SCRIPTS DECEMBER 24, 2014 How to restart an Interrupted Database Restore in SQL Server? Steps to restart an Interrupted Database Restore in SQL Server There may scenario when you are restoring VLDB database backup and the restore database command inte

  4. Pingback: SQL SCRIPTS DECEMBER 24, 2014 How to restart an Interrupted Database Restore in SQL Server? Steps to restart an Interrupted Database Restore in SQL Server There may scenario when you are restoring VLDB database backup and the restore database command inte

  5. refaai

    use msdb
    select database_name,type,DATEPART(day,backup_start_date),sum((dbo.backupset.compressed_backup_size)/(1024*1024*1042)) from dbo.backupset where
    –dbo.backupset.database_name=’HOHaj070113′ and
    –type=’I’ and
    backup_start_date >= ‘20140601’ group by cube(database_name, DATEPART(day,backup_start_date),type) order by 1 desc ,2 desc,3 desc

    Reply
  6. Pingback: SQL Server – Database Backup Compression | Learning in the Open

Thanks for the comment, will get back to you soon... Jugal Shah

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s