Locking & Blocking Made Simple: Our Agenda
Locking & Blocking Made Simple: Our Agenda
Locking & Blocking Made Simple: Our Agenda
Joe Webb Microsoft SQL Server MVP WebbTech Solutions, LLC [email protected]
Our Agenda
The Nature of Multi-User Databases The Basics of Locking and Blocking Techniques for Controlling Locking
A Bit of Background
Joe Webb Started in IT in 1993 after receiving a BSEE & MBA from Auburn University Founded WebbTech Solutions in 1996 Providing consulting, mentoring, & technical training Began working with SQL Server in 1996 with version 6.0 5-time recipient of the Microsoft MVP Award for SQL Server Author or Coauthor of 3 books Served 6 years on the Board of Directors for PASS (sqlpass.org) Served 2 years on the Executive Committee as VP of Marketing and EVP of Finance Delivered more than 50 sessions at over 20 conferences in North America & Europe
9/11/2009
An Example
Total = $6,200
Pay $800
Balance = $200
9/11/2009
Ensures that resources are not being simultaneously used by different processes in conflicting ways Provides consistency Prevents conflict
Results when one process must wait for another process to release a resource Decreases transactional throughput, increases wait times, etc
Locking
Types of Locks
Lock Type Shared (S) Update (U) Description Indicates that the resource is being read by a process Indicates that a process is going to update the resource. Only one Update lock can exist at a time for a resource. Before writing, its converted to an Exclusive lock Used to prevent multiple processes from attempting the same resource at one time. Used to establish lock hierarchy. They are placed before lower level locks for efficiency and to protect the lower level locks Protects the definition of a resource during data manipulation or prevents data manipulation when a DDL command is executing Used when copying large amounts of data into a table Protects a range of key values for SERIALIZABLE operations
Lockable Resources
Resource
Database File Object Page Key Extent RID Application Metadata HOBT Allocation unit
Blocking
9/11/2009
Lock Compatibility
Shared Shared Update Exclusive Update Exclusive
= Ok = Wait
Lock Granularity
Lock Escalation
SQL Server attempts to be as granular as possible to increase concurrency Lock Escalation is the process of converting many lowerlevel locks to fewer higher level locks Escalations occur when:
The number of locks for a table scan is greater than approximately 5,000 The amount of non-AWE memory used for locks exceeds 24% and the locks setting is zero The amount of non-AWE memory used for locks exceed 40% and the locks setting is non-zero
Escalation decreases the amount of server resources required to keep track of locks while increasing the possibility of concurrency conflicts
9/11/2009
Granularity Hints
Hint NOLOCK PAGLOCK ROWLOCK TABLOCK TABLOCKX UPDLOCK XLOCK Description Instructs SQL Server to disregard locks placed on resources, allowing dirty reads Places locks at the page level, rather than row or table level Places locks on rows rather than pages or tables Places locks on a table rather than at a lower level Places an exclusive lock on a table Places an update lock on a resource Indicates that an exclusive lock is to be placed on the specified resource; maybe used with PAGLOCK, ROWLOCK, and TABLOCK
9/11/2009
9/11/2009
9/11/2009
Indexing
Use narrow keys for indexes Create adequate indexes for reading data to reduce the number of rows to be read Consider including additional columns in nonclustered indexes to convert them to covering indexes
9/11/2009
Additional Resources
http://www.sql-server-performance.com http://msdn.microsoft.com/enus/library/ms191168.aspx http://www.simpletalk.com/content/article.aspx?article=671 http://support.microsoft.com/kb/271509 http://support.microsoft.com/kb/271509 Inside SQL Server books http://www.webbtechsolutions.com/blog http://www.twitter.com/joewebb
9/11/2009
Questions?
Thank you!
[email protected] http://www.webbtechsolutions.com/blog http://www.twitter.com/joewebb
10