今天看到一个表,空间有20G,想将其中的数据清理,将空间释放出来,
用delete速度太慢,
用drop会导致相关的procedure,package等报错,需要重新编译,非常麻烦,
最后,选择了truncate
操作步骤如下:
1.因为将要保留的数据建立备份
create table test_table_backup
as
select * from test_table where creation_date > trunc(sysdate -5)
2.truncate table
truncate table test_table drop storage
3.将数据回存
insert into test_table
select * from test_table_backup
4.删除备份的表
drop table test_table_backup
用truncate table时,有两个参数
1.drop storage会在删除数据后,将空间释放出来,可以看到表占用的空间变小了。
drop storage
deallocates the space currently used by the deleted rows
2.reuse storage 不会将空间释放出来。
reuse storage
leaves the space allocated from the deleted rows
注意:因为truncate操作不会进入到日志中,所以操作要小心,要不然,哭都没有地哭。
个人认为的缺点:没有办法加上where,以保留想要的数据。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69103/viewspace-1420783/
,如需转载,请注明出处,否则将追究法律责任。