Home » Script to get database sizes in SQL Server

Script to get database sizes in SQL Server

by Vlad Drumea
3 comments

This post contains a script I’ve put together to query database sizes in SQL Server along with some additional info like file counts per file types and their sizes in GB.

Get all database sizes in a SQL Server instance

The following query retrieves the following information, one record per database:

  • Database – the name of the database
  • DatabaseState – the state of the database (duh)
  • DataFiles – number of data files (.mdf and .ndf)
  • DataFilesSizeGB – the total size of the data file(s) in GB
  • LogFIles – number of transaction log files (.ldf) – note: 99% of the time there is no valid reason to have more than one transaction log file per database.
  • LogFileSizeGB – the total size of the transaction log file(s) in GB
  • FILESTREAMContainers – the number of FILESTREAM containers – at the disk level these are actually directories
  • FSContainersSizeGB – the total size of the FILESTREAM container(s) in GB
  • DatabaseSizeGB – the total size of the database in GB

You might ask yourself “isn’t the internet full of scripts that do the same thing?”, and the answer is “yes, but there’s a catch”.
In this case the catch being that, although there are some great examples out there, the ones I’ve found rely solely on sys.master_files so they end up omitting FILESTREAM container sizes altogether.

Since FILESTREAM size information is not returned by the system-wide view sys.master_files, but by the database-level view sys.database_files, I had to use a temp table and populate it via a cursor with the FILESTREAM info for each database with such containers.

Output example


Note that I’m not excluding system databases from the output but, if needed, that can be done by adding WHERE d.[database_id] > 4 to the query.

Getting the size of a specific database

If needed, you can run this script in the context of a specific database to get the files and size info for that database alone.


Conclusion

The post is pretty much self-explanatory, I needed a script to query for SQL Server database size info in a certain way and without omitting FILESTREAM containers.

If you’ve found this post helpful, you might also like this post about getting table sizes in SQL Server.

You may also like

3 comments

Marc Ferrari May 3, 2024 - 10:52

Hello,

Thank you for the script…

It used to work but for whatever reason, I now get an error :

Msg 8115, Level 16, State 2, Line 41
Arithmetic overflow error converting expression to data type int.

Completion time: 2024-05-03T10:49:56.0646263+02:00

Any idea ?

Thank you

Marc

Reply
Vlad Drumea May 3, 2024 - 11:00

Hi Marc,
most likely the size column sums up to something that exceeds INT and it needs to be cast as BIGINT first.
So changing every occurrence of the size column to cast(sum as bigint) or f.size to cast(f.size as bigint) should sort it out.

Reply
Vlad Drumea May 7, 2024 - 22:07

Hi Marc,
I’ve updated both scripts, they should no longer throw that error on larger databases.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.