Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 45,540 times

Contents

Related Categories

SQL Server Locks - Lock Contention Solved

streamload

Lock Contention Solved

If you used Streamload.com at all during June, July, and August, you probably got a "You were the deadlock loser" error, or a "Lock timeout" error, or an "Object required" error. These were all caused by lock contention. After scouring the documentation and talking to a few people, I learned what I summarized above and will say again here:

  • SQL Server starts with row-level locks, but often escalates these to page and table locks, causing deadlocks
  • SQL Server requires locks for reading from the database (SELECTs), so even folks not trying to modify the database are affected by the lock system.

Fortunately, I stumbled across some obscure keywords from the SQL Server lexicon: NOLOCK and ROWLOCK. They are used like this:

SELECT COUNT(UserID)
FROM Users WITH (NOLOCK)
WHERE Username LIKE 'foobar'

and

UPDATE Users WITH (ROWLOCK)
SET Username = 'fred' WHERE Username = 'foobar'

What do these extra incantations do? We'll examine these two hints - NOLOCK and ROWLOCK - on the next page!

Shameless Plug
These and many other cutting-edge techniques power Streamload.com, a digital entertainment delivery site offering unlimited free online stora

Comments

  • Re: [1688] SQL Server Locks

    Posted by Griff on 15 Feb 2007

    I've implemented the [WITH (NOLOCK)] hint and it's been a reasonable success.  The only additional note of caution I would add is that we have one table that is occasionally updated (batch ins...

  • (Dead-)locking in tables

    Posted by hvse on 23 Sep 2005

    Great.

    This little hint with those little word (nolock) saved my life - and the life of our application. (http://www.gpsoverip.de)

    Thank you VERY much ...

  • Seeing Uncommitted Data

    Posted by RAFinnerty on 29 Sep 2004

    You say (with regard to locking): "For financial code and denormalized aggregates (those little counters of related data that you stash away and try desperately to keep accurate), you should play it s...

  • Transaction logs file Management

    Posted by alok_john on 27 Oct 2003

    Please contribute your suggestion in relation with various issues related with log file management in SQL server. Such as
    1.    Methods to Shrinking the log file.
    2.    Recover the database form Log file...