在我们学习SQL优化相关案例时,在删除表时,会遇到purge关键字,那么今天我们就一起来聊聊purge关键字的用法。
用法:
清除Oracle回收站(recyclebin)中的表和索引并释放与其相关的空 间,还可清除回收站,或者清除表空间中记录的已删除的部分表空间。
语法:
接下来我们可以看看对应的操作:
创建一张表:
SQL> create table test01(id number);
Table created
插入数据:
SQL> insert into test01 values(1001);
1 row inserted
SQL> insert into test01 values(1002);
1 row inserted
SQL> select * from test01;
ID
----------
1001
1002
删除表:
SQL> drop table test01;
Table dropped
查看是否有该数据:
SQL> select object_name ,original_name,operation,droptime from recyclebin;
OBJECT_NAME ORIGINAL_NAME OPERATION DROPTIME
------------------------------ -------------------------------- --------- -------------------
BIN$4tmhFE5pQt2VpxAVHIw16A==$0 TEST01 DROP 2020-06-16:22:03:49
通过闪回恢复:
SQL> flashback table test01 to before drop;
Done
再次查看表:
SQL> select * from test01;
ID
----------
1001
1002
接下来,使用Purge关键字删除表
SQL> drop table test01 purge;
Table dropped
再次查看,recyclebin 中就不在有此数据。
SQL> select object_name ,original_name,operation,droptime from recyclebin where original_name='TEST01';
OBJECT_NAME ORIGINAL_NAME OPERATION DROPTIME
------------------------------ -------------------------------- --------- -------------------
查看回收站有多少条数据
SQL> select count(*) from recyclebin;
COUNT(*)
----------
51
清空回收站
SQL> purge recyclebin;
Done
再次查看回收站
SQL> select count(*) from recyclebin;
COUNT(*)
----------
0
purge后不能回滚和恢复。