急!在线等,如何提高某条sql更新语句的执行效率

lmw1314 2010-03-18 10:30:02

--两个表:tableA和tableB
--tableA字段:id,context
--tableB字段,id,startdate,context
--其中tableA的id是tableB的id的某段时间内的数据id,id自增,
--tableA数据量10万,tableB数据量100万,
--tableA中没有主键和索引,id唯一
--tableB中主键id,索引id、context、startdate
--现在需要更新tableB中的context,但是用下面的更新语句执行更新时会产生死锁。
--求各位大大帮忙!
--急!在线等!
update tableB a
set a.context=(select b.context from tableA b where a.id=b.id)
where exists (select 1 from tableA b where a.id = b.id)

...全文
67 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lmw1314 2010-03-18
  • 打赏
  • 举报
回复
谢谢!
hackersun12345 2010-03-18
  • 打赏
  • 举报
回复
update tableB a
set a.context=(select b.context from tableA b where a.id=b.id)
where exists (select 1 from tableA b where a.id = b.id)
看似简单,其实是效率最低的写法。
分析一下SQL语句:
select b.context from tableA b where a.id=b.id
tableA没有主键,对于10万 * 100万的两张表关联,这一句子查询我估算少说也得10秒钟,然后需要被更新tableA平均数据量大约10万/2 = 5万,那么平均执行时间为:5万*10秒=50万秒。估计等执行完,你头发也白了。

解决方案:
1、为tableA的ID加上索引,提高查询速度。
2、使用存储过程
CURSOR IS select b.context from tableA b where a.id=b.id;
对CURSOR进行循环,更新tableA

希望对你有所帮助!
suiziguo 2010-03-18
  • 打赏
  • 举报
回复
这一条语句就会产生死锁?

两表关联更新,这写法效率算高的了。

17,089

社区成员

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

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