Wednesday, August 8, 2012

Sp_spaceused


  declare @Space TABLE  (
   tablename    sysname,
   [rows]       bigint,
   reserved_S   varchar(32),
   data_S       varchar(32),
   index_size_S varchar(32),
   unused_S     varchar(32)
   )


  INSERT @Space(tablename,[rows],reserved_s,data_s,index_size_s,unused_s)
  EXEC sp_msforeachtable 'exec sp_spaceused [?]'


select
DB_NAME() as DatabaseName,
tablename    ,
   [rows]       ,
   type_desc,
   cast(replace(reserved_S, 'KB','') as bigint)/1024 as reserved_MB,
    cast(replace(data_S, 'KB', '') as bigint)/1024 as datasize_MB ,
    cast(replace(index_size_S, 'KB', '') as bigint)/1024 as index_size_MB  ,
    cast(replace(unused_S, 'KB', '') as bigint)/1024 as unused_MB
from @Space spa
inner join sys.indexes idx on idx.[object_id]=object_id(spa.tablename) and index_id in (0,1)
where (cast(replace(reserved_S, 'KB', '') as bigint)/1024) > 50000
order by unused_MB desc

No comments: