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

Comments

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

  1. ser Avatar
    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)

    1. Jugal Shah Avatar

      You can restoer headeronly command

    2. Jugal Shah Avatar

      you can use restore headeronly command or check the backup system table to check that

    3. Todd Avatar
      Todd

      backupmediaset.is_compressed =1

  2. refaai Avatar
    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

  3. Johnson Avatar
    Johnson

    Great Information! I got my solution here thanks for sharing. I have found another helpful article regarding backup compression in sql server. see here: http://www.sqlserverlogexplorer.com/advantages-of-backup-compression/

  4. […] T-SQL Script to Check the Native Compressed backup file size and Backup Compression Ratio Link […]

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