在一张产品表(Product)中有200万数据,表中包含下列几列:id,name,loginid,content,inserttime,modifytime。数据格式类似下面的样式:id name  loginid content  inserttime    modifytime
1 name1,张三,  content1, 2012-12-12 12:12:12, 2012-12-12 12:12:12
2 name2,张三,  content2, 2012-12-12 12:12:12, 2012-12-12 12:12:12
3 name3,张三,  content3, 2012-12-12 12:12:12, 2012-12-12 12:12:12
4 name4,李四,  content4, 2012-12-12 12:12:12, 2012-12-12 12:12:12
5 name5,李四,  content5, 2012-12-12 12:12:12, 2012-12-12 12:12:12
6 name6,王二,  content6, 2012-12-12 12:12:12, 2012-12-12 12:12:12
7 name7,王二,  content7, 2012-12-12 12:12:12, 2012-12-12 12:12:12现在有一个这样的需求:多个用户同时更新自己的数据,好比张三、李四、王二同时更新自己的修改时间。下面的sql语句能同时运行吗  update Product set modifytime=getdate() where loginid='张三';  update Product set modifytime=getdate() where loginid='李四';  update Product set modifytime=getdate() where loginid='李四';实际情况中,每个用户的数据在1万左右,有的更多,可能2万甚至10万,少的也有几百或者几千。请高人帮我解答下。

解决方案 »

  1.   

    不能同时运行
    顺序执行SQL语句
      

  2.   

    单纯直接这样执行,是会按顺序先执行张三,再李四,下面那个估计你没改过来而已。
    update Product set modifytime=getdate() where loginid='张三'; 
    update Product set modifytime=getdate() where loginid='李四';
    update Product set modifytime=getdate() where loginid='李四';如果是多个页面update是可以的。但是要注意数量不能太多,因为当锁数量超过5000时,就会升级,成为页锁或者表锁,这时候其他稍微慢一点点的就会出现等待,甚至逻辑不合理的情况下会出现死锁。所以最好使用顺序更新。
      

  3.   

    update Product set modifytime=getdate() where loginid='张三'; 
    update Product set modifytime=getdate() where loginid='李四';
    update Product set modifytime=getdate() where loginid='王二';sql语句有个笔误,应该是上面的
    loginid上有索引
    上面的sql语句是单个同时运行(可以理解为在不同的sql查询分析器里同时执行),不是在一个窗口里一起运行。可以变成:
    Begin Transaction
    update Product set modifytime=getdate() where loginid='张三'; 
    waitfor delay '00:00:20'
    commit transactionBegin Transaction
    update Product set modifytime=getdate() where loginid='李四'; 
    waitfor delay '00:00:20'
    commit transactionBegin Transaction
    update Product set modifytime=getdate() where loginid='王二'; 
    waitfor delay '00:00:20'
    commit transaction
      

  4.   

    假设把SET ( LOCK_ESCALATION =  DISABLE),禁止锁升级是可以办到的。但这样是会影响并发处理的。谁能给个比较好的处理方案呢
      

  5.   

    你这个操作需要经常频繁操作吗?如果是的话,要考虑如何改进了,这样做对性能有很大影响,不仅仅是锁,还有update的时候速度问题。