Query to fetch table sizes in the Analytics application

Query to fetch table sizes in the Analytics application

Idea
Requirement
To determine the sizes of the tables available under our analytics application, we can execute the following query in the DB Query tool and retrieve the details.

Notes
Query

Navigate to Application Settings --> Troubleshoot --> DB Query Tool --> Execute the below query

SELECT db.id as "Workspace Id", db.name as "Workspace Name", db.status as "Workspace status", obj.id as "Table Obj Id",tb.id as "Table id", obj.displayname as "Table Display name",td.table_name as "Internal Table Name", obj.status as "Table status",obj.type as "Type", iammail.email_id as "User", round(CAST(float8 (((sum(pg_table_size('"' || ist.table_schema || '"."' || ist.table_name || '"'))) / 1024) / 1024) AS numeric), 2) AS "Size in MB", pgut.n_live_tup as "Row Count" FROM zdbdatabase db JOIN zdbdbobject obj ON db.id = obj.dbid JOIN zdbtable tb ON obj.id = tb.objid JOIN tabledetails td ON tb.id = td.table_id LEFT JOIN information_schema.tables ist ON lower(td.table_name) = ist.table_name RIGHT JOIN iamuser ON db.owner_zuid = iamuser.zuid RIGHT JOIN iamuseremail iammail ON iammail.user_auto_id = iamuser.user_auto_id left join pg_stat_user_tables pgut on lower(td.table_name)=pgut.relname WHERE ist.table_schema = 'public' AND db.owner_zuid > 107  and pgut.n_live_tup is not null group by 1,4,5,7,10,12 ORDER BY 1 DESC;

Info
Output


Warning
Note
If you wish to sort the table size data based on size, you can use the condition below.

Example:
Descending --> ORDER BY 11 DESC;