查询数据库中表空间使用状况
/*-- ===================================================================
版本: SQL Server 2000, 2005
功能: 查询数据库中表空间使用状况
=================================================================== --*/
USE [要查询空间信息的库名]
SELECT
ServerName = CONVERT(sysname, SERVERPROPERTY('ServerName')),
DatabaseID = DB_ID(),
DatabaseName = DB_NAME(),
ObjectID = DATA.id,
Owner = USER_NAME(DATA.uid),
ObjectName = DATA.Name,
Reserved = CONVERT(decimal(15, 2), DATA.Reserved / SPER.PageSperMB),
DataSize = CONVERT(decimal(15, 2), (DATA.DataSize + DATA.[TextSize]) / SPER.PageSperMB),
IndexSize = CONVERT(decimal(15, 2), (DATA.IndexSize - DATA.DataSize - DATA.[TextSize]) /SPER.PageSperMB),
UnUsedSize = CONVERT(decimal(15, 2), (DATA.Reserved - DATA.IndexSize) / SPER.PageSperMB),
DATA.Rows
FROM(
SELECT
O.id, O.uid, O.name, O.xtype,
Reserved = ISNULL(SUM(CASE WHEN IX.indid IN (0, 1, 255) THEN CONVERT(dec(15), IX.reserved)ELSE 0 END), 0),
DataSize = ISNULL(SUM(CASE WHEN IX.indid < 2 THEN CONVERT(dec(15), IX.dpages) ELSE 0 END),0),
[TextSize] = ISNULL(SUM(CASE WHEN IX.indid = 255 THEN CONVERT(dec(15), IX.used) ELSE 0 END),0),
IndexSize = ISNULL(SUM(CASE WHEN IX.indid IN (0, 1, 255) THEN CONVERT(dec(15), IX.used) ELSE0 END), 0),
Rows = ISNULL(MAX(CASE WHEN IX.indid < 2 THEN IX.rows ELSE 0 END), 0)
FROM dbo.sysindexes IX WITH(NOLOCK)
INNER JOIN dbo.sysobjects O WITH(NOLOCK)
ON IX.id = O.id
WHERE O.xtype IN ( 'U')--, 'S')
AND O.status > =0
AND (IX.indid < 2 OR IX.indid = 255)
GROUP BY O.id, O.uid, O.name, O.xtype
)DATA
CROSS JOIN(
SELECT PageSperMB = 1048576. / low, low
FROM master.dbo.spt_values
WHERE number = 1
AND type = 'E'
)SPER
ORDER BY ObjectName, Owner
GO