Database size reports differently than the sum of all the tables in SQL Server -


i trying determine if sql server database healthy.

i ran couple of commands check size , shocked @ differences reported between sum of table sizes , database size.

i wondering why there large size difference.

exec sp_spaceused @updateusage = n'true';  database_name | database_size | unallocated space fleetequip    |1357.00 mb     |0.20 mb 

and

exec sp_msforeachtable   @command1="exec sp_spaceused '?'" 

(way formatting include tables - html table nice)

name           | rows   | reserved(kb) | data(kb) | index_size(kb) | unused(kb) equipmentstate | 131921 | 40648        | 40608    | 8              | 32 

the sum of tables comes 45768 kb

you can @ definition of sp_spaceused exec sp_helptext 'sp_spaceused'

though prefer result format returned following actually:

select object_definition(object_id('sp_spaceused')) [processing-instruction(x)] xml path

can try below (based on aggregate query contains) , see discrepancy lies?

select object_name(p.object_id),  reservedpages = sum(a.total_pages),     usedpages = sum(a.used_pages),     pages = sum(             case                 -- xml-index , ft-index internal tables not considered "data", part of "index_size"                 when it.internal_type in (202,204,211,212,213,214,215,216) 0                 when a.type <> 1 a.used_pages                 when p.index_id < 2 a.data_pages                 else 0             end         ) sys.partitions p join sys.allocation_units on p.partition_id = a.container_id     left join sys.internal_tables on p.object_id = it.object_id group p.object_id rollup 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -