在一个sql里面有很多重复子查询,怎么优化 5
updatetabletseta1=NVL(a1,(selecta1fromtablewheret.a1=a1anda1isnotnullandt.call_Id=#ca...
update table t set
a1=NVL(a1,(select a1 from table where t.a1=a1 and a1 is not null and t.call_Id=#callId#,ROWNUM=1)),
a2=NVL(a1,(select a2 from table where t.a2=a2 and a2 is not null and t.call_Id=#callId#,ROWNUM=1)),
a3=NVL(a1,(select a3 from table where t.a3=a3 and a3 is not null and t.call_Id=#callId#,ROWNUM=1)),
a4=NVL(a1,(select a4 from table where t.a4=a4 and a4 is not null and t.call_Id=#callId#,ROWNUM=1)),
a5=NVL(a1,(select a5 from table where t.a5=a5 and a5 is not null and t.call_Id=#callId#,ROWNUM=1)),
a6=NVL(a1,(select a6 from table where t.a6=a6 and a6 is not null and t.call_Id=#callId#,ROWNUM=1)),
where 条件判断
问题是 在更新时有很多子查询 而且这些子查询都是查询同一个表,这个在实际上效率很低,怎么优化啊 展开
a1=NVL(a1,(select a1 from table where t.a1=a1 and a1 is not null and t.call_Id=#callId#,ROWNUM=1)),
a2=NVL(a1,(select a2 from table where t.a2=a2 and a2 is not null and t.call_Id=#callId#,ROWNUM=1)),
a3=NVL(a1,(select a3 from table where t.a3=a3 and a3 is not null and t.call_Id=#callId#,ROWNUM=1)),
a4=NVL(a1,(select a4 from table where t.a4=a4 and a4 is not null and t.call_Id=#callId#,ROWNUM=1)),
a5=NVL(a1,(select a5 from table where t.a5=a5 and a5 is not null and t.call_Id=#callId#,ROWNUM=1)),
a6=NVL(a1,(select a6 from table where t.a6=a6 and a6 is not null and t.call_Id=#callId#,ROWNUM=1)),
where 条件判断
问题是 在更新时有很多子查询 而且这些子查询都是查询同一个表,这个在实际上效率很低,怎么优化啊 展开
1个回答
展开全部
我觉得你应该分成6个更新语句来执行,这里使用ax代替a1...a6
如下:
UPDATE TABLE t SET
t.ax = (SELECT tx.ax FROM TABLE tx WHERE tx.ax = t.ax AND tx.call_Id = #callId# AND ROWNUM = 1)
WHERE t.ax IS NULL and 条件判断
不知道这个是不是你想要的结果,生产数据修改,请注意备份
如下:
UPDATE TABLE t SET
t.ax = (SELECT tx.ax FROM TABLE tx WHERE tx.ax = t.ax AND tx.call_Id = #callId# AND ROWNUM = 1)
WHERE t.ax IS NULL and 条件判断
不知道这个是不是你想要的结果,生产数据修改,请注意备份
追问
在一条语句上优化,因为这个字段很多的 不是6个 分开写也不怎么好了
追答
一条语句,每次更新都会更新6个字段,而且这些更新有些是无用的,你用了nvl,分成6查询时查询会消耗掉时间
可以试试这样,使用语句块:
declare
cursor c1 is select rowid as rid from table t where 条件判断;
begin
for r in c1 loop
-- 更新字段
UPDATE TABLE t SET
t.a1 = (SELECT a1 FROM TABLE tx WHERE tx.a1 = t.a1 AND tx.call_Id = #callId# AND ROWNUM = 1)
WHERE where t.rowid = r.rid and t.a1 IS NULL;
UPDATE TABLE t SET
t.a2 = (SELECT a2 FROM TABLE tx WHERE tx.a2 = t.a2 AND tx.call_Id = #callId# AND ROWNUM = 1)
WHERE where t.rowid = r.rid and t.a2 IS NULL;
-- 其他字段.....
end loop;
commit;
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询