List data and log file sizes per database in SQL Server
Every data and log file of every database on the instance, with its size and growth settings, in one query.
The task
sys.master_files shows every database file on the instance, each database's data files and transaction log, with allocated size in 8 KB pages. sys.database_files shows the same for the current database only. Summing by volume and comparing with the free space that sys.dm_os_volume_stats reports is the basis for disk capacity planning.
The snippet
Written for SQL Server. Versions for PostgreSQL and MySQL are under Variations.
SELECT DB_NAME(database_id) AS database_name,
name AS logical_name,
type_desc,
CAST(size / 128.0 AS DECIMAL(12,1)) AS size_mb,
CASE max_size WHEN -1 THEN NULL ELSE CAST(max_size / 128.0 AS DECIMAL(12,1)) END AS max_size_mb,
CASE WHEN is_percent_growth = 1 THEN CONCAT(growth, ' %')
ELSE CONCAT(growth / 128, ' MB') END AS growth,
is_read_only,
physical_name
FROM sys.master_files
ORDER BY DB_NAME(database_id), type_desc, name;
仕組み
size, max_size and fixed growth are all in 8 KB pages, and 128 pages make a MB; dividing rather than multiplying by 8 first avoids an integer overflow on the 2 TB log cap. max_size = -1 means the file may grow until the disk is full, shown here as NULL; log files often show 268435456 pages, the 2 TB cap. growth is a percentage when is_percent_growth = 1, otherwise pages. type_desc separates ROWS (data) from LOG. Log size is not data size: a log much bigger than its data file usually means the full recovery model with log backups that are missing or rare. Small fixed growth steps on a busy database cause frequent growth events, each a pause; percentage growth makes the steps larger and larger as the file grows. The sizes here are what each file reserves on disk; FILEPROPERTY(name, 'SpaceUsed'), run inside each database, shows how much of it holds data.
Variations
PostgreSQL: size of every database
PostgreSQLSELECT datname,
pg_database_size(datname) AS size_bytes,
pg_size_pretty(pg_database_size(datname)) AS size
FROM pg_database
ORDER BY pg_database_size(datname) DESC;
MySQL: InnoDB tablespace file sizes
MySQLSELECT FILE_NAME, TABLESPACE_NAME, FILE_TYPE,
ROUND(TOTAL_EXTENTS * EXTENT_SIZE / 1024 / 1024, 1) AS size_mb
FROM information_schema.FILES
WHERE TABLESPACE_NAME IS NOT NULL
ORDER BY TOTAL_EXTENTS * EXTENT_SIZE DESC;
Questions that come up
A log file is 10x the data file. Is that broken?
Usually a configuration issue, not a fault. In the full or bulk-logged recovery model the log is only truncated by log backups, so missing log backups let it grow forever; log_reuse_wait_desc in sys.databases names what is holding it (LOG_BACKUP, ACTIVE_TRANSACTION, REPLICATION and so on). A single huge transaction or index rebuild also has to fit in the log. Fix the cause first, then shrink once with DBCC SHRINKFILE to a sensible size. Repeated shrink-and-grow cycles leave the log split into many small virtual log files, which slows recovery and log backups.
How do I forecast disk run-out from this query?
Get free space per volume from sys.dm_os_volume_stats(database_id, file_id), which works on Windows and Linux, then divide it by the monthly growth from the database-growth recipe. A database growing 5 GB a month on a volume with 40 GB free has about eight months. Remember that files with a size cap stop at max_size_mb regardless of free disk.
Keep reading
Related on SQLly
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.