【Oracle】刪除大表操作一則

楊奇龍發表於2012-04-06
      因為資料庫空間不足,需要對歷史資料進行清理,查詢涉及的表竟然有550G,和開發溝通之後將歷史資料使用應用程式遷移到其他機器上,之後對舊錶進行刪除!(對於此種情況多少有些無奈,入職之前表已經存在了,建表的時候應該考慮使用分割槽表,清理資料會更方便)
 檢視錶的大小
YANG@yangdb>set timing on;
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            550.075195
Elapsed: 00:00:00.98
YANG@yangdb>
使用 truncate的reuse storage 特性,預設時是drop storage,這樣會直接對object佔用的刪除之後並不直接drop storage ,這樣可以避免回收大量的extent 太多導致系統資源緊張的情況
YANG@yangdb>truncate table YANG.YANGTAB reuse storage;
Table truncated.
Elapsed: 00:04:19.31
YANG@yangdb>
YANG@yangdb>
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            550.075195
Elapsed: 00:00:00.09
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 563277M;
ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 563277M
*
ERROR at line 1:
ORA-03230: segment only contains 72099438 blocks of unused space above high water mark
Elapsed: 00:00:00.27
第一次 size 設定的有點大!
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 503277M;
Table altered.
Elapsed: 00:00:17.99
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            491.481628
Elapsed: 00:00:00.03
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 453277M;
Table altered.
Elapsed: 00:00:14.50
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            442.653503
Elapsed: 00:00:00.03
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 403277M;
Table altered.
Elapsed: 00:00:14.86
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            393.825378
Elapsed: 00:00:00.03
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 323277M;
Table altered.
Elapsed: 00:00:22.53
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            315.700378
Elapsed: 00:00:00.02
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 253277M;
Table altered.
Elapsed: 00:00:28.05
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            247.341003
Elapsed: 00:00:00.02
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 183277M;
Table altered.
Elapsed: 00:00:54.36
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            178.981628
Elapsed: 00:00:00.13
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 123277M;
Table altered.
Elapsed: 00:00:33.64
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            120.387878
Elapsed: 00:00:00.03
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 83277M;
Table altered.
Elapsed: 00:00:22.36
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            81.3253784
Elapsed: 00:00:00.03
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 53277M;
Table altered.
Elapsed: 00:00:14.35
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            52.0285034
Elapsed: 00:00:00.02
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 33277M;
Table altered.
Elapsed: 00:00:09.40
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            32.4972534
Elapsed: 00:00:00.02
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 13277M;
Table altered.
Elapsed: 00:00:09.29
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            12.9660034
Elapsed: 00:00:00.02
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 3277M;
Table altered.
Elapsed: 00:00:04.44
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            3.20037842
Elapsed: 00:00:00.02
YANG@yangdb>ALTER table YANG.YANGTAB DEALLOCATE UNUSED KEEP 277M;
Table altered.
Elapsed: 00:00:03.08
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            .270690918
Elapsed: 00:00:00.07
YANG@yangdb>drop table YANG.YANGTAB;
Table dropped.
Elapsed: 00:00:01.11
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB_NEW';
SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB_NEW                        83.7783203
Elapsed: 00:00:00.09
Elapsed: 00:00:00.01
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB_NEW';

SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB_NEW                        83.7783203
YANG@yangdb>rename YANGTAB_new to YANGTAB;
Table renamed.
Elapsed: 00:00:00.10
YANG@yangdb>SELECT segment_name,bytes/1024/1024/1024 FROM user_Segments WHERE segment_name='YANGTAB';

SEGMENT_NAME                   BYTES/1024/1024/1024
------------------------------ --------------------
YANGTAB                            83.7783203
Elapsed: 00:00:00.03
YANG@yangdb>
Elapsed: 00:00:00.03

附上操作過程中遇到的低階錯誤
1 oracle 和mysql 之間對錶的重新命名的語法混淆了,汗!
YANG@yangdb>rename table YANG.YANGTAB_new to YANG.YANGTAB;
rename table YANG.YANGTAB_new to YANG.YANGTAB
       *
ERROR at line 1:
ORA-00903: invalid table name
2 表名不允許帶owner
YANG@yangdb>rename YANG.YANGTAB_new to YANG.YANGTAB;
rename YANG.YANGTAB_new to YANG.YANGTAB
       *
ERROR at line 1:
ORA-01765: specifying owner s name of the table is not allowed
Elapsed: 00:00:00.01

參考自己的另一篇文章

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-720583/,如需轉載,請註明出處,否則將追究法律責任。

相關文章