Skip to content

Instantly share code, notes, and snippets.

@joshids
Created September 27, 2016 07:44
Show Gist options
  • Save joshids/5679948a8fb6333db86023ea1383bc55 to your computer and use it in GitHub Desktop.
Save joshids/5679948a8fb6333db86023ea1383bc55 to your computer and use it in GitHub Desktop.
Script to calculate table and index sizes for all databases that login has access to.
--sqlserver 2005 +
EXECUTE master.sys.sp_MSforeachdb
'USE [?];
select getdate() as snapdate,serverproperty(''MachineName'') svr,@@servicename sv, ''?'' _dbname, nomTable= object_name(p.object_id),p.partition_id,p.partition_number,
lignes = sum(
CASE
When (p.index_id < 2) and (a.type = 1) Then p.rows
Else 0
END
),
''memory (kB)'' = cast(ltrim(str(sum(a.total_pages)* 8192 / 1024.,15,0)) as float),
''data (kB)'' = ltrim(str(sum(
CASE
When a.type <> 1 Then a.used_pages
When p.index_id < 2 Then a.data_pages
Else 0
END
) * 8192 / 1024.,15,0)),
''indexes (kb)'' = ltrim(str((sum(a.used_pages)-sum(
CASE
When a.type <> 1 Then a.used_pages
When p.index_id < 2 Then a.data_pages
Else 0
END) )* 8192 / 1024.,15,0)),p.data_compression,
p.data_compression_desc
from sys.partitions p, sys.allocation_units a ,sys.sysobjects s
where p.partition_id = a.container_id
and p.object_id = s.id and s.type = ''U'' -- User table type (system tables exclusion)
group by p.object_id,p.partition_id,p.partition_number,p.data_compression,p.data_compression_desc
order by 3 desc'
;
@joshids
Copy link
Author

joshids commented Sep 27, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment