![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
--查数据库中多数表占用的空间
CREATE TABLE TMP
(name varchar(50),
rows int,
reserved varchar(50),
data varchar(50),
index_size varchar(50),
unused varchar(50))
GO
INSERT INTO tmp (name,rows,reserved,data,index_size,unused)
EXEC sp_msforeachTable @Command1="sp_spaceused '?'"
SELECT *,CONVERT(int, replace(data,' KB','')) as tt FROM tmp ORDER BY tt DESC
DROP TABLE tmp
GO
-- 收缩可让数据库大小减小很多特别是MDF文件
ALTER DATABASE DBname
Set RECOVERY SIMPLE
Go
DBCC SHRINKDATABASE (DBname)
Go
ALTER DATABASE DBname
Set RECOVERY FULL
Go