在mssql2005中,当我想以MB为单位获取表的大小时,我使用EXEC sp_spaceused'table'。
有没有办法在SQL Azure中使用某些查询或API来获取特定表所使用的空间?
来自Ryan Dunn http://dunnry.com/blog/CalculatingTheSizeOfYourSQLAzureDatabase.aspx
select
sum(reserved_page_count) * 8.0 / 1024 [SizeInMB]
from
sys.dm_db_partition_stats
GO
select
sys.objects.name, sum(reserved_page_count) * 8.0 / 1024 [SizeInMB]
from
sys.dm_db_partition_stats, sys.objects
where
sys.dm_db_partition_stats.object_id = sys.objects.object_id
group by sys.objects.name
order by sum(reserved_page_count) DESC
第一个将以MB为单位给出数据库的大小,第二个将以相同的方式为您提供数据库的大小,但是对于数据库中按大到小排序的每个对象,将其分解出来。
这是一个查询,它将按表格为您提供总行数,行数和每行字节数:
select
o.name,
max(s.row_count) AS 'Rows',
sum(s.reserved_page_count) * 8.0 / (1024 * 1024) as 'GB',
(8 * 1024 * sum(s.reserved_page_count)) / (max(s.row_count)) as 'Bytes/Row'
from sys.dm_db_partition_stats s, sys.objects o
where o.object_id = s.object_id
group by o.name
having max(s.row_count) > 0
order by GB desc
这是一个与上面相同的查询,但按索引分解:
select
o.Name,
i.Name,
max(s.row_count) AS 'Rows',
sum(s.reserved_page_count) * 8.0 / (1024 * 1024) as 'GB',
(8 * 1024* sum(s.reserved_page_count)) / max(s.row_count) as 'Bytes/Row'
from
sys.dm_db_partition_stats s,
sys.indexes i,
sys.objects o
where
s.object_id = i.object_id
and s.index_id = i.index_id
and s.index_id >0
and i.object_id = o.object_id
group by i.Name, o.Name
having SUM(s.row_count) > 0
order by GB desc
通过这种方式,您可以获得更大的优势:
SELECT sys.objects.name,
SUM(row_count) AS 'Row Count',
SUM(reserved_page_count) * 8.0 / 1024 AS 'Table Size (MB)'
FROM sys.dm_db_partition_stats, sys.objects
WHERE sys.dm_db_partition_stats.object_id = sys.objects.object_id
GROUP BY sys.objects.name
ORDER BY [Table Size (MB)] DESC