我可以在数据库范围内应用 NOLOCK 吗?
有没有办法使WITH(NOLOCK)应用于在特定数据库上运行的任何SELECT语句?
Is there a way to make WITH(NOLOCK) be applied on any SELECT statement run on a particular database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
否,但您可以使用SNAPSHOT ISOLATION 数据库级别在 SQL Server 2005 及更高版本上,它应该对解决死锁有很大帮助。
SQL Server 2005 引入了 SQL-92 隔离级别的扩展,引入了 SNAPSHOT 隔离级别和 READ COMMITTED 的附加实现。新的 READ_COMMITTED_SNAPSHOT 隔离级别可以透明地替换所有事务的 READ COMMITTED。
SNAPSHOT 隔离指定事务中读取的数据永远不会反映其他同时事务所做的更改。事务使用事务开始时存在的数据行版本。读取数据时不会对数据加锁,因此 SNAPSHOT 事务不会阻止其他事务写入数据。写入数据的事务不会阻止快照事务读取数据。您需要通过设置 ALLOW_SNAPSHOT_ISOLATION 数据库选项来启用快照隔离才能使用它。
READ_COMMITTED_SNAPSHOT 数据库选项确定在数据库中启用快照隔离时默认 READ COMMITTED 隔离级别的行为。如果未显式指定 READ_COMMITTED_SNAPSHOT ON,则 READ COMMITTED 将应用于所有隐式事务。这会产生与设置 READ_COMMITTED_SNAPSHOT OFF(默认值)相同的行为。当 READ_COMMITTED_SNAPSHOT OFF 生效时,数据库引擎使用共享锁来强制执行默认隔离级别。如果将 READ_COMMITTED_SNAPSHOT 数据库选项设置为 ON,则数据库引擎默认使用行版本控制和快照隔离,而不是使用锁来保护数据。
No but you can use the SNAPSHOT ISOLATION Database level on SQL Server 2005 and up, it should help a lot with deadlocks
SQL Server 2005 introduces extensions to the SQL-92 isolation levels with the introduction of the SNAPSHOT isolation level and an additional implementation of READ COMMITTED. The new READ_COMMITTED_SNAPSHOT isolation level can transparently replace READ COMMITTED for all transactions.
SNAPSHOT isolation specifies that data read within a transaction will never reflect changes made by other simultaneous transactions. The transaction uses the data row versions that exist when the transaction begins. No locks are placed on the data when it is read, so SNAPSHOT transactions do not block other transactions from writing data. Transactions that write data do not block snapshot transactions from reading data. You need to enable snapshot isolation by setting the ALLOW_SNAPSHOT_ISOLATION database option in order to use it.
The READ_COMMITTED_SNAPSHOT database option determines the behavior of the default READ COMMITTED isolation level when snapshot isolation is enabled in a database. If you do not explicitly specify READ_COMMITTED_SNAPSHOT ON, READ COMMITTED is applied to all implicit transactions. This produces the same behavior as setting READ_COMMITTED_SNAPSHOT OFF (the default). When READ_COMMITTED_SNAPSHOT OFF is in effect, the Database Engine uses shared locks to enforce the default isolation level. If you set the READ_COMMITTED_SNAPSHOT database option to ON, the database engine uses row versioning and snapshot isolation as the default, instead of using locks to protect the data.
取决于你的数据库。某些数据库引擎允许您默认脏读或类似行为。
例如,某些 MSSQL Server 版本允许您将数据库设置为快照隔离,这在 SQLMenace 的帖子中进行了描述。
Depends on your database. Some database engines allow you to default to dirty reads or similar behaviors.
Example, some MSSQL Server versions allow you to setup the database as snapshot isolation, which is described in SQLMenace's post.