I am looking at this for a few minutes now and could not find a thread mentioning this.
I wanted to query currently used space and available space from a database on SQL Server.
The numbers I query should match the numbers I see, when I open the properties dialog on a database in SQL Server Management Studio, like this:
So I browsed a bit and found that I can query against sys.database_files
like so (from here):
SELECT
DB_NAME() AS DbName,
name AS FileName,
size/128.0 AS CurrentSizeMB,
size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0 AS FreeSpaceMB
FROM sys.database_files;
This gets me this result:
DbName FileName CurrentSizeMB FreeSpaceMB databasename databasename 17338.000000 16795.312500 databasename databasename_log 351.000000 331.234375
Trying to recognize my numbers from the previous dialog:
- OK, adding
17338
and351
gets me to17689
which is exactly the size the dialog lists - But for available space I cannot find it,
16795.312500
is off by almost half a MB
What can I do to get exactly the same number the properties dialog shows for Space Available?
Did I use the query/formulae in a wrong way or is this simply not possible at this point due to some rounding (converting pages/extents to MB…)?
Best Answer
Tracing the query used by ssms
One way to do this is by taking the profiler's 'tuning' template and then tracing your login
This query pops up
The query gets a
DbSize
andSpaceUsed
column from some DMV's.The
DbSize
columnA straightforward query, get the size where the file type
= 0 = Rows
= 2 = FILESTREAM
= 4 = Full-text
This query gets all file sizes except the log size.
I'm assuming this is added later as it is added to the total size in the properties pane.
The
SpaceUsed
columnA bit bigger, first get the sum of total pages from the
sys.allocation_units
DMV
And then add any filestream data to this number (if none then add
0
):The total query
Running the total query gives me:
Both the
DBSize
andSpaceUsed
column is the size in8KB
pages.If I transform the query to be divided by
/128.0
and subtractSpaceUsed
fromDBSize
like your queryI can replicate the properties view of the space available:
Still need to apply rounding with 2 digits after the comma
where your query gives me a lower amount on my database
Trying to find a why
Assumptions
We established that the
DBSize
column is the same for both queries, the difference should be between thetotal_pages
column from thesys.allocation_units
dmv and theFILEPROPERTY
functionon the
FILEPROPERTY
documentation:Source
Where
total_pages
=Source
Guess
We are getting the sum of all these allocation units. My guess would be that the difference is based on the file having more overhead pages not directly in the allocation units dmv, such as File header,
PFS
,GAM
,SGAM
,DCM
,BCM
.More on these here
Using
sp_spaceused
and@updateusage
to update the space usage info (DBCC UPDATEUSAGE
)The
sp_spaceused
procedure gives the same result as theSSMS
Properties window and thesys.allocation_units
query.But when running sp_spaceused with
@updateusage = 'true'
The values are updated:
The changed SSMS query that uses
sys.allocation_units
also shows this updated value:While the
FILEPROPERTY
query did not change!