-
Notifications
You must be signed in to change notification settings - Fork 1
/
tablesizes.sql
41 lines (41 loc) · 1.92 KB
/
tablesizes.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
SELECT Database_ID = DB_ID()
, Database_Name = DB_NAME()
, Schema_Name = a3.name
, TableName = a2.name
, TableSize_MB = (a1.reserved + ISNULL(a4.reserved,0)) / 128
, RowCounts = a1.rows
, DataSize_MB = a1.data / 128
, IndexSize_MB = (CASE WHEN (a1.used + ISNULL(a4.used,0)) > a1.data
THEN (a1.used + ISNULL(a4.used,0)) - a1.data
ELSE 0
END) /128
, Free_MB = (CASE WHEN (a1.reserved + ISNULL(a4.reserved,0)) > a1.used
THEN (a1.reserved + ISNULL(a4.reserved,0)) - a1.used
ELSE 0
END) / 128
FROM (SELECT ps.object_id
, [rows] = SUM(CASE
WHEN (ps.index_id < 2) THEN row_count
ELSE 0
END)
, reserved = SUM(ps.reserved_page_count)
, data = SUM(CASE
WHEN (ps.index_id < 2)
THEN (ps.in_row_data_page_count + ps.lob_used_page_count + ps.row_overflow_used_page_count)
ELSE (ps.lob_used_page_count + ps.row_overflow_used_page_count)
END)
, used = SUM (ps.used_page_count)
FROM sys.dm_db_partition_stats ps
GROUP BY ps.object_id) AS a1
INNER JOIN sys.all_objects a2 ON a1.object_id = a2.object_id
INNER JOIN sys.schemas a3 ON a2.schema_id = a3.schema_id
LEFT JOIN (SELECT it.parent_id
, reserved = SUM(ps.reserved_page_count)
, used = SUM(ps.used_page_count)
FROM sys.dm_db_partition_stats ps
INNER JOIN sys.internal_tables it ON it.object_id = ps.object_id
WHERE it.internal_type IN (202,204)
GROUP BY it.parent_id) AS a4 ON a4.parent_id = a1.object_id
WHERE a2.type <> 'S' and a2.type <> 'IT'
--AND a2.name IN ('spt_values')
ORDER BY a1.reserved desc