從 Oracle 10g 開始, 就開始提供有 recyclebin 的功能 (資源回收筒) ,
所以一旦不小心 drop table 刪錯了, 而且已經 commit ,
由於在 Oracle 10g 裡面的運作, 只是把原本的 Table 名稱換成 BIN$1234567890==$0 ,
沒關係, 只要把 recyclebin 裡面的物件還原就好啦~~
總是會有不小心的時候嘛~~
也由於沒有釋放 Tablespace 的空間, 也因此查詢 tablespace 佔用大小時就會越來越慢 ,
所以建議還是固定時間去清理一下 ,
以下就是整個還原會用到的相關指令 :
1. 查看目前有那些 Table
select * from tab
2. 查看資源回收有那些物件
select * from recyclebin;
或
select * from dba_recyclebin
或 SQLPLUS 的
SQL> show recyclebin
3. 再來就是還原指令
FLASHBACK TABLE [被刪除的 Table Name] TO BEFORE DROP;
如果是刪除 Table 不要放到 recyclebin 呢?
drop table recycletest purge;
當然囉, 也可以指定那些物件確定要清除 recyclebin
purge table recycletest;
要清空資源回收筒:
purge recyclebin;
或
purge dba_recyclebin;
要注意喔~ 建議用 BIN$1234567890==$0 當作 Table 名稱的方式來還原,
因為有可能建立重複的 Table 名稱, 到時候還原的方式是以後刪除的先還原,
BIN$1234567890==$0 數字越大表示越晚刪除的物件.
遇到重複 Table 的情況時, 可以還原的時候變更 Table 的名稱,
flashback table [刪除的 Table 名稱] to before drop rename to [要還原的 Table 名稱];
當然也可以打開或關閉資源回收筒的功能:
alter session set recyclebin = off;
alter system set recyclebin = off;
因 TIPTOP 執行批次作業或是報表的時候,都會產生暫存 Table 的,
像是 TT 開頭+一串數字+_文字 的 Table 就是暫存時產生的資料,以後也不會再用到了。
有時候也不會 DROP 掉一直存留著,或是就算 DROP 也會一直留在回收筒,
只要在 Oracle 建立排程,定時把這些垃圾都清一清吧~
declare
str varchar2(1000);
num number;
begin
--刪除 TEMP TABLE
for i in(select table_name from user_tables ,user_objects where table_name like 'TT%' and created <= sysdate - 3 and table_name = object_name and object_type = 'TABLE' and tablespace_name = 'TEMPTABS')
loop
str:='drop table '||i.table_name||' purge ';
execute immediate str ;
commit;
end loop;
--清空回收筒
for i in(select object_name,droptime from user_recyclebin where droptime < to_char(sysdate-3,'YYYY-MM-DD hh:mm:ss') and type= 'TABLE' order by droptime)
loop
str:='purge table "'||i.object_name||'" ';
execute immediate str ;
commit;
end loop;
end;
沒有留言:
張貼留言