使用Oracle OracleBulkCopy遇到的問題


OracleBulkCopy是批量導入數據的工具,效率比Insert高,行為類似SQL*Loader,官方資料Bulk Copy
OracleBulkCopy執行之前,數據庫會將導入表相關的索引設置為不可用狀態,以便快速導入數據
在導入數據完成后,嘗試將索引還原
官方描述是

Integrity Constraints Affecting Oracle Bulk Copy
During a Oracle bulk copy, some integrity constraints are automatically enabled or disabled, as follows:

Enabled Constraints

During an Oracle bulk copy, the following constraints are automatically enabled by default:

NOT NULL

UNIQUE

PRIMARY KEY (unique-constraints on not-null columns)

NOT NULL constraints are checked at column array build time. Any row that violates the NOT NULL constraint is rejected.

UNIQUE constraints are verified when indexes are rebuilt at the end of the load. The index is left in an Index Unusable state if it violates a UNIQUE constraint.

Disabled Constraints

During an Oracle bulk copy, the following constraints are automatically disabled by default:

CHECK constraints

Referential constraints (FOREIGN KEY)

If the EVALUATE CHECK_CONSTRAINTS clause is specified, then CHECK constraints are not automatically disabled. The CHECK constraints are evaluated during a direct path load and any row that violates the CHECK constraint is rejected.

如果導入的數據導致索引規則被破壞,則還原操作會失敗,比如唯一索引的字段,當前數據表中出現了重復的數據,則嘗試還原的操作會失敗
而執行OracleBulkCopy的過程並不會報錯

在執行完成后,可以通過查詢索引的方式檢查是否有索引被破壞

select * from user_indexes where tablespace_name='tablespace_name' and status!='VALID'

如果有,根據索引找到具體表,進行處理,具體處理如下:
1.刪除索引
如果直接嘗試修改數據,會報錯說索引正處於UNUSUAL狀態,不能進行操作,所以要先刪除索引
2.修改數據,使其滿足索引要求
3.重建索引

也可以在執行OracleBulkCopy操作之前備份數據庫,即直接備份、還原數據庫即可

更好的使用OracleBulkCopy

先使用OracleBulkCopy將數據導入臨時表,再使用Insert導入目標表

參考資料

What specifically does OracleBulkCopy do, and how can I optimize its performance?
慎用 OracleBulkCopy
Oracle bulk copy duplicate rows


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM