怎么查询数据库中表占用空间大小??
数据库的MDF文件占用30多个G的空间,但是日志文件却只有几十M,收缩数据库后都没办法,请问有办法查找到数据库中哪个表的占用空间大小吗???
[解决办法]
这是2008的,前50个大表
SELECT top 50 OBJ.name AS ObjName
,STAT.row_count AS [RowCount]
,sum(STAT.used_page_count )* 8/1024 AS UsedSizeMB
FROM sys.partitions AS PART
INNER JOIN sys.dm_db_partition_stats AS STAT
ON PART.partition_id = STAT.partition_id
AND PART.partition_number = STAT.partition_number
INNER JOIN sys.objects AS OBJ
ON STAT.object_id = OBJ.object_id
INNER JOIN sys.schemas AS SCH
ON OBJ.schema_id = SCH.schema_id
INNER JOIN sys.indexes AS INDX
ON STAT.object_id = INDX.object_id
AND STAT.index_id = INDX.index_id
group by OBJ.name ,STAT.row_count
ORDER BY UsedSizeMB desc
CREATE TABLE test1 (NAME VARCHAR(30),[ROWS] bigINT,reserved VARCHAR(30),[data] VARCHAR(30),index_size VARCHAR(30),unused VARCHAR(30))
go
INSERT INTO test1(name,[rows],reserved,data,index_size,unused)
EXECUTE sp_msforeachtable 'sp_spaceused [?] '
FETCH NEXT FROM c1 INTO @table_name
END
CLOSE c1
DEALLOCATE c1
SELECT t1.*
,t2.schemaname
FROM #temp_Table t1
INNER JOIN @tab1 t2 ON (t1.tablename = t2.tablename )
ORDER BY schemaname,t1.tablename;
DROP TABLE #temp_Table