ITPub博客

首页 > 数据库 > Oracle > truncate table (PLSQL)

truncate table (PLSQL)

原创 Oracle 作者:smkuang 时间:2015-01-30 14:05:36 0 删除 编辑
今天看到一个表,空间有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/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论
  • 博文量
    4
  • 访问量
    10410