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. "Dirty reads" are not a concern at all. NHibernate provides Lock Modes. If I use the LockMode.UpgradeNoWait mode I can see that in the generated SQL a 'with (updlock, rowlock)' appears. I've been reading up on this, but my SQL is unfortunately not sophisticated enough to really understand the ramifications. 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.

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? 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.

The rowlock just indicates that you want row-level locks instead of page or table locks.

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. They do not have anything to do with adding the nolock hint to a query. This is the way to do it.

链接地址: http://www.djcxy.com/p/5618.html

上一篇: 更新声明中的DeadLock和UPDLOCK提示的用法

下一篇: 使用(updlock,rowlock)对SQL Server中的查询了解效果