2

我有两个表,比如说 Main,transactions 表

主要的 :

mainID Name  Active
1  sharath  1
2  kiran    0
3  ashish   1

交易 :

TransID TransType status MainID IsActive
101      D         22    1       0
102      R         27    2       1
103      R         32    2       1
104      D         11    1       0
105      R         43    3       0

在这些更新语句中更可取:

1)

Update TR
set  status  = 0,
     Isactive = 0
from Transaction TR
inner join main MN with(nolock) on MN.MainID = TR.MainID 
where MN.Isactive = 0

2)

Update TR
set  status  = 0,
     Isactive = 0
from Transaction TR
inner join main MN on MN.MainID = TR.MainID 
where MN.Isactive = 0

在第一个查询中,我使用 Main 进行了内部连接,并使用with(nolock)它来更新 Isactive = 0 的那些 MainID 的记录

在第二个查询中,没有使用with(nolock)我写的更新语句。

哪一个更可取,n将来也会进行更新而不会出现任何错误。

4

1 回答 1

1

将 NOLOCK 与 SELECT 一起使用将返回可能是脏的数据。当我们确定脏数据没问题时,我们可以使用这个表提示来快速得到结果。但是我看不出为什么应该在 DML 上使用 NOLOCK,因为脏数据可能会被写入 db 并提交。主表中未提交的事务可能会将脏数据引入事务表。

于 2013-02-26T09:12:56.993 回答