请教 2表(多表)update 2个(多个)值 sql怎么写?

victorforever 2005-11-10 05:45:31
2表(多表)update 2个(多个)值 sql怎么写?

其中table1表100多万条数据,table2表30多万条,最多也就是更新table1中的30多万条,其余的不更新
更新1列,可以按下面的写,如果要更新多个值,sql怎么写?

update table1 a
set a.col3 = nvl((select b.col3
from table2 b
where b.col1 = a.col1
and b.col2 = a.col2), a.col3);

update操作怎么优化,能提高执行效率?
谢谢!
...全文
1337 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
kulama2004 2005-11-11
  • 打赏
  • 举报
回复
update table1 a
set a.col3 = (select b.col3
from table2 b
where b.col1 = a.col1
and b.col2 = a.col2 and b.col3 is not null );

table2的col1,col2,col3加index
table1的index先drop掉,之后重建
sanoul 2005-11-11
  • 打赏
  • 举报
回复
如果更新的字段中含有INDEX,最好先DROP掉,更新完了再重建.
Hangfeng 2005-11-11
  • 打赏
  • 举报
回复
表数据量比较多的话,进行关联操作一定要建索引.
update table1 a
set (a.col3,a.col4)=(select b.col3,b.col4
from table2 b
where b.col1=a.col1
and b.col2=a.col2)
像上面的语句,col1,col2都要建索引.
xjqqxjqq 2005-11-11
  • 打赏
  • 举报
回复
同意楼上的,更新表,如果数据比较多最好把index先drop掉,会快一点。
victorforever 2005-11-10
  • 打赏
  • 举报
回复
试过了,很慢,测试了一下,更新5000条数据要20多分钟
被更新的表table1有几个index,不知道是不是跟index有关呢?
zzwind5 2005-11-10
  • 打赏
  • 举报
回复
要么写个存储过程不知道能不能快点

declare
cursor c_name is select col1, col3 from table2;
begin
for tab in c_name loop
update table1
set col3=tab.col3
where col1.tab.col1;
end loop;
end;
victorforever 2005-11-10
  • 打赏
  • 举报
回复
我这样试过,速度太慢,几个小时,都完不了,我就给停了

想看看能否少一次扫描
zzwind5 2005-11-10
  • 打赏
  • 举报
回复
楼主这样是全表更新
这样会快点
update table1 a
set a.col3 = (select b.col3
from table2 b
where b.col1 = a.col1)
where exists (
select 1
from table2 b
where b.col1 = a.col1)

17,089

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧