I wish this was actually a better way, but really it’s an idea for a better way to get at the data.
Something that has come up at every place I’ve worked is the desire to monitor growth on data and log files for databases. If you aren’t using some 3rd party monitoring software this is a pretty painful operation which forces us to jump back to cursor type operations where we have to gather information one database at a time (although this does not apply to transaction logs).
Gathering Log Space
To currently gather log space you would use DBCC SQLPERF(‘LOGSPACE’) which will give you size and used percentage for every log file on a server.
Dumping that information into a table allows you to manipulate the data to figure out KB/MB/GB usage (rather than just plain percent).
As an example (GatherLogSpace.sql):
Gathering Log Space
This is a reasonable amount of work, but what if you want to get space used in data files? Well that gets a whole lot more complicated.
Gathering Data File Space
In order to gather information on space used in data files you actually have to go to each and every database and run a query using sys.dm_db_file_space_usage.
As an example (GatheringDataFileSpace.sql):
Gathering the space available in data files
Strangely the sys.dm_db_file_space_usage DMV only contains the data file information. The log file data can only be obtained using the first process. This means that there is no single place that you can go to get file usage information for a database.
What about PowerShell?
PowerShell could give you the data, but it is still a cursor type operation, and you have to work through each database, drilling down to the log information, the filegroup and file inside each of those filegroups.
What we really need is a nice set based query that we could run in TSQL to give us all the information that is needed.
What I Would Like To See
Right now you could go and grab the size for each and every file for every database inside of SQL Server by querying sys.master_files:
There’s a whole lot more information in sys.master_files, including growth data, so you can know exactly how much each file will grow the next time it does.
Even though there’s this great data there is nothing to give us space used data. It would probably take too much overhead to keep that information up to date, but there’s no reason that we shouldn’t be able to easily get at the data when we want to.
Ideally we would see the existing DMV changed into a function, which would accept a database_id and file_id, and return the same data that it does now.
If that were the case it would be simple to run one query that would give us the size of each file, used space, free space, and how much the file would grow the next time an autogrowth hit.
This could quickly and easily give us all the data we want:
As a hope and desire to see this happen I’ve created a Microsoft Connect item – sys.dm_db_file_space_usage Should Be A Function
If you manage databases, and it’s something that you’d really like to have (who wouldn’t?) then please go and give it an upvote. There’s a chance it might make it into the product at some point, and that would make a DBAs life a little bit easier.