SQL Server 2008: How to query all databases sizes?

后端 未结 15 953
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 18:08

I have MS SQL 2008 R2, 500 databases. What is the most efficient, easiest and \'modern\' way to query all databases sizes.

The output should have columns:

相关标签:
15条回答
  • 2021-01-29 18:58
    IF OBJECT_ID('tempdb.dbo.#space') IS NOT NULL
        DROP TABLE #space
    
    CREATE TABLE #space (
          database_id INT PRIMARY KEY
        , data_used_size DECIMAL(18,2)
        , log_used_size DECIMAL(18,2)
    )
    
    DECLARE @SQL NVARCHAR(MAX)
    
    SELECT @SQL = STUFF((
        SELECT '
        USE [' + d.name + ']
        INSERT INTO #space (database_id, data_used_size, log_used_size)
        SELECT
              DB_ID()
            , SUM(CASE WHEN [type] = 0 THEN space_used END)
            , SUM(CASE WHEN [type] = 1 THEN space_used END)
        FROM (
            SELECT s.[type], space_used = SUM(FILEPROPERTY(s.name, ''SpaceUsed'') * 8. / 1024)
            FROM sys.database_files s
            GROUP BY s.[type]
        ) t;'
        FROM sys.databases d
        WHERE d.[state] = 0
        FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '')
    
    EXEC sys.sp_executesql @SQL
    
    SELECT
          d.database_id
        , d.name
        , d.state_desc
        , d.recovery_model_desc
        , t.total_size
        , t.data_size
        , s.data_used_size
        , t.log_size
        , s.log_used_size
    FROM (
        SELECT
              database_id
            , log_size = CAST(SUM(CASE WHEN [type] = 1 THEN size END) * 8. / 1024 AS DECIMAL(18,2))
            , data_size = CAST(SUM(CASE WHEN [type] = 0 THEN size END) * 8. / 1024 AS DECIMAL(18,2))
            , total_size = CAST(SUM(size) * 8. / 1024 AS DECIMAL(18,2))
        FROM sys.master_files
        GROUP BY database_id
    ) t
    JOIN sys.databases d ON d.database_id = t.database_id
    LEFT JOIN #space s ON d.database_id = s.database_id
    ORDER BY t.total_size DESC
    
    0 讨论(0)
  • 2021-01-29 19:06

    Not to steal your answer and adapt it for points or anything, but here is another factorization:

    select d.name, 
        sum(m0.size*8.0/1024) data_file_size_mb, 
        sum(m1.size*8.0/1024) log_file_size_mb 
    from sys.databases d
    inner join sys.master_files m0 on m0.database_id = d.database_id
    inner join sys.master_files m1 on m1.database_id = d.database_id
    where m0.type = 0 and m1.type = 1
    group by d.name, d.database_id
    order by d.database_id
    
    0 讨论(0)
  • 2021-01-29 19:07

    The following code worked for me very well.

        SELECT
            D.name As DbName,
            F.Name AS FullDbName,
            CASE WHEN F.type_desc='ROWS' THEN 'mdf' ELSE 'ldf' END AS FileType,
            F.physical_name AS PhysicalFile,
            CONVERT(DATE,D.create_date) AS CreationDate,
            F.state_desc AS OnlineStatus,
            CAST((F.size*8)/1024 AS VARCHAR(26)) + ' MB' AS FileSize_MB,
            CAST(F.size*8 AS VARCHAR(32)) + ' Bytes' AS FileSize_Bytes,
            CAST(CAST(ROUND((F.size*8)/(1024.0*1024.0),0) AS INT) AS VARCHAR(32)) + ' GB' AS FileSize_GB
    
        FROM 
            sys.master_files F
            INNER JOIN sys.databases D ON D.database_id = F.database_id
    
        ORDER BY
             D.name 
    
    0 讨论(0)
提交回复
热议问题