SQL Update大表

时间:2016-01-20 11:27:38

标签: sql oracle merge sql-update bigdata

我有一个问题。我需要更新两个大表 - t_contact(1.7亿行)和t_participants(1100万行)。此表都包含列CUSTOMER_ID。其中一些ID错误,我需要更新它。错误的ID约为14万。 我明白,如果我使用UPDATE TABLE,它需要花费很多次,但这两个表一定不会长时间不可用。我该怎么办?

3 个答案:

答案 0 :(得分:1)

将一个表拆分为多个部分,并在PL / SQL块中逐个处理它们。例如,假设ID是结果,您可以将t_participants拆分为每个行数为100万的部分:

begin
  -- 1 and 11 - hardcoded values, 
  -- since your t_participants table has 11 000 000 rows
  for i in 1..11 loop 
    merge t_contact c
    using (select * from t_participants 
           where id between (i - 1) * 1000000 and i * 1000000) p
       on (c.id = p.id)
     when matched then update ...;
  commit;
  end loop;
end;

我拍摄了1000000条记录的大小,但你可以选择另一种尺寸。这取决于您的服务器性能。尝试手动更新100,1000,10000等行以定义最方便的大小。

答案 1 :(得分:1)

如果您将错误的ID存储在应使用合并的位置:

MERGE INTO t_contact D
USING (select * from t_wrong_ids) S
ON (D.CUSTOMER_ID = S.NEW_ID)
WHEN MATCHED THEN UPDATE SET D.CUSTOMER_ID = S.OLD_ID

比正常更新快很多。

第二张表是相同的:

MERGE INTO t_participants D
USING (select * from t_wrong_ids) S
ON (D.CUSTOMER_ID = S.NEW_ID)
WHEN MATCHED THEN UPDATE SET D.CUSTOMER_ID = S.OLD_ID

答案 2 :(得分:0)

declare
  i number := 0;
  cursor s1 is SELECT rowid, t.* FROM table_name  t WHERE column_name =x;
begin
  for c1 in s1 loop
      UPDATE table_name SET column_name = y
             where rowid = c1.rowid;
      i := i + 1;              -- Commit after every X records
      if i > 50000 then
         commit;
         i := 0;
      end if;
  end loop;
  commit;
end;
/