{"id":296,"date":"2017-05-25T09:03:36","date_gmt":"2017-05-25T13:03:36","guid":{"rendered":"http:\/\/techref.camellarry.com\/?p=296"},"modified":"2017-05-25T09:03:36","modified_gmt":"2017-05-25T13:03:36","slug":"sql-database-table-sizes","status":"publish","type":"post","link":"http:\/\/techref.camellarry.com\/?p=296","title":{"rendered":"SQL Database Table Sizes"},"content":{"rendered":"<p>The following SQL command lists each table in a database and shows total row counts and disk spaced used.<\/p>\n<pre><span style=\"color: #0000ff;\">SELECT <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 t.NAME AS TableName,<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 s.Name AS SchemaName,<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 p.rows AS RowCounts,<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 SUM(a.total_pages) * 8 AS TotalSpaceKB, <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 SUM(a.used_pages) * 8 AS UsedSpaceKB, <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB<\/span>\r\n<span style=\"color: #0000ff;\">FROM <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 sys.tables t<\/span>\r\n<span style=\"color: #0000ff;\">INNER JOIN <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 sys.indexes i ON t.OBJECT_ID = i.object_id<\/span>\r\n<span style=\"color: #0000ff;\">INNER JOIN <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id<\/span>\r\n<span style=\"color: #0000ff;\">INNER JOIN <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 sys.allocation_units a ON p.partition_id = a.container_id<\/span>\r\n<span style=\"color: #0000ff;\">LEFT OUTER JOIN <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 sys.schemas s ON t.schema_id = s.schema_id<\/span>\r\n<span style=\"color: #0000ff;\">WHERE <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 t.NAME NOT LIKE 'dt%' <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 AND t.is_ms_shipped = 0<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 AND i.OBJECT_ID &gt; 255 <\/span>\r\n<span style=\"color: #0000ff;\">GROUP BY <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 t.Name, s.Name, p.Rows<\/span>\r\n<span style=\"color: #0000ff;\">ORDER BY <\/span>\r\n<span style=\"color: #0000ff;\">\u00a0 t.Name<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following SQL command lists each table in a database and shows total row counts and disk spaced used. SELECT \u00a0 t.NAME AS TableName, \u00a0 s.Name AS SchemaName, \u00a0 p.rows AS RowCounts, \u00a0 SUM(a.total_pages) * 8 AS TotalSpaceKB, \u00a0 SUM(a.used_pages) * 8 AS UsedSpaceKB, \u00a0 (SUM(a.total_pages) &#8211; SUM(a.used_pages)) * 8 AS UnusedSpaceKB FROM \u00a0 sys.tables [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[8],"tags":[94,70,72],"class_list":["post-296","post","type-post","status-publish","format-standard","hentry","category-sql","tag-database","tag-sql","tag-syntax"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9t3oE-4M","_links":{"self":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=296"}],"version-history":[{"count":1,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/296\/revisions"}],"predecessor-version":[{"id":297,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/296\/revisions\/297"}],"wp:attachment":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}