保证两个表的id上有索引:update t1 a set f=( select replace(f,a1,a2) from t1 ,t2 b where instr(f,a1,1)>0 and a.id=b.id) where exists(select * from t2 b where a.id=b.id)
update t1 a set f=(select replace(f,a1,a2) from t1 b,t2 where instr(f,a1,1)>0 and a.id=b.id) where a.id in (select id from t1,t2 where instr(f,a1,1)>0)总觉得这句有问题, 你的逻辑是什么? select replace(f,a1,a2) from t1 b,t2 where instr(f,a1,1)>0 and a.id=b.id) 这一句说明了什么? 只返回一个值吗?
to jxc(GameHeart): 谢谢. 但将in改为exists后会将t1中的所有f都update, 而且t2中并不存在id.to dinya2003(dinya 11i) : 语句实现将f中的a1替换为a2,即假如t1为: id f 1 343a 2 34577 3 456c768t2为: a1 a2 a * b % c $替换后t1应为: id f 1 343* 2 34577 3 456$768 请问是否有更好的update方法?谢谢。
update语句属于查询更新,像你这种循环查询更新速度更慢,我前一段时间刚因为这件事 郁闷过,建议: 1:先建立视图view_t1_update(目的:将已经更新后的数据存储到视图中) select a.id,replace(a.f,b.a1,b.a2) f from test1 a ,test2 b where instr(a.f,b.a1,1)>0 2:当需要更新时再从视图view_t1_update中取需要更新的值,这样便节省了时间 你可以试验一下,我这数据不多,试验效果不明显,另外,如果你的更新条件没有输入参数,只是表之间的关系,我觉得这种方式是可行的,有时候不一定非要一个SQL语句完成所有的工作,可以进行任务分解。
针对表T1中的每一条记录,都要计算instr,然后把计算出来的结果和t2表中的每一个进行比较. 对T1中的F字段建立一个函数索引. create index idx on T1 (instr(f,a1,1)) ; 但是一般用户要创建函数索引,必须有global query rewrite和create any index的权限.另:可以考虑写过程,用嵌套游标来执行查询,更新.
写个过程就行: create or replace procedure prc_up is cursor c_cursor is select a1,a2 from t2; v_id t1.id%type; v_f t1.f%type; begin open c1; loop fetch c1 into v_id,v_f; exit when c1%notfound; update t1 set f=replace(f,v_id,v_f); end loop; end prc_up;
对不起,上面游标写错了:这个对create or replace procedure prc_up is cursor c1 is select a1,a2 from t2; v_id t1.id%type; v_f t1.f%type; begin open c1; loop fetch c1 into v_id,v_f; exit when c1%notfound; update t1 set f=replace(f,v_id,v_f); end loop; end prc_up;
select replace(f,a1,a2)
from t1 ,t2 b where instr(f,a1,1)>0 and a.id=b.id)
where exists(select * from t2 b where a.id=b.id)
where a.id in (select id from t1,t2 where instr(f,a1,1)>0)总觉得这句有问题, 你的逻辑是什么?
select replace(f,a1,a2) from t1 b,t2 where instr(f,a1,1)>0 and a.id=b.id)
这一句说明了什么? 只返回一个值吗?
谢谢.
但将in改为exists后会将t1中的所有f都update,
而且t2中并不存在id.to dinya2003(dinya 11i) :
语句实现将f中的a1替换为a2,即假如t1为:
id f
1 343a
2 34577
3 456c768t2为:
a1 a2
a *
b %
c $替换后t1应为:
id f
1 343*
2 34577
3 456$768
请问是否有更好的update方法?谢谢。
update语句属于查询更新,像你这种循环查询更新速度更慢,我前一段时间刚因为这件事
郁闷过,建议:
1:先建立视图view_t1_update(目的:将已经更新后的数据存储到视图中)
select a.id,replace(a.f,b.a1,b.a2) f from test1 a ,test2 b where instr(a.f,b.a1,1)>0
2:当需要更新时再从视图view_t1_update中取需要更新的值,这样便节省了时间
你可以试验一下,我这数据不多,试验效果不明显,另外,如果你的更新条件没有输入参数,只是表之间的关系,我觉得这种方式是可行的,有时候不一定非要一个SQL语句完成所有的工作,可以进行任务分解。
对T1中的F字段建立一个函数索引.
create index idx on T1 (instr(f,a1,1)) ;
但是一般用户要创建函数索引,必须有global query rewrite和create any index的权限.另:可以考虑写过程,用嵌套游标来执行查询,更新.
create or replace procedure prc_up is
cursor c_cursor is select a1,a2 from t2;
v_id t1.id%type;
v_f t1.f%type;
begin
open c1;
loop
fetch c1 into v_id,v_f;
exit when c1%notfound;
update t1 set f=replace(f,v_id,v_f);
end loop;
end prc_up;
cursor c1 is select a1,a2 from t2;
v_id t1.id%type;
v_f t1.f%type;
begin
open c1;
loop
fetch c1 into v_id,v_f;
exit when c1%notfound;
update t1 set f=replace(f,v_id,v_f);
end loop;
end prc_up;
我试了你建议的方法,能稍微提高速度,但不是很明显。:(to dinya2003(dinya 11i) :
请问805是否不支持函数索引?我建不成功。to rolandzhang() :
我将你的过程稍微作了修改然后运行,但速度也不快啊。