Memory usage – AumTechHub.Com http://aumtechhub.com Just share it!!! Mon, 09 May 2022 13:01:10 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 179596041 How to get table size in a database? http://aumtechhub.com/how-to-get-table-size-in-a-database/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-get-table-size-in-a-database Thu, 11 Nov 2021 04:54:04 +0000 https://aumtechhub.com/?p=280 This query will return size of each table in a sql database. This is a great way to monitor growth of a database and decide what restrictions might be needed to keep the database size from growing out of control. You may want to put a limit on file upload size on a table that might storing documents for example.

SELECT
t.NAME AS TableName,
p.rows AS TableRowCounts,
CONVERT(DECIMAL,SUM(a.total_pages)) * 8 / 1024 AS TotalSpaceMB,
CONVERT(DECIMAL,SUM(a.total_pages)) * 8 / 1024 /1024 AS TotalSpaceGB,
SUM(a.used_pages) * 8 / 1024 AS UsedSpaceMB ,
SUM(a.used_pages) * 8 / 1024 / 1024 AS UsedSpaceGB
FROM
DatabaseName.sys.tables t
INNER JOIN
DatabaseName.sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
DatabaseName.sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
DatabaseName.sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN
DatabaseName.sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
UsedSpaceGB DESC, t.Name

]]>
280