用 2 亿数据更新 big table

update big table with 200mn data

我想用 0 更新 NULL TFLAG 列。由于 table 很大,更新列的有效方法是什么。

stats:
-----
Size             144 GB
Num Rows         235,676,098
Number of nulls   33408624

create table ticket (hkey number , tflag number);
alter table ticket add constraint hkey_p primary key (hkey);

Insert into TICKET (HKEY,TFLAG) values (1,1);
Insert into TICKET (HKEY,TFLAG) values (2,1);
Insert into TICKET (HKEY,TFLAG) values (3,null);
Insert into TICKET (HKEY,TFLAG) values (4,null);
Insert into TICKET (HKEY,TFLAG) values (5,null);
Insert into TICKET (HKEY,TFLAG) values (6,null);
Insert into TICKET (HKEY,TFLAG) values (7,null);
Insert into TICKET (HKEY,TFLAG) values (8,1);
Insert into TICKET (HKEY,TFLAG) values (9,null);
Insert into TICKET (HKEY,TFLAG) values (10,1);
Insert i

简单的答案是

update ticket
set hflag = 0
where hflag is null;

长答案是,这取决于。 table 是否同时处理大量事务?您可以添加一个 and rownum < 10000(或您认为合适的任何其他数字)来缩小交易规模。在更新之间添加提交以不锁定记录。但是如果并发事务不是问题,我会只使用上面的更新语句并让数据库执行它的操作。

最快的方法可能是:

create table ticket_new
as
select hkey, decode( tflag, null, 0, tflag ) tflag
from ticket;

如果你有资源,你也可以使用并行执行来做到这一点。

然后将 ticket 重命名为 ticket_old(作为备份) 将 ticket_new 重命名为 ticket

一切顺利,您现在可以放下 ticket_old。