简体   繁体   English

了解使用(updlock,rowlock)对SQL Server中的查询的影响

[英]Understanding effects of with (updlock, rowlock) on a query in SQL Server

I am using NHibernate to query data from a reporting database and would like to optimize READ performance. 我正在使用NHibernate查询报告数据库中的数据,并希望优化READ性能。 "Dirty reads" are not a concern at all. “脏读”根本不是一个问题。 NHibernate provides Lock Modes. NHibernate提供锁定模式。 If I use the LockMode.UpgradeNoWait mode I can see that in the generated SQL a 'with (updlock, rowlock)' appears. 如果我使用LockMode.UpgradeNoWait模式,我可以看到在生成的SQL中出现'with(updlock,rowlock)'。 I've been reading up on this, but my SQL is unfortunately not sophisticated enough to really understand the ramifications. 我一直在阅读这篇文章,但遗憾的是我的SQL不够复杂,无法真正理解其后果。 I believe what I really want is a 'with (nolock)' statement to appear, but NHibernate does not see to provide any way of doing that. 我相信我真正想要的是一个'with(nolock)'语句出现,但NHibernate并没有提供任何方法来做到这一点。

My question then is this: Let's say my table is being written to in other transactions. 我的问题是:让我们说我的表正在写入其他交易中。 Will the 'with (updlock, rowlock)' hint in a query against this table cause it to return any faster? 在这个表的查询中,'with(updlock,rowlock)'提示会导致它更快地返回吗? Can it hurt performance? 它会伤害性能吗? The query is a very simple select against a single table. 查询是针对单个表的非常简单的选择。 No joins. 没有加入。 There is a non-clustered index on the table that covers the query. 表上有一个非聚集索引,用于覆盖查询。

The updlock will place update locks on every row being touched (selected) - so this means until the end of the transaction (explicit or implicit), the row(s) touched by the SELECT will have an update lock on them which allows for other transactions to read, but not update or delete the row. updlock将在被触摸(选中)的每一行上放置更新锁 - 这意味着直到事务结束(显式或隐式), SELECT触及的行将对其进行更新锁定,允许其他行要读取的事务,但不更新或删除该行。

The rowlock just indicates that you want row-level locks instead of page or table locks. rowlock只是表明你想要行级锁 ,而不是页面或表锁。

That lock makes sense if you need to select first, then update a row within the same explicit transaction. 如果您需要先选择,然后在同一显式事务中更新一行,那么该锁定是有意义的。

It doesn't make it run any faster, and can cause other transactions to be blocked 它不会使它运行得更快,并且可能导致其他事务被阻止

Apparently I was mistaken about the use of Lock modes in NHibernate. 显然我错误地认为在NHibernate中使用Lock模式。 They do not have anything to do with adding the nolock hint to a query. 它们与将nolock提示添加到查询没有任何关系。 This is the way to do it. 这是做到这一点的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM