简体   繁体   English

使用T-SQL进行SQL Server数据库分析

[英]SQL Server Database analysis using T-SQL

I have a small application which has around 38 tables in the Database (SQL Server 2000/2005). 我有一个小应用程序,在数据库(SQL Server 2000/2005)中有大约38个表。 Now i would like to know how much data (in KB's/MB's not records) they are taking using T-SQL. 现在我想知道他们使用T-SQL获取了多少数据(KB / MB不是记录)。
Saying in better way i would like to do analysis of entire database. 用更好的方式说我想做整个数据库的分析。 Is that possible? 那可能吗? How ? 怎么样 ? Thanks All, 谢谢大家,

If you want to find the space of each table then you can use the following: 如果要查找每个表的空间,可以使用以下内容:

USE yourdbname
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"

For further reading check out Displaying the Size of your SQL Server Database's Tables 有关进一步阅读,请查看显示SQL Server数据库表的大小

for the whole database you can just run this 对于整个数据库,您可以运行它

exec sp_helpdb 'YourDatabaseName'

per table you can use this (2005+) 每张桌子你可以使用这个(2005+)

declare @PageSize float
select @PageSize=v.low/1024.0 from master.dbo.spt_values v 
where v.number=1 and v.type='E'

SELECT tbl.name,

ISNULL((select @PageSize * SUM(CASE WHEN a.type <> 1 THEN a.used_pages 
WHEN p.index_id < 2 THEN a.data_pages ELSE 0 END)

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

where i.object_id = tbl.object_id),0.0) AS [DataSpaceUsed_KB],

ISNULL((select @PageSize * SUM(a.used_pages - CASE WHEN a.type <> 1 
THEN a.used_pages      WHEN p.index_id < 2 THEN a.data_pages ELSE 0 END)

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

where i.object_id = tbl.object_id),0.0) AS [IndexSpaceUsed_KB]

FROM

sys.tables AS tbl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM