Skip to content

使用Oracle OracleBulkCopy遇到的问题

L edited this page Jan 13, 2021 · 4 revisions

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

Clone this wiki locally