【Oracle】刪除大表操作一則
因為資料庫空間不足,需要對歷史資料進行清理,查詢涉及的表竟然有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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle億級大表高效刪除案例分析Oracle
- 【Oracle】刪除所有表Oracle
- oracle批次刪除表Oracle
- oracle批量刪除表Oracle
- 刪除大表資料
- Oracle大表刪除部分資料的最佳方案Oracle
- oracle 失誤刪掉資料檔案後,刪除表空間操作Oracle
- 刪除oracle表被鎖住Oracle
- oracle級聯刪除使用者,刪除表空間Oracle
- oracle大資料量分批刪除Oracle大資料
- python 刪除大表資料Python
- MySQL大表刪除解決方案MySql
- 在MySQL中如何有效的刪除一個大表?MySql
- oracle刪除表欄位和oracle表增加欄位Oracle
- Oracle快速找回被刪除的表Oracle
- Oracle-給表刪除欄位Oracle
- Oracle 閃回刪除表原理分析Oracle
- oracle資料庫備份刪除操作Oracle資料庫
- MySQL如何優雅的刪除大表MySql
- 如何刪除大表中的資料
- ORACLE刪除-表分割槽和資料Oracle
- ORACLE表空間的建立修改刪除Oracle
- Oracle批量建立、刪除資料庫表Oracle資料庫
- oracle資料庫建立、刪除索引等操作Oracle資料庫索引
- Oracle表 列欄位的增加、刪除、修改以及重新命名操作sqlOracleSQL
- Oracle恢復誤操作刪除掉的表Oracle
- 刪除 oracleOracle
- 【Oracle】 RAC 環境刪除oracle 之一Oracle
- 【C/C++】資料庫刪除大表C++資料庫
- MYSQL下如何安全的快速刪除大表MySql
- Oracle如何刪除表中重複記錄保留第一條Oracle
- Laravel 軟刪除操作Laravel
- Oracle如何刪除表中重複記錄Oracle
- oracle 刪除表空間試驗面面觀Oracle
- oracle修改表增加列刪除列修改列Oracle
- 大資料量刪除的思考(一)大資料
- 【北亞資料恢復】誤刪除oracle表和誤刪除oracle表資料的資料恢復方法資料恢復Oracle
- 200G大表刪除資料方案