Re: UPDLOCK and HOLDLOCK

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Andrew J. Kelly (sqlmvpnooospam_at_shadhawk.com)
Date: 11/16/04


Date: Tue, 16 Nov 2004 17:43:52 -0500

It's purpose is to tell sql server where to start and not where to end up.
Since the default is ROW level it is usually pointless to specify a ROWLOCK
hint but to be complete and cover all the bases they have it. I can think
of only one time where I saw it used effectively and in a nutshell it went
something like this: There were updates being done that affected thousands
of rows at a time. Most of these rows were contiguous and all on one of
several pages. It would be more efficient for SQL Server to use a Pagelock
instead of several thousand row locks. But there was one or two rows in
amongst these that needed to be available for another process to read and
update. If page level locks were taken it would block the other process.
By specifying Rowlock in the update it kept it at a row level and not the
page. You see locks are only escalated directly up to the table level. It
never goes from Row - page - table. It's always Row - table or Page to
table. There were not enough locks to warrant a table level escalation so
the Rowlock hint kept sql server from starting at the Page level which it
has all rights to do if the conditions are right. The more useful of the
locking hints is actually the PAGLOCK hint. That tells SQL Server to start
at the page level and skip the rows altogether. In a situation such as I
mentioned above where you don't have contention on any of the pages being
updated you can save a lot of locks and overhead. Let me know if that
clears it up or just confuses you more<g>.

-- 
Andrew J. Kelly  SQL MVP
"Toby Herring" <therring*@*teletrack.com> wrote in message 
news:uDEAVHCzEHA.3408@tk2msftngp13.phx.gbl...
> Really?  My experience says otherwise.  Do you know of any sample cases 
> that show this to be the case?
>
> Everybody else in here jumped all over my about a month ago about ROWLOCK 
> being the default locking mechanism.  If it is in fact the default, but it 
> doesn't prevent escalation, what exactly is the point of its existence?
>
> -- 
> Toby Herring
> MCDBA, MCSD, MCP+SB
> Need a Second Life? 
> http://secondlife.com/ss/?u=03e0e5b303c234bf08e80ee40119a65e
>
>
> "Andrew J. Kelly" <sqlmvpnooospam@shadhawk.com> wrote in message 
> news:%23z7G1PBzEHA.2016@TK2MSFTNGP15.phx.gbl...
>> They believe it will keep the locks from escalating but this is just not 
>> true.
>
> 


Relevant Pages

  • Re: row vs page locking...
    ... so they automatically escalate to page level locks. ... 'Lock Escalation' - see Books Online. ... SQL Server 2005 is going to have a Snapshot ... Good old fashioned locking is less sexy, but I find, more productive! ...
    (microsoft.public.sqlserver.server)
  • Re: Repeatable read. What is it exactly...
    ... SQL Server will hold locks on the data you have read ... can change change the data until you commit or rollback. ... SQL Server locks the data you read. ... > as transaction Isolation levels go. ...
    (microsoft.public.sqlserver.server)
  • Re: Locking in SQL Server and Oracle
    ... I am glad you would like to stay with SQL Server :-) ... > hard it would be to convert to Oracle during our next PeopleSoft upgrade. ... > had thought that since databases are databases, tables are tables, and SQL ... > uses row-level locks exclusively. ...
    (microsoft.public.sqlserver.server)
  • Re: Database Blocking Redux
    ... SQL Server stores rows on pages and pages on extents. ... Given the number of locks to manage, it's rare for SQL Server to lock individual rows. ... If SQL Server finds there are too many pages locked, it escalates locks to the entire extent and then to the entire table. ... When reading rows using SqlDataReader, does reading the next row release the lock on the previous row? ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Need help with DAO and SQL Server locking issue
    ... I will forward your response to the client. ... > Absolutely nobody uses DAO to work with SQL Server, ... I'd advise moving code that uses recordsets ... >>Locks are being placed on tables inside of a transaction each time the ...
    (microsoft.public.sqlserver.odbc)