{"id":847,"date":"2021-04-19T13:58:41","date_gmt":"2021-04-19T13:58:41","guid":{"rendered":"https:\/\/datarebus.com\/?p=847"},"modified":"2021-06-09T14:46:26","modified_gmt":"2021-06-09T14:46:26","slug":"query-to-list-size-of-the-table-with-row-counts","status":"publish","type":"post","link":"https:\/\/datarebus.com\/index.php\/2021\/04\/19\/query-to-list-size-of-the-table-with-row-counts\/","title":{"rendered":"Query to List Size of the Table with Row Counts"},"content":{"rendered":"\n<p>This query helps you identify your tables and calculate size\/rowcount ratio easily:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><code>USE [DatabaseName] -- replace your <\/code><\/p><p><code>GO<\/code><\/p><p>SELECT s.Name AS SchemaName,t.Name AS TableName,p.rows AS RowCounts, CAST(ROUND((SUM(a.used_pages) \/ 128.00), 2) AS NUMERIC(36, 2)) AS Used_MB,<\/p><p>CAST(ROUND((SUM(a.total_pages) &#8211; SUM(a.used_pages)) \/ 128.00, 2) AS NUMERIC(36, 2)) AS Unused_MB,CAST(ROUND((SUM(a.total_pages) \/ 128.00), 2) AS<\/p><p>NUMERIC(36, 2)) ASTotal_MB FROM sys.tables t INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id INNER JOIN sys.partitions p ON i.object_id =p.OBJECT_ID AND i.index_id = p.index_id INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id INNER JOIN sys.schemas s ON t.schema_id = s.schema_id<\/p><p>GROUP BY t.Name, s.Name, p.Rows<\/p><p>ORDER BY s.Name, t.Name<\/p><p>GO<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This query helps you identify your tables and calculate size\/rowcount ratio easily: USE [DatabaseName] &#8212; replace your GO SELECT s.Name AS SchemaName,t.Name AS TableName,p.rows AS RowCounts, CAST(ROUND((SUM(a.used_pages) \/ 128.00), 2) AS NUMERIC(36, 2)) AS Used_MB, CAST(ROUND((SUM(a.total_pages) &#8211; SUM(a.used_pages)) \/ 128.00, 2) AS NUMERIC(36, 2)) AS Unused_MB,CAST(ROUND((SUM(a.total_pages) \/ 128.00), 2) AS NUMERIC(36, 2)) ASTotal_MB FROM sys.tables [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":845,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,16],"tags":[],"_links":{"self":[{"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/posts\/847"}],"collection":[{"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/comments?post=847"}],"version-history":[{"count":2,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/posts\/847\/revisions"}],"predecessor-version":[{"id":1029,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/posts\/847\/revisions\/1029"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/media\/845"}],"wp:attachment":[{"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/media?parent=847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/categories?post=847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/tags?post=847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}