【问题标题】:Bulk update of column values of entire table批量更新整个表的列值
【发布时间】:2011-07-01 17:56:31
【问题描述】:

我们有一个大约 3500 万行的 Oracle 11g 数据库表。我们处于必须更新一列的所有值的情况。此列已编入索引。

我有一个脚本可以生成更新的值并将其填充到文本文件中。

我正在寻找一种对这张表进行批量更新的好策略。我们可以承受大约 10 小时的停机时间。

这是个好主意

  • 将整个表转储到平面文件中
  • 使用任何脚本语言更新值
  • 重新加载整个表
  • 重建索引

可能会遇到哪些陷阱?

我不擅长 PL/SQL。有没有办法在 PL/SQL 或数据库本身“内部”解决这个问题?

谢谢, 帕布

【问题讨论】:

  • 编辑:列被索引。在问题中更正了这一点。

标签: oracle11g


【解决方案1】:

最快的方法可能是根据更新值的平面文件创建external table,然后:

create table new_table as
  select o.col1, o.col2, o.col3, ..., x.value as colN
  from old_table o
  join extern_table x on ...;

(确保连接返回 old_table 中的所有行。连接可能需要是外连接。)

-- drop foreign key constraints that reference old_table
alter table child1 drop constraint fk_to_old_table1;
...

drop table old_table;

rename new_table to old_table;

-- Re-create indexes and constraints on old_table
alter table old_table add constraint oldpk primary key (col1);
...

-- Re-create the dropped foreign key constraints to old_table
alter table child1 add constraint fk_to_old_table1 ...;
...

【讨论】:

  • 托尼,感谢您的回复。我无法在查询本身中提供更新的值,因为我需要运行外部脚本来生成更新的值。这将在一个文本文件中。
  • 我已经更新了我的答案:您可以使用外部表将您的平面文件加入到查询中以创建新表。
猜你喜欢
  • 2012-11-22
  • 1970-01-01
  • 2011-04-19
  • 2020-01-08
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
  • 2020-06-03
  • 2016-01-28
相关资源
最近更新 更多