Monday 7 March 2016

Lock Escalation in SQL Server

Today lets talk about Lock Escalations in SQL Server. Lock Escalations are an optimization technique used by SQL Server to control the amount of locks that are held within the Lock Manager of SQL Server. Let’s start in the first step with the description of the so-called Lock Hierarchy in SQL Server, because that’s the reason why the concept of the Lock Escalations exists in a relational database like SQL Server.

Lock Hierarchy
The following picture shows you the lock hierarchy used by SQL Server:

As you can see from the picture, the lock hierarchy starts at the database level, and goes down to the row level. You always have a Shared Lock (S) on the database level itself. When your query is connected to a database (e.g. USE MyDatabase), the Shared Lock prevents the dropping of the database, or that backups are restored over that database. And underneath the database level, you have locks on the table, on the pages, and the records when you are performing an operation.
When you are executing a SELECT statement, you have an Intent Shared Lock (IS) on the table and page level, and a Shared Lock (S) on the record itself. When you are performing a data modification statement (INSERT, UPDATE, DELETE), you have an Intent Exclusive or Update Lock (IX or IU) on the table and page level, and a Exclusive or Update Lock (X or U) on the changed records. SQL Server always acquires locks from top to bottom to prevent so-called Race Conditions, when multiple threads trying to acquire locks concurrently within the locking hierarchy. Imagine now how the lock hierarchy would look like, when you perform a DELETE operation on a table against 20.000 rows. Let’s assume that a row is 400 bytes long, means that 20 records fit onto one page of 8kb:

You have one S Lock on the database, 1 IX Lock on the table, 1.000 IX locks on the pages (20.000 records are spread across 1.000 pages), and you have finally 20.000 X locks on the records itself. In sum you have acquired 21.002 locks for the DELETE operation. Every lock needs in SQL Server 96 bytes of memory, so we look at 1.9 MB of locks just for 1 simple query. This will not scale indefinitely when you run multiple queries in parallel. For that reason SQL Server implements now the so-called Lock Escalation.

Lock Escalations
As soon as you have more than 5.000 locks on one level in your locking hierarchy, SQL Server escalates these many fine-granularity locks into a simple coarse-granularity lock. By default SQL Server *always* escalates to the table level. This mean that your locking hierarchy from the previous example looks like the following after the Lock Escalation has been successfully performed.

As you can see, you have only one big lock on the table itself. In the case of the DELETE operation, you have one Exclusive Lock (X) on the table level. This will hurt the concurrency of your database in a very negative way! Holding an Exclusive Lock on the table level means that no other session is able anymore to access that table – every other query will just block. When you are running your SELECT statement in the Repeatable Read Isolation Level, you are also holding your Shared Locks till the end of the transaction, means you will have a Lock Escalation as soon as you have read more than 5.000 rows. The result is here one Shared Lock on the table itself! Your table is temporary readonly, because every other data modification on that table will be blocked!
There is also a misconception that SQL Server will escalate from the row level to the page level, and finally to the table level. Wrong! Such a code path doesn’t exist in SQL Server! SQL Server will by default *always* escalate directly to the table level. An escalation policy to the page level just doesn’t exist. If you have your table partitioned (Enterprise Edition only!), then you can configure an escalation to the partition level. But you have to test here very carefully your data access pattern, because a Lock Escalation to the partition level can cause a deadlock. Therefore this option is also not enabled by default.
Since SQL Server 2008 you can also control how SQL Server performs the Lock Escalation – through the ALTER TABLE statement and the property LOCK_ESCALATION. There are 3 different options available:
- TABLE
- AUTO
- DISABLE
-- Controlling Lock Escalation
ALTER TABLE Table_name
SET(
LOCK_ESCALATION = AUTO -- or TABLE or DISABLE
)GO
The default option is TABLE, means that SQL Server *always* performs the Lock Escalation to the table level – even when the table is partitioned. If you have your table partitioned, and you want to have a Partition Level Lock Escalation (because you have tested your data access pattern, and you don’t cause deadlocks with it), then you can change the option to AUTO. AUTO means that the Lock Escalation is performed to the partition level, if the table is partitioned, and otherwise to the table level. And with the option DISABLE you can completely disable the Lock Escalation for that specific table. But disabling Lock Escalations is not the very best option, because the Lock Manager of SQL Server can then consume a huge amount of memory, if you are not very carefully with your queries and your indexing strategy.

Lock Escalation Thresholds
Lock escalation is triggered when lock escalation is not disabled on the table by using the ALTER TABLE SET LOCK_ESCALATION option, and when either of the following conditions exists:

- A single Transact-SQL statement acquires at least 5,000 locks on a single nonpartitioned table or index.
- A single Transact-SQL statement acquires at least 5,000 locks on a single partition of a partitioned table and the ALTER TABLE SET LOCK_ESCALATION option is set to AUTO.
- The number of locks in an instance of the Database Engine exceeds memory or configuration thresholds.
If locks cannot be escalated because of lock conflicts, the Database Engine periodically triggers lock escalation at every 1,250 new locks acquired.

Conclusion
Lock Escalation in SQL Server is mainly a nightmare. How will you delete more than 5.000 rows from a table without running into Lock Escalations? You can disable Lock Escalation temporarily, but you have to be very careful here. Another option (that I’m suggesting) is to make your DELETE/UPDATE statements in a loop as different, separate transactions: DELETE/UPDATE less than 5.000 rows, so that you can prevent Lock Escalations. As a very nice side-effect your huge, big transaction will be splitted into multiple smaller ones, which will also help you with Auto Growth issues that you maybe have with your transaction log.
Click Here For more details

15 comments:

  1. Howdy! I know this is kinda off topic however I'd fiyured I'd ask.
    Would you bbe interested in trading links or maybe guest authoring a blog post
    or vice-versa? My site goees over a lot of the same topics ass yours
    and I feel we could greatloy benefit from each
    other. If you happen to bee interested feel free too send me an email.

    I look forward to hearing from you! Great blog by the way!

    ReplyDelete
  2. What's up aⅼl, here еvery one is sharing these familiarity,
    tthus іt's nice t᧐ rеad thus website, and I used to
    pay а quick visit tһis weblog every ⅾay.

    ReplyDelete
  3. Thank you for the auspicioᥙs writeup. It in fact was a amսsement account it.
    Look adѵanced to far added agreeable from you! However, how can we communicate?

    viѕit site : Hοw To Password Protect Folder In 3 Easy Stеps

    ReplyDelete
  4. Fastidious answer back іn return of this query with solid arguments
    and telling everythіng about that.
    his response : Best Encryption Software Android Apps & 10 Deadly Sins Of How
    To Ꭼncryρt A Password For Freeing

    ReplyDelete
  5. whoah this blog is great i like reading your articles. Stay up the good work!
    You know, lots of people are looking round for this information, you can help them greatly.

    ReplyDelete
  6. Since the admin of this web site is working, no doubt very soon it will be renowned, due to its
    quality contents.

    ReplyDelete
  7. I've been exploring for a little bit for any high-quality articles or blog posts
    in this kind of space . Exploring in Yahoo I finally stumbled
    upon this web site. Studying this information So i'm happy to exhibit that I've a very excellent uncanny feeling I came
    upon just what I needed. I so much for sure will make certain to don't
    omit this web site and give it a look on a continuing basis.

    ReplyDelete
  8. Pretty! This has been a really wonderful article. Many
    thanks for supplying these details.

    ReplyDelete
  9. Yay google is my king aided me to find this outstanding web site!

    ReplyDelete
  10. I the efforts you have put in this, thanks for all the great articles.

    ReplyDelete
  11. Hello, i think that i saw you visited my web site thus i came to ?return the favor?.I'm attempting to find things to enhance my website!I suppose its ok to use some of your ideas!!

    ReplyDelete
  12. My brother recommended I might like this website.
    He used to be entirely right. This submit actually made my day.

    You can not believe simply how so much time I had spent for this information!
    Thanks!

    ReplyDelete
  13. I truly wanted to post a small message to be
    able to thank you for these precious facts you are sharing on this website.

    My time consuming internet search has now been paid with useful knowledge to share with my best
    friends. I 'd repeat that we visitors actually are rather endowed to exist in a notable
    website with so many perfect professionals with insightful secrets.
    I feel truly lucky to have discovered the web pages and look forward to really more entertaining minutes reading here.
    Thanks a lot again for everything.

    ReplyDelete
  14. hi!,I love your writing very much! share we communicate more approximately your article on AOL?
    I need a specialist in this area to resolve my problem.
    Maybe that is you! Looking ahead to look you.

    ReplyDelete
  15. It's going to be end of mine day, but before end I am reading this wonderful article to improve
    my knowledge.

    ReplyDelete