Pages

Sunday, August 12, 2012

Find the database growth in SQL Server

How to find the database growth in SQL Server


Can we find the database growth and find out at what is the percentage of growth of the database weekly/monthly/quaterly or yearly.

The answer is YES. We can find the history growth of the database.

Key point to remember for maintaining the growth history of database is not to delete the backup history in MSDB database.

DBA's generally create the maintenance plan for every SQL Server installed to have a proper backups of the  databases residing in it.

While creating mark a point where you create HISTORY CLEAN UP TASK. In that task keep deletion history value as atleast 12 months, so that you can have atleast an year growth details of a database.

Query used to find the growth details is:


DECLARE @dbname sysname

/* Work with current database if a database name is not specified */

SET @dbname = 'Databasename'

/*change the databasename that you want */

SELECT CONVERT(char, backup_start_date, 111) AS [Date], --yyyy/mm/dd format
 CONVERT(char, backup_start_date, 108) AS [Time],
 @dbname AS [Database Name], [filegroup_name] AS [Filegroup Name], logical_name AS [Logical Filename], 
 physical_name AS [Physical Filename], CONVERT(numeric(9,2),file_size/1048576) AS [File Size (MB)],
 Growth AS [Growth Percentage (%)]
FROM
(
 SELECT b.backup_start_date, a.backup_set_id, a.file_size, a.logical_name, a.[filegroup_name], a.physical_name,
  (
   SELECT CONVERT(numeric(5,2),((a.file_size * 100.00)/i1.file_size)-100)
   FROM msdb.dbo.backupfile i1
   WHERE  i1.backup_set_id = 
      (
       SELECT MAX(i2.backup_set_id) 
       FROM msdb.dbo.backupfile i2 JOIN msdb.dbo.backupset i3
        ON i2.backup_set_id = i3.backup_set_id
       WHERE i2.backup_set_id < a.backup_set_id AND 
        i2.file_type='D' AND
        i3.database_name = @dbname AND
        i2.logical_name = a.logical_name AND
        i2.logical_name = i1.logical_name AND
        i3.type = 'D'
      ) AND
    i1.file_type = 'D' 
  ) AS Growth
 FROM msdb.dbo.backupfile a JOIN msdb.dbo.backupset b 
  ON a.backup_set_id = b.backup_set_id
 WHERE b.database_name = @dbname AND
  a.file_type = 'D' AND
  b.type = 'D'
  
) as Derived
WHERE (Growth <> 0.0) OR (Growth IS NULL)
ORDER BY logical_name, [Date]

------------------------------------------------------------

If we want to find the history growth of all user created databases, then please use the below mentioned query:


set nocount on
Declare @C1 Cursor
DECLARE @dbname sysname
/* Work with current database if a database name is not specified */
set @C1 = cursor for 
select name from sys.databases where database_id>4
open @C1
fetch next from @C1 into @dbname
while @@fetch_status = 0
begin

SELECT CONVERT(char, backup_start_date, 111) AS [Date], --yyyy/mm/dd format
 CONVERT(char, backup_start_date, 108) AS [Time],
 @dbname AS [Database Name], [filegroup_name] AS [Filegroup Name], logical_name AS [Logical Filename], 
 physical_name AS [Physical Filename], CONVERT(numeric(9,2),file_size/1048576) AS [File Size (MB)],
 Growth AS [Growth Percentage (%)]
FROM
(
 SELECT b.backup_start_date, a.backup_set_id, a.file_size, a.logical_name, a.[filegroup_name], a.physical_name,
  (
   SELECT CONVERT(numeric(5,2),((a.file_size * 100.00)/i1.file_size)-100)
   FROM msdb.dbo.backupfile i1
   WHERE  i1.backup_set_id = 
      (
       SELECT MAX(i2.backup_set_id) 
       FROM msdb.dbo.backupfile i2 JOIN msdb.dbo.backupset i3
        ON i2.backup_set_id = i3.backup_set_id
       WHERE i2.backup_set_id < a.backup_set_id AND 
        i2.file_type='D' AND
        i3.database_name = @dbname AND
        i2.logical_name = a.logical_name AND
        i2.logical_name = i1.logical_name AND
        i3.type = 'D'
      ) AND
    i1.file_type = 'D' 
  ) AS Growth
 FROM msdb.dbo.backupfile a JOIN msdb.dbo.backupset b 
  ON a.backup_set_id = b.backup_set_id
 WHERE b.database_name = @dbname AND
  a.file_type = 'D' AND
  b.type = 'D'
  
) as Derived
WHERE (Growth <> 0.0) OR (Growth IS NULL)
ORDER BY logical_name, [Date]
fetch next from @C1 into @dbname
end
close @C1
deallocate @C1

--------------------------------------