Sqlserver-dba.com

SQL Server database free space with sys.dm_os_performance_counters

sys.dm_os_performance_counters have enough information to measure SQL Server database free space.  Broken down into  data file and log file used space

 The purpose of sys.dm_os_performance_counters is to return a row per performance counter.

 The script below returns a row per database on an SQL Server instance.

The columns are:

DBASE_NAME =  database name

DBASE_SIZE_MB = Data file size in MB

LOG_SIZE_MB = Log file Size in MB

LOG_USED_PERC =  Current value of the counter (Log file Used size) * 100  \ Current value of the counter (Log file Size )

RECOVERY_MODEL_DESC = Recovery or Simple

IS_AUTO_SHRINK_ON = Auto Shrink

 -- SQL Server 2008

-- Jack Vamvas

-- June 1, 2011

-- http://www.sqlserver-dba.com

-- Twitter: jackvamvas

 -- Instance level query *******************************

 SELECT

  DMOSPC_1.instance_name AS DBASE_NAME,

  DMOSPC_3.cntr_value/1024.00 AS DBASE_SIZE_MB,

  DMOSPC_1.cntr_value/1024.00 AS LOG_SIZE_MB,

  CAST(DMOSPC_2.cntr_value*100.00/DMOSPC_1.cntr_value AS DEC (10,2)) AS LOG_USED_PERC,

  DBASE.RECOVERY_MODEL_DESC ,

  DBASE.IS_AUTO_SHRINK_ON

 FROM

  sys.databases AS DBASE

INNER JOIN

  sys.dm_os_performance_counters AS DMOSPC_1 ON DMOSPC_1.instance_name = DBASE.name

  AND DMOSPC_1.counter_name = 'Log file(s) Size (KB)'

INNER JOIN

  sys.dm_os_performance_counters AS DMOSPC_2 ON DMOSPC_1.instance_name = DMOSPC_2.instance_name

  AND DMOSPC_2.counter_name = 'Log file(s) Used Size (KB)'

INNER JOIN

  sys.dm_os_performance_counters AS DMOSPC_3 ON DMOSPC_1.instance_name = DMOSPC_3.instance_name

  AND DMOSPC_3.counter_name = 'Data File(s) Size (KB)'

  -- Things to look at:

-- Is LOG_USED_PERC close to the threshold?

-- Is AUTO_SHRINK_ON . ?

-- Is Recovery Model SIMPLE or RECOVERY?

 I prefer to have IS_AUTO_SHRINK_ON as off. As shrinking the file leads to sql fragmentation.

Check SQL Server Disk Space Check  to check insufficient disk space and it also displays the number of Kb that each file will grow by on the next extend.

Source:Jack Vamvas (http://www.sqlserver-dba.com)

Author: Jack Vamvas (http://www.sqlserver-dba.com)

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact