--两个表: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)

解决方案 »

  1.   

    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希望对你有所帮助!