这个怎样? update table1 a set a.feild1 = val1 where a.val22 in (select val22 from table2 where b.state = 0);
table1为大表,table1为小表,在这种条件下,用IN的效率高于exist 的效率. 比较下面两个查询: select * from big where object_id in (select object_id from small) select * from big where exists( select null from small where small.object_id = big.object_id)
因此,我同意楼上的写法.
我个人的观点是: 只是看一下b.state = 0 和a.val22 = b.val22之间的概率那个大,如果是满足b.state = 0 的少就把它放在后面:即: update table1 a set a.feild1 = val1 where exist(select * from table2 b where a.val22 = b.val22 and b.state = 0 ); 因为这样第一次就可以排除大部分,同时我们也要考虑到关联的时间!所以我建议试一下我的方法; 希望把结果告之!
to blueshu(颓废中ing...) ,就一句“用游标”,也没说明怎么用呀?怎么用?
update table1 a,table2 b set a.feild1 = val1 where b.state = 0 and a.val22 = b.val22;这样不行吗?
在table2.state列上建索引了吗?建了索引应该能快速排除掉大部分数据,也许会快点。
colacoca(我是一瓶倒过来的可口可乐):您的方法是不可以的!
colacoca(我是一瓶倒过来的可口可乐),你的方法在Oracle里不行。
在Oracle中不支持这样的Update的吧: update table1 a,table2 b set a.feild1 = val1 where b.state = 0 and a.val22 = b.val22;
wupangzi(无本之木) 根据这个的需求b.state = 0这个条件还是放在前面,效率高。
如果b中b.state=0的记录少的话,可以这样写 update table1 a set a.field = val1 where exist ( select * from (select * from table2 b where b.state=0) c where c.val22=b.val22 ) 如果优化器只生成c表一次的话,应该可以更快吧。
update table1 a
set a.feild1 = val1
where a.val22 in (select val22 from table2 where b.state = 0);
table1为大表,table1为小表,在这种条件下,用IN的效率高于exist
的效率. 比较下面两个查询: select * from big where object_id in (select object_id from small)
select * from big where exists( select null from small
where small.object_id = big.object_id)
因此,我同意楼上的写法.
只是看一下b.state = 0 和a.val22 = b.val22之间的概率那个大,如果是满足b.state = 0 的少就把它放在后面:即:
update table1 a
set a.feild1 = val1
where exist(select * from table2 b
where a.val22 = b.val22
and b.state = 0 );
因为这样第一次就可以排除大部分,同时我们也要考虑到关联的时间!所以我建议试一下我的方法;
希望把结果告之!
set a.feild1 = val1
where b.state = 0
and a.val22 = b.val22;这样不行吗?
update table1 a,table2 b
set a.feild1 = val1
where b.state = 0
and a.val22 = b.val22;
根据这个的需求b.state = 0这个条件还是放在前面,效率高。
update table1 a
set a.field = val1
where exist ( select * from (select * from table2 b where b.state=0) c where c.val22=b.val22 )
如果优化器只生成c表一次的话,应该可以更快吧。