Im using query to find out database file size. I'm using system view and DMV, because i do want to find out both- actual file size and theoretical (in case of sparse files) file size.
Select
DB_NAME(mf.database_id) AS [Database Name],
mf.Name,
mf.physical_name PhysicalName,
cast(mf.size as bigint) * 8192 mfSize_bytes,
fs.size_on_disk_bytes fsSize_bytes
From sys.master_files mf
Left Join sys.dm_io_virtual_file_stats(DEFAULT, DEFAULT) fs
On mf.database_id = fs.database_id and mf.file_id = fs.file_id
Order By DB_NAME(mf.database_id)
I am confused- converted both values to "bytes", comparing them. But on all instances i checked- temdb file size (from sys.master_files) is smaller than file size on disk (from sys.dm_io_virtual_file_stats).
In all other cases if there is difference, then that is correct (size on disk is actually smaller than file size- because they are sparse files).
What is the reason for this difference?
UPDATE: When i am querying
Select is_sparse, * From sys.database_files
On SQL Server 2005 database snapshot, then column is_sparse = 0 (because it is showing properties of the files from original database, not snapshot file. So- is there bug in documentation?).
sys.dm_io_virtual_file_stats- which seems opposite to what you are seeing. I believe the latter if we're talking about actual size on disk rather than space used within the file. – Aaron Bertrand Jun 20 '12 at 12:59sys.master_files. You should be using a non-database-specific catalog view for this, because the one in the snapshot is actually the view in the source database (that view doesn't actually exist in the snapshot, right?). This is kind of the whole point of a snapshot, it only contains the snapshot image of things that have changed, but they will always be pre-change, so even if you change properties of the source database file (there aren't many you can make while a snapshot exists), the snapshot will always sayis_sparse = 0. – Aaron Bertrand Jun 20 '12 at 13:47