SQL 服务器 - 删除了 table 中的所有行(未截断)但 space 在收缩后没有减少
SQL Server - Deleted all rows in table (not truncate) but space is not decreasing after shrink
我正在对我们的 UAT 环境进行一些测试,所以我删除了一个大 table 中的所有行。然后我 运行 缩小了受影响的文件名。
但是,我仍然看到 table (60gb) 占用了原始大小,即使有 0 行。进一步查看,有一个 NULL 索引(我认为这意味着非索引,因此使用 PK)为此 table 占用了 30gb 和 30gb 的 "free space"。
如何将索引 space 和 "Free space" 都恢复为 0gb?
谢谢!
艾伦
右键单击 properties
上的 db.click,然后 files
。
注意 initial size
日志文件。
如果您的 table 是一个堆,那么当行被删除时 space 不会被回收。您必须创建一个聚集索引来回收 space(然后删除聚集索引以在末尾有一个堆)
create table dbo.myheap
(
id int identity,
col char(500) not null
);
go
insert into dbo.myheap(col)
select top (10000) a.name
from master.dbo.spt_values as a
cross join master.dbo.spt_values as b;
go
exec sp_spaceused 'dbo.myheap' --myheap 10000 5384 KB 5336 KB 8 KB 40 KB
go
--delete all rows
delete dbo.myheap;
go
--space is not freed
exec sp_spaceused 'dbo.myheap' --myheap 0 5384 KB 5336 KB 8 KB 40 KB
go
--from heap to clustered
create clustered index clxheaptocluster on dbo.myheap(id);
go
exec sp_spaceused 'dbo.myheap' --myheap 0 0 KB 0 KB 0 KB 0 KB
go
--cleanup
drop table dbo.myheap
go
对于聚簇tables,重建聚簇索引(或ALL):
ALTER INDEX ALL /*clusteredindexname*/ ON dbo.myclusteredtable REBUILD;
我正在对我们的 UAT 环境进行一些测试,所以我删除了一个大 table 中的所有行。然后我 运行 缩小了受影响的文件名。
但是,我仍然看到 table (60gb) 占用了原始大小,即使有 0 行。进一步查看,有一个 NULL 索引(我认为这意味着非索引,因此使用 PK)为此 table 占用了 30gb 和 30gb 的 "free space"。
如何将索引 space 和 "Free space" 都恢复为 0gb?
谢谢! 艾伦
右键单击 properties
上的 db.click,然后 files
。
注意 initial size
日志文件。
如果您的 table 是一个堆,那么当行被删除时 space 不会被回收。您必须创建一个聚集索引来回收 space(然后删除聚集索引以在末尾有一个堆)
create table dbo.myheap
(
id int identity,
col char(500) not null
);
go
insert into dbo.myheap(col)
select top (10000) a.name
from master.dbo.spt_values as a
cross join master.dbo.spt_values as b;
go
exec sp_spaceused 'dbo.myheap' --myheap 10000 5384 KB 5336 KB 8 KB 40 KB
go
--delete all rows
delete dbo.myheap;
go
--space is not freed
exec sp_spaceused 'dbo.myheap' --myheap 0 5384 KB 5336 KB 8 KB 40 KB
go
--from heap to clustered
create clustered index clxheaptocluster on dbo.myheap(id);
go
exec sp_spaceused 'dbo.myheap' --myheap 0 0 KB 0 KB 0 KB 0 KB
go
--cleanup
drop table dbo.myheap
go
对于聚簇tables,重建聚簇索引(或ALL):
ALTER INDEX ALL /*clusteredindexname*/ ON dbo.myclusteredtable REBUILD;