SqlServer 数据库占用磁盘空间过大的解决方式

今天突然发现数据库占用空间400多个G,但是查看数据库表空间及索引大小,一共加起来30多个G。
为了解决此问题采取SqlServer数据库收缩功能,数据库的收缩分为自动收缩和手动收缩。本次解决问题采用了自动收缩的方式进行。自动收缩步骤如图:
SqlServer 数据库占用磁盘空间过大的解决方式_第1张图片
修改完成之后,点击确认。数据库会每隔半小时收缩空间大于25%的内容。

参考Sql脚本如下:
— 查看数据库大小
SELECT DB_NAME(database_id) AS [Database Name],
[Name] AS [Logical Name],
[Physical_Name] AS [Physical Name],
((size * 8) / 1024) AS [Size(MB)]
FROM sys.master_files
ORDER BY [Size(MB)] DESC

— 查看数据库索引大小
USE ldtcasedossier
GO
SELECT tn.[name] AS [Table name], ix.[name] AS [Index name],
SUM(sz.[used_page_count]) * 8 AS [Index size (KB)]
FROM sys.dm_db_partition_stats AS sz
INNER JOIN sys.indexes AS ix ON sz.[object_id] = ix.[object_id]
AND sz.[index_id] = ix.[index_id]
INNER JOIN sys.tables tn ON tn.OBJECT_ID = ix.object_id
GROUP BY tn.[name], ix.[name]
ORDER BY tn.[name]

你可能感兴趣的:(Sql脚本,数据库,sqlserver,sql)