SQL Server 2008: How to query all databases sizes?

后端 未结 15 950
伪装坚强ぢ
伪装坚强ぢ 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:54

    I recently came across this page looking for something like this. Just in case someone comes across this and is interested in only the core user databases, you can use something like this which excludes Master, msdb...

    SELECT
    DB_NAME(db.database_id) DatabaseName,
    (CAST(mfrows.RowSize AS FLOAT)*8)/1024 RowSizeMB,
    (CAST(mflog.LogSize AS FLOAT)*8)/1024 LogSizeMB
    FROM sys.databases db
    LEFT JOIN (SELECT database_id, SUM(size) RowSize FROM sys.master_files WHERE type = 0 GROUP BY database_id, type) mfrows ON mfrows.database_id = db.database_id
    LEFT JOIN (SELECT database_id, SUM(size) LogSize FROM sys.master_files WHERE type = 1 GROUP BY database_id, type) mflog ON mflog.database_id = db.database_id
    where DB_NAME(db.database_id) not like 'master'
    and DB_NAME(db.database_id) not like 'msdb'
    and DB_NAME(db.database_id) not like 'model'
    and DB_NAME(db.database_id) not like 'tempdb'
    and DB_NAME(db.database_id) not like 'Northwind'
    and DB_NAME(db.database_id) not like 'ReportServer'
    order by DB_NAME(db.database_id)
    

提交回复
热议问题