-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPACE_USED.sql
53 lines (47 loc) · 2.16 KB
/
SPACE_USED.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
42
43
44
45
46
47
48
49
50
51
52
53
--системная процедура
exec sp_spaceused --если запускать без параметров, то считает данные по всей БД
--общий объём занятого места объектами в БД
SELECT DB_NAME() as DatabaseName
, SUM(a.total_pages) * 8 AS TotalSpaceKB
, SUM(a.used_pages) * 8 AS UsedSpaceKB
, (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
, CAST(SUM(a.total_pages) * 8 / 1024.00 as decimal(10, 2)) AS TotalSpaceMB
, CAST(SUM(a.used_pages) * 8 / 1024.00 as decimal(10, 2)) AS UsedSpaceMB
, CAST((SUM(a.total_pages) - SUM(a.used_pages)) * 8 / 1024.00 as decimal(10, 2)) AS UnusedSpaceMB
FROM sys.all_objects AS o
INNER JOIN sys.schemas AS s
ON s.[schema_id] = o.[schema_id]
INNER JOIN sys.indexes AS i
ON i.[object_id] = o.[object_id]
INNER JOIN sys.partitions AS p
ON p.[object_id] = i.[object_id]
AND p.index_id = i.index_id
INNER JOIN sys.allocation_units AS a
ON a.container_id = p.[partition_id]
WHERE o.is_ms_shipped = 0 --убираем из выборки системные объекты (таблицы)
;
--список объектов и занятое ими место в KB и MB
SELECT s.[name] + '.' + o.[name] AS ObjectName
, p.[rows] AS RowCounts
, SUM(a.total_pages) * 8 AS TotalSpaceKB
, CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB
, SUM(a.used_pages) * 8 AS UsedSpaceKB
, CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB
, (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
, CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
FROM sys.all_objects AS o
INNER JOIN sys.schemas AS s
ON s.[schema_id] = o.[schema_id]
INNER JOIN sys.indexes AS i
ON i.[object_id] = o.[object_id]
INNER JOIN sys.partitions AS p
ON p.[object_id] = i.[object_id]
AND p.index_id = i.index_id
INNER JOIN sys.allocation_units AS a
ON a.container_id = p.[partition_id]
WHERE o.is_ms_shipped = 0 --убираем из выборки системные объекты (таблицы)
GROUP BY o.[name]
, s.[name]
, p.[rows]
ORDER BY o.[name]
;