2010. 6. 4. 01:15

DB파일사이즈

 Collecting database usage information for free space and trending

I find it important in my environment to monitor the following database/database file level metrics:

  • Server/Instance Name
  • Database Name
  • Database File Names (both logical and full physical path)
  • File Size (In Megabytes)
  • Database Status
  • Recovery Mode
  • Free Space (In Megabytes and Percent)

To collect this information I need to tap into the either the master.dbo.sysfiles system table in SQL 2000 or master.sys.sysfiles compatibility view in SQL 2005. I also need to make use of a few T-SQL functions at the DBA's disposal. First, let me present the query. Afterwards I'll explain the finer points.

  1. DECLARE @DBInfo TABLE 
    ServerName VARCHAR(100), 
    DatabaseName VARCHAR(100), 
    FileSizeMB INT
    LogicalFileName sysname
    PhysicalFileName NVARCHAR(520), 
    Status sysname
    Updateability sysname
    RecoveryMode sysname
    FreeSpaceMB INT
    FreeSpacePct VARCHAR(7), 
    FreeSpacePages INT
    PollDate datetime

    DECLARE @command VARCHAR(5000

    SELECT @command 'Use [' '?' '] SELECT 
    @@servername as ServerName, 
    '''' '?' '''' ' AS DatabaseName, 
    CAST(sysfiles.size/128.0 AS int) AS FileSize, 
    sysfiles.name AS LogicalFileName, sysfiles.filename AS PhysicalFileName, 
    CONVERT(sysname,DatabasePropertyEx(''?'',''Status'')) AS Status, 
    CONVERT(sysname,DatabasePropertyEx(''?'',''Updateability'')) AS Updateability, 
    CONVERT(sysname,DatabasePropertyEx(''?'',''Recovery'')) AS RecoveryMode, 
    CAST(sysfiles.size/128.0 - CAST(FILEPROPERTY(sysfiles.name, ' 
    '''' 
           
    'SpaceUsed' '''' ' ) AS int)/128.0 AS int) AS FreeSpaceMB, 
    CAST(100 * (CAST (((sysfiles.size/128.0 -CAST(FILEPROPERTY(sysfiles.name, 
    '''' 'SpaceUsed' '''' ' ) AS int)/128.0)/(sysfiles.size/128.0)) 
    AS decimal(4,2))) AS varchar(8)) + ' 
    '''' '%' '''' ' AS FreeSpacePct, 
    GETDATE() as PollDate FROM dbo.sysfiles' 
    INSERT INTO @DBInfo 
       
    (ServerName
       
    DatabaseName
       
    FileSizeMB
       
    LogicalFileName
       
    PhysicalFileName
       
    Status
       
    Updateability
       
    RecoveryMode
       
    FreeSpaceMB
       
    FreeSpacePct
       
    PollDate
    EXEC sp_MSForEachDB @command 

    SELECT 
       
    ServerName
       
    DatabaseName
       
    FileSizeMB
       
    LogicalFileName
       
    PhysicalFileName
       
    Status
       
    Updateability
       
    RecoveryMode
       
    FreeSpaceMB
       
    FreeSpacePct
       
    PollDate 
    FROM @DBInfo 
    ORDER BY 
       
    ServerName
       
    DatabaseName 

 


CAST(sysfiles.size/128.0 - CAST(FILEPROPERTY(sysfiles.name, ' + '''' + 'SpaceUsed' + '''' + ' ) AS int)/128.0 AS int) AS FreeSpaceMB

 

The FreeSpaceMB calculation is simple once you understand what I explained above for the calculations associated with FileSize. To determine free space we need to know how much of the total file space is being consumed. This information is exposed via the FILEPROPERTY() function. FILEPROPERTY() expects the following parameters: file name, and property.

Expect a future tip on the various uses for FILEPROPERTY(), but for now we focus on the SpaceUsed property. This value is also stored in 8Kb pages, so the factor of 1024/8 (or 128) remains constant. Using the same basic rules we discussed above this is what the formula would look like this: Available Space in Mb = (File Size in Mb) - (Space Used in Mb). The formula in the query casts all variables as integer data types and converts the available values from pages to Mb.

I know that one of the major foundations of database normalization is not storing calculated values in a database. However, I like to be able to trend my metadata over time and with the complexity of the formulas I prefer to do all my thinking up-front. That being said, I don't just store the size information in the table and run calculations in my queries after the fact - I do my calculations directly in the query that populates the repository.


 



 

이 글은 스프링노트에서 작성되었습니다.

'Common Admin' 카테고리의 다른 글

문서화되지 않은 프로시저  (0) 2010.06.04
Raid구성성능  (0) 2010.06.04
DBCC 명령어  (0) 2010.06.04
WMI Providers_2  (0) 2010.06.04