--To check the database size:
SELECT
dbs.NAME as DatabaseName,
CAST((SUM(mFiles.SIZE) * 8 / 1024.00 / 1024.00) AS NUMERIC(18,2))
AS [DBSizeInGB]
FROM MASTER.SYS.MASTER_FILES mFiles INNER JOIN MASTER.SYS.DATABASES dbs
ON dbs.DATABASE_ID = mFiles.DATABASE_ID
WHERE dbs.DATABASE_ID > 4
and dbs.name = 'MyDB'
GROUP BY dbs.NAME
-- To Check Table Sizes in MyDB:
SELECT
s.name + '.' + t.Name AS [Table Name],
part.rows AS [TotalRowsInTable],
CAST((SUM( DISTINCT au.Total_pages) * 8 ) / 1024.000 / 1024.000 AS NUMERIC(18, 3))
AS [TableTotalSpaceInGB]
FROM
SYS.Tables t INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN SYS.Indexes idx ON t.Object_id = idx.Object_id
INNER JOIN SYS.Partitions part ON idx.Object_id = part.Object_id
AND idx.Index_id = part.Index_id
INNER JOIN SYS.Allocation_units au ON part.Partition_id = au.Container_id
INNER JOIN SYS.Filegroups fGrp ON idx.Data_space_id = fGrp.Data_space_id
INNER JOIN SYS.Database_files Df ON Df.Data_space_id = fGrp.Data_space_id
WHERE t.Is_ms_shipped = 0 AND idx.Object_id > 255
GROUP BY t.Name, s.name, part.rows
ORDER BY [TableTotalSpaceInGB] DESC
--To check the Indiex sizes in MyDB:
SELECT
OBJECT_SCHEMA_NAME(i.OBJECT_ID) AS SchemaName,
OBJECT_NAME(i.OBJECT_ID) AS TableName,
i.name AS IndexName,
i.index_id AS IndexID,
CAST((SUM( DISTINCT a.used_pages) * 8 ) / 1024.000 / 1024.000 AS NUMERIC(18, 3)) 'IndexTotalSpaceInGB'
--8 * SUM(a.used_pages) AS 'IndexTotalSpaceInGB'
FROM sys.indexes AS i
JOIN sys.partitions AS p ON p.OBJECT_ID = i.OBJECT_ID AND p.index_id = i.index_id
JOIN sys.allocation_units AS a ON a.container_id = p.partition_id
GROUP BY i.OBJECT_ID,i.index_id,i.name
ORDER BY IndexTotalSpaceInGB desc
SELECT
dbs.NAME as DatabaseName,
CAST((SUM(mFiles.SIZE) * 8 / 1024.00 / 1024.00) AS NUMERIC(18,2))
AS [DBSizeInGB]
FROM MASTER.SYS.MASTER_FILES mFiles INNER JOIN MASTER.SYS.DATABASES dbs
ON dbs.DATABASE_ID = mFiles.DATABASE_ID
WHERE dbs.DATABASE_ID > 4
and dbs.name = 'MyDB'
GROUP BY dbs.NAME
-- To Check Table Sizes in MyDB:
SELECT
s.name + '.' + t.Name AS [Table Name],
part.rows AS [TotalRowsInTable],
CAST((SUM( DISTINCT au.Total_pages) * 8 ) / 1024.000 / 1024.000 AS NUMERIC(18, 3))
AS [TableTotalSpaceInGB]
FROM
SYS.Tables t INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN SYS.Indexes idx ON t.Object_id = idx.Object_id
INNER JOIN SYS.Partitions part ON idx.Object_id = part.Object_id
AND idx.Index_id = part.Index_id
INNER JOIN SYS.Allocation_units au ON part.Partition_id = au.Container_id
INNER JOIN SYS.Filegroups fGrp ON idx.Data_space_id = fGrp.Data_space_id
INNER JOIN SYS.Database_files Df ON Df.Data_space_id = fGrp.Data_space_id
WHERE t.Is_ms_shipped = 0 AND idx.Object_id > 255
GROUP BY t.Name, s.name, part.rows
ORDER BY [TableTotalSpaceInGB] DESC
--To check the Indiex sizes in MyDB:
SELECT
OBJECT_SCHEMA_NAME(i.OBJECT_ID) AS SchemaName,
OBJECT_NAME(i.OBJECT_ID) AS TableName,
i.name AS IndexName,
i.index_id AS IndexID,
CAST((SUM( DISTINCT a.used_pages) * 8 ) / 1024.000 / 1024.000 AS NUMERIC(18, 3)) 'IndexTotalSpaceInGB'
--8 * SUM(a.used_pages) AS 'IndexTotalSpaceInGB'
FROM sys.indexes AS i
JOIN sys.partitions AS p ON p.OBJECT_ID = i.OBJECT_ID AND p.index_id = i.index_id
JOIN sys.allocation_units AS a ON a.container_id = p.partition_id
GROUP BY i.OBJECT_ID,i.index_id,i.name
ORDER BY IndexTotalSpaceInGB desc
No comments:
Post a Comment