一条update语句如果是这样写
update a set a.Col1=b.Col1 from TableA a,TableB b where a.Col2=b.Col2 ————(1)还有这样写
update TableA set Col1=(select b.Col1 from TableA a,TableB b where a.Col2=b.Col2) ————(2)理论上说(1)应该比(2)快
可实际我测试(1)却比(2)慢,大约25w条的数据量...抑郁中不知道原因
TableA.col2与TableB.col2均存在索引然后可以引申到这样
update a set a.Col1=b.Col1 from TableA a,TableB b,TableC c where a.Col3=c.Col3 and b.Col2=c.Col2 ————(3)(3)我本来是这样写的
update TableA set Col1=(select b.Col1 from TableB b where b.Col2=(select c.Col2 from TableC c where a.Col3=c.Col3)) ————(4)首先(3)与(4)是否等价?应该不是的,那么(4)能够有更有效率的写法么
我的列都有索引然后大家能告诉我一下这个效率问题是什么原因呢..........
我抑郁着...
update a set a.Col1=b.Col1 from TableA a,TableB b where a.Col2=b.Col2 ————(1)还有这样写
update TableA set Col1=(select b.Col1 from TableA a,TableB b where a.Col2=b.Col2) ————(2)理论上说(1)应该比(2)快
可实际我测试(1)却比(2)慢,大约25w条的数据量...抑郁中不知道原因
TableA.col2与TableB.col2均存在索引然后可以引申到这样
update a set a.Col1=b.Col1 from TableA a,TableB b,TableC c where a.Col3=c.Col3 and b.Col2=c.Col2 ————(3)(3)我本来是这样写的
update TableA set Col1=(select b.Col1 from TableB b where b.Col2=(select c.Col2 from TableC c where a.Col3=c.Col3)) ————(4)首先(3)与(4)是否等价?应该不是的,那么(4)能够有更有效率的写法么
我的列都有索引然后大家能告诉我一下这个效率问题是什么原因呢..........
我抑郁着...
你把1再改一下试试:
update a set a.Col1=b.Col1
from (select Col1,Col2 from TableA) a join (select Col1,Col2 from TableB) b on a.Col2=b.Col2 --(1)
update a set a.Col1=b.Col1
from (select Col1,Col2 from TableA) a
join (select Col1,Col2 from TableC) c on a.Col3=c.Col3
join (select Col1,Col2 from TableB) b on c.Col2=b.Col2
再者,我怎么觉得用join会慢呢
--------
???
create table ta(a int, b int, c int)
create table tb(a int, b int, c int)insert ta(a,c) select 1,111
union all select 2,222
union all select 3,333insert tb(a,b) select 1,11
union all select 2,22
union all select 3,33update ta
set b=tb.b
from (select a,b from ta) ta join (select a,b from tb) tb on ta.a=tb.aselect * from tadrop table ta,tb/*
a b c
----------- ----------- -----------
1 11 111
2 22 222
3 33 333(3 row(s) affected)
*/
慢不慢试试不就知道了,,,
我觉得比where快
--方法2改写为
update a
set Col1=(select top 1 Col1 from TableB where Col2=a.Col2)
from TableA a