Question
· Dec 8, 2021

Query to return table sizes

I've been accessing Cache tables from a developer/reporting side, but am now involved in a project to create a data warehouse for our application.  I'm trying to find a query I can use to return the sizes of all the tables in the database, so we can identify the largest tables and handle those individually.  Can someone give me a query I can run against our Cache database to return the sizes of all the tables from largest to smallest?  

Thanks for the help

Product version: Caché 2017.1
Discussion (8)1
Log in or sign up to continue

Here's a full example relating global size back to related classes/tables, expanding on @Vitaliy.Serdtsev's example:

select sdef.parent "Class", class.SqlSchemaName||'.'||class.SqlTableName "Table", ​LIST(sdef.Location) "Globals", SUM(s."Allocated MB") "Allocated MB", SUM(s."Used MB") "Used MB" from %SYS.GlobalQuery_Size('C:\InterSystems\IRIS20\mgr\user','','*D,*S,*I',0,0,1) s
join (
select parent,DataLocation as Location from %Dictionary.StorageDefinition where not parent %STARTSWITH '%'
union all
select parent,StreamLocation from %Dictionary.StorageDefinition where not parent %STARTSWITH '%'
union all
select parent,IndexLocation from %Dictionary.StorageDefinition where not parent %STARTSWITH '%'
) sdef
on sdef.Location = '^'||s.Name
join %Dictionary.CompiledClass class on class.Name = sdef.parent
group by sdef.parent

Note that if you use inheritance with persistent classes sharing an extent this'll be a little bit off (since the same global is used for multiple classes and will be double counted).