Advertisement
ffarias

size of all tables in database

Jul 22nd, 2019
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.04 KB | None | 0 0
  1. --size of all tables in database
  2.  
  3. SELECT s.name AS SCHEMA_NAME,
  4.        t.name AS OBJ_NAME,
  5.        t.type_desc AS OBJ_TYPE,
  6.        i.name AS indexName,
  7.        SUM(p.rows) AS RowCounts,
  8.        SUM(a.total_pages) AS TotalPages,
  9.        SUM(a.used_pages) AS UsedPages,
  10.        SUM(a.data_pages) AS DataPages,
  11.        (SUM(a.total_pages) * 8) / 1024 AS TotalSpaceMB,
  12.        (SUM(a.used_pages) * 8) / 1024 AS UsedSpaceMB,
  13.        (SUM(a.data_pages) * 8) / 1024 AS DataSpaceMB
  14. FROM sys.objects t
  15.     INNER JOIN sys.schemas s
  16.         ON t.schema_id = s.schema_id
  17.     INNER JOIN sys.indexes i
  18.         ON t.object_id = i.object_id
  19.     INNER JOIN sys.partitions p
  20.         ON i.object_id = p.object_id
  21.            AND i.index_id = p.index_id
  22.     INNER JOIN sys.allocation_units a
  23.         ON p.partition_id = a.container_id
  24. WHERE t.name NOT LIKE 'dt%'
  25.       AND i.object_id > 255
  26.       AND i.index_id <= 1
  27. GROUP BY s.name,
  28.          t.name,
  29.          t.type_desc,
  30.          i.object_id,
  31.          i.index_id,
  32.          i.name
  33. ORDER BY SUM(a.total_pages) DESC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement