NOLOCK 与事务隔离级别

发布于 2024-07-24 16:39:46 字数 82 浏览 4 评论 0原文

使用“SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED”和NOLOCK有什么区别? 这个比那个好吗?

What's the difference between using "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED" and NOLOCK? Is one better than the other?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

意中人 2024-07-31 16:39:46

它们是同一件事,只是范围不同。 NOLOCK 是基于每个表放置的,SET Transaction... 可以放置为一个块。

They're the same thing, just scoped differently. NOLOCK is placed on a per table basis and SET Transaction... can be placed as a block.

娇妻 2024-07-31 16:39:46

NOLOCK 是一个查询提示,因此仅适用于指定它的查询中的特定表。

设置事务隔离级别适用于当前连接内此后执行的所有代码或直到显式修改为止。

需要澄清的是,从功能上讲,工作中的隔离级别是相同的,但涵盖的范围可能不同。

NOLOCK is a query hint and as such only applies to the specifc table within the query in which it is specified.

Setting the transaction isolation level applies to all code executed hence forth within the current connection or until it is explicitly modified.

To clarify, functionally the isoloation level at work is the same however the scope which is covered may not be.

方觉久 2024-07-31 16:39:46

请参阅几个小时前针对该问题的该答案 SQL Server SELECT 语句导致阻塞

引用 Remus Rusanu

SELECT 可以阻止更新。 正确设计的数据模型和查询只会导致最小的阻塞,而不是问题。 “通常”WITH NOLOCK 提示几乎总是错误的答案。 正确的答案是调整您的查询,使其不会扫描巨大的表。

如果查询不可调整,那么您应该首先考虑SNAPSHOT ISOLATION级别,其次您应该考虑使用DATABASE SNAPSHOTS,最后一个选项应该是DIRTY READS(最好更改隔离级别而不是使用NOLOCK HINT)。 请注意,正如名称明确指出的那样,脏读将返回不一致的数据(例如,您的总工作表可能不平衡)。

其他答案也可能对您有帮助。

See that answer from a few hours ago, to the question SQL Server SELECT statements causing blocking.

Quoting Remus Rusanu:

SELECT can block updates. A properly designed data model and query will only cause minimal blocking and not be an issue. The 'usual' WITH NOLOCK hint is almost always the wrong answer. The proper answer is to tune your query so it does not scan huge tables.

If the query is untunable then you should first consider SNAPSHOT ISOLATION level, second you should consider using DATABASE SNAPSHOTS and last option should be DIRTY READS (and is better to change the isolation level rather than using the NOLOCK HINT). Note that dirty reads, as the name clearly states, will return inconsistent data (eg. your total sheet may be unbalanced).

The other answers may help you as well.

鸩远一方 2024-07-31 16:39:46

它们具有相同的效果,只有一个用作锁定提示(nolock),另一个用于连接范围。

请小心其中任何一个 - 脏读可能是一件非常糟糕的事情,具体取决于您的应用程序。 两次读取同一条记录或由于页面移动而丢失一条记录对于用户来说可能是一件非常令人困惑的事情......

They have the same effect, only one is used as a lock hint (nolock) and the other is used for a connection scope.

Be careful with either of those - dirty reads can be a very bad thing depending on your app. Reading the same record twice or missing a record because of page movement can be a very confusing thing to users...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文