Locking And Concurrency

3,126 views
2,967 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,126
On SlideShare
0
From Embeds
0
Number of Embeds
31
Actions
Shares
0
Downloads
105
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Locking And Concurrency

  1. 1. ISQL Users Group September 2009 SQL Server Locking and Concurrency [email_address] www.sqlserverutilities.com blogs.microsoft.co.il/blogs/yaniv_etrogi
  2. 2. <ul><li>Concurrency Models </li></ul><ul><li>Pessimistic Concurrency </li></ul><ul><li>Optimistic Concurrency </li></ul>
  3. 3. <ul><li>Pessimistic Concurrency </li></ul><ul><li>Locks are acquired on data being read </li></ul><ul><ul><ul><li>Preventing other processes from modifying that data </li></ul></ul></ul><ul><ul><ul><li>Other processes can still read the data </li></ul></ul></ul><ul><li>Locks are acquired on data being modified </li></ul><ul><ul><ul><li>Preventing other processes from modifying that data </li></ul></ul></ul><ul><ul><ul><li>Preventing other processes from reading that data </li></ul></ul></ul>
  4. 4. <ul><li>Optimistic Concurrency </li></ul><ul><li>Readers see the state of the data before the modification occurred using Row versioning </li></ul><ul><li>Writers are unaffected by Readers because all read operations are accessing a saved version of the data </li></ul><ul><li>Readers do not block Writers </li></ul><ul><li>Writers do not block Readers </li></ul><ul><li>Writers can block Writers which generates a conflict and then it is up to the application to handle the conflict. </li></ul>
  5. 5. <ul><li>Isolation Levels </li></ul><ul><li>Read Uncommitted (Pessimistic Concurrency ) </li></ul><ul><li>Read Committed (Pessimistic Concurrency ) </li></ul><ul><li>Repeatable Read (Pessimistic Concurrency ) </li></ul><ul><li>Serializable (Pessimistic Concurrency ) </li></ul><ul><li>Snapshot ( Optimistic Concurrency ) </li></ul>
  6. 6. <ul><li>Isolation Levels 1 </li></ul><ul><li>Read Uncommitted </li></ul><ul><li>Readers read uncommitted data </li></ul><ul><li>Read operations do not acquire share locks </li></ul><ul><li>Read operations disregard shared locks acquired by other connections </li></ul><ul><li>Non-repeatable reads and phantoms are possible </li></ul>
  7. 7. <ul><li>Isolation Levels 2 </li></ul><ul><li>Read Committed </li></ul><ul><li>Readers can only read data that has been committed </li></ul><ul><li>Read operations acquire share locks </li></ul><ul><li>Share locks acquired by other processes are honored </li></ul><ul><li>Non-repeatable reads and phantoms are possible </li></ul><ul><li>If the database setting READ_COMMITTED_SNAPSHOT is ON then the READ Committed isolation level works in an Optimistic Concurrency Model </li></ul><ul><li>This is also known as READ Committed Snapshot </li></ul>
  8. 8. <ul><li>Isolation Levels 3 </li></ul><ul><li>Repeatable Read </li></ul><ul><li>Does not allow a non-repeatable read </li></ul><ul><li>Phantoms are possible </li></ul><ul><li>If a SELECT statement is reissued within the same transaction it is guaranteed to retrieve the same data as was retrieved in the previous SELECT but new rows can exist in the set of data retreived </li></ul>
  9. 9. <ul><li>Isolation Levels 4 </li></ul><ul><li>Serializable </li></ul><ul><li>Phantoms are not possible </li></ul><ul><li>If a SELECT statement is reissued within the same transaction it is guaranteed to retrieve the same data as was retrieved in the previous SELECT and no new rows can exist in the set of data </li></ul>
  10. 10. <ul><li>Isolation Levels 5 </li></ul><ul><li>Snapshot </li></ul><ul><li>Like in the Read Committed snapshot processes can read older versions of committed data if the current version is locked </li></ul>
  11. 11. Lock Modes http://msdn.microsoft.com/en-us/library/ms175519.aspx
  12. 12. <ul><li>Shared Locks </li></ul><ul><li>Acquired when data is read </li></ul><ul><li>Acquired on a table, page, index key or a row </li></ul><ul><li>Many processes can acquire shared locks on the same data </li></ul><ul><li>No process can acquire an exclusive lock on data that has a shared lock on it </li></ul><ul><ul><ul><li>unless the process requesting the exclusive lock is the same process holding the shared lock </li></ul></ul></ul>
  13. 13. <ul><li>Exclusive Locks </li></ul><ul><li>Acquired on data when the data is modified by any DML statement (INSERT, UPDATE, DELETE, SELECT INTO, MERGE) </li></ul><ul><li>Only one process at a given time can hold an exclusive lock on the same data resource </li></ul><ul><li>No locks of any kind can be acquired by a process if another process has the requested data resource exclusively locked </li></ul><ul><li>Exclusive locks are held until the end of the transaction </li></ul>
  14. 14. <ul><li>Update Locks </li></ul><ul><li>Acquired when a data modification statement executes but first there is a need to search for the data behind the scenes before the actual DML can take place </li></ul><ul><li>For example - An INSERT into a table with a clustered index requires first to search the data in order to find the correct position at which to locate the new row. During that search time an UPDATE locks is acquired for the sake of protecting the data </li></ul><ul><li>Using a hints a process can explicitly request an UPDATE lock </li></ul>
  15. 15. <ul><li>Intent Locks </li></ul><ul><li>Intent locks are a “mix” of the Locks modes previously discussed </li></ul><ul><li>Intent locks serve as meta data for </li></ul><ul><li>cases such as when a process needs to lock </li></ul><ul><li>a table it first needs to determine if a row </li></ul><ul><li>or a page of that table is already locked </li></ul>
  16. 16. <ul><li>Schema Locks </li></ul><ul><ul><ul><li>schema stability </li></ul></ul></ul><ul><ul><ul><li>schema modification </li></ul></ul></ul><ul><ul><ul><li>bulk update </li></ul></ul></ul><ul><ul><ul><li>schema stability locks and schema modification locks are acquired when a DDL statement such as ALTER TABLE is issued in order to protect the table structure while there are statements already issued that refer to the object in it’s current version, that is before the schema modification </li></ul></ul></ul><ul><ul><ul><li>bulk update lock is acquired when using the bcp (in) utility or when a BULK INSERT command is issued </li></ul></ul></ul>
  17. 17. <ul><li>Conversion Locks </li></ul><ul><li>Conversion locks are the result of a conversion from one lock mode to another </li></ul><ul><li>SIX </li></ul><ul><li>SIU </li></ul><ul><li>UIX </li></ul><ul><li>See example </li></ul>
  18. 18. <ul><li>Key Locks </li></ul><ul><li>In the Read Committed, Repeatable Read and Snapshot isolation levels Key Locks are used to lock the index keys </li></ul><ul><ul><ul><li>When a clustered index exists the data rows are the leaf level of the index and key locks are acquired </li></ul></ul></ul><ul><ul><ul><li>When the table is a heap key locks can be acquired on the non-clustered indexes and row locks (RID) for the actual data </li></ul></ul></ul><ul><li>In the Serializable isolation level key-range locks are used to lock the entire range(s) in order to prevent phantoms </li></ul><ul><li>See example </li></ul>
  19. 19. <ul><li>Key Locks Types </li></ul><ul><li>There are nine types of </li></ul><ul><li>key-range locks and each has a two-part name </li></ul><ul><ul><ul><li>The first part indicates the type of lock on the range of data between index keys </li></ul></ul></ul><ul><ul><ul><li>The second part indicates the type of lock on the key itself </li></ul></ul></ul>
  20. 20. <ul><li>Key Locks Details </li></ul><ul><li>RangeS-S </li></ul><ul><li>Shared lock on the range between keys; shared lock on the key at the end of the range </li></ul><ul><li>RangeS-U </li></ul><ul><li>Shared lock on the range between keys; update lock on the key at the end of the range </li></ul><ul><li>RangeIn-Null </li></ul><ul><li>Exclusive lock to prevent inserts on the range between keys; no lock on the keys themselves </li></ul><ul><li>RangeX-X </li></ul><ul><li>Exclusive lock on the range between keys; exclusive lock on the key at the end of the range </li></ul>
  21. 21. Key Locks Details 2 RangeIn-S Conversion lock created by S and RangeIn_Null lock RangeIn-U Conversion lock created by U and RangeIn_Null lock RangeIn-X Conversion of X and RangeIn_Null lock RangeX-S Conversion of RangeIn_Null and RangeS_S lock RangeX-U Conversion of RangeIn_Null and RangeS_U lock
  22. 22. <ul><li>Lock Duration </li></ul><ul><li>The duration a lock is held depends on the mode of the lock and the transaction isolation level in effect </li></ul><ul><li>In Read Committed and Snapshot shared locks are released as soon as the data is read and processed </li></ul><ul><li>In Repeatable Read and Serializable shared locks have the same duration as exclusive locks </li></ul><ul><ul><ul><li>Shared Locks are not released until the transaction is over </li></ul></ul></ul><ul><li>In all isolation levels exclusive locks are held until the end of the transaction </li></ul><ul><ul><ul><li>An update lock is also held until the end of the transaction </li></ul></ul></ul>
  23. 23. <ul><li>Lock Compatibility </li></ul><ul><li>Two locks are compatible if one lock can be granted while another lock on the same resource is already held by a different process </li></ul><ul><ul><ul><li>Two connections can hold a shared lock on the same row or page </li></ul></ul></ul><ul><li>If a lock requested for a resource is not compatible with a lock currently being held the requesting connection must wait for the lock </li></ul><ul><ul><ul><li>If a shared page lock is held on a page and another connection requests an exclusive page lock it will wait </li></ul></ul></ul>
  24. 24. Lock Compatibility matrix http://msdn.microsoft.com/en-us/library/ms186396.aspx
  25. 25. <ul><li>Lock Starvation </li></ul><ul><li>By examining the compatibility of locks not only with </li></ul><ul><li>processes granted but also processes waiting SQL </li></ul><ul><li>Server prevents lock starvation which can result when </li></ul><ul><li>requests for shared locks keep overlapping so that the </li></ul><ul><li>request for the exclusive lock can never be granted </li></ul><ul><li>P1 holds a shared page lock </li></ul><ul><li>P2 requests an exclusive page lock and waits </li></ul><ul><li>P3 requests a shared page lock that is </li></ul><ul><li>compatible with the shared page lock held by P1 </li></ul><ul><li>P3 waits for its shared page lock because </li></ul><ul><li>P2 is ahead of it in the lock queue with a </li></ul><ul><li>request (exclusive page) that is not compatible even though P3 request’s is compatible with P1 </li></ul>
  26. 26. <ul><li>Lock Escalation </li></ul><ul><li>SQL Server automatically escalates row, key, or page locks to table locks </li></ul><ul><li>SQL Server never escalates to page locks and it is not possible to lock just a single partition of a table or index. The result of a lock escalation is always a full table lock </li></ul><ul><ul><ul><li>SQL Server 2008 allows escalation at the partition level </li></ul></ul></ul><ul><li>A statement that acquires many individual row locks can be escalated to a table lock </li></ul><ul><li>It is lighter to acquire and maintain a single table lock than to hold many individual row locks </li></ul><ul><li>Lock escalation reduces locking overhead and keeps the system from running out of locks </li></ul>
  27. 27. <ul><li>Lock Escalation 2 </li></ul><ul><li>The number of locks held by a single statement on one object (index or heap) exceeds a threshold (~5000) </li></ul><ul><li>Memory consumed by lock resources exceeds 40 percent of the non-AWE (32-bit) or regular (64-bit) enabled memory </li></ul><ul><li>The server wide settings “Locks” effects the total memory available </li></ul><ul><ul><ul><li>0 = dynamically allocated </li></ul></ul></ul><ul><li>A Lock escalation attempt can fall due to lock compatibility and in this case SQL Server will continue and try to escalate the lock every time the transaction acquires another 1,250 locks on the same object </li></ul>
  28. 28. <ul><li>Lock Escalation 3 </li></ul><ul><li>The CREATE/ ALTER TABLE command can also have an impact on Locks and Lock escalation </li></ul><ul><li>ALLOW_ROW_LOCKS = { ON | OFF } </li></ul><ul><ul><ul><li>Row locks are allowed when accessing the index </li></ul></ul></ul><ul><li>ALLOW_PAGE_LOCKS = { ON | OFF } </li></ul><ul><ul><ul><li>Page locks are allowed when accessing the index </li></ul></ul></ul><ul><li>SQL Server 2008 only </li></ul><ul><ul><ul><li>SET ( LOCK_ESCALATION = { AUTO | TABLE | DISABLE } ) </li></ul></ul></ul><ul><ul><ul><li>This is a definition on a per table specific basis </li></ul></ul></ul>
  29. 29. <ul><li>Lock Escalation 4 </li></ul><ul><li>The CREATE/ ALTER INDEX command can also impact the way data is accessed and locked </li></ul><ul><li>When ALLOW_ROW_LOCKS = ON and ALLOW_PAGE_LOCK = ON: row level, page level and table level locks are allowed when you access the index </li></ul><ul><li>When ALLOW_ROW_LOCKS = OFF and ALLOW_PAGE_LOCK = OFF: only a table level lock is allowed when you access the index </li></ul><ul><li>The index &quot;IX_Parts&quot; (partition 1) on table &quot;Parts&quot; cannot be reorganized because page level locking is disabled </li></ul>
  30. 30. <ul><li>Disable Lock Escalation </li></ul><ul><li>DBCC TRACEON (12 11); </li></ul><ul><li>Completely disables lock escalation. </li></ul><ul><li>instructs SQL Server to ignore the memory </li></ul><ul><li>acquired by the lock manager up to the maximum </li></ul><ul><li>statically allocated lock memory </li></ul><ul><li>DBCC TRACEON (12 24); </li></ul><ul><li>Disables lock escalation based on the number of </li></ul><ul><li>locks acquired but allows escalation based on </li></ul><ul><li>memory consumption </li></ul><ul><ul><ul><li>It enables lock escalation when the lock manager acquires 40 percent of the statically allocated memory. </li></ul></ul></ul><ul><li>SQL Server generates an out-of-memory error when memory allocated to the lock manager exceeds the total allocated memory </li></ul><ul><li>DBCC TRACESTATUS (-1) </li></ul>
  31. 31. <ul><li>Control Locking using Hints </li></ul><ul><li>Effect the table referenced in the FROM clause </li></ul><ul><li>A hint applied to a Read operation mostly requires an explicit transaction to take effect </li></ul><ul><li>Any DML anyhow executes as an implicit transaction </li></ul><ul><li>SELECT * FROM T1 WITH (lock hint) … </li></ul><ul><li>DELETE FROM T1 WITH (lock hint) … </li></ul><ul><li>UPDATE T1 WITH (lock hint) … </li></ul><ul><li>INSERT T1 WITH (lock hint) … </li></ul>
  32. 32. <ul><li>Lock Hints </li></ul><ul><li>HOLDLOCK – serializes access same as SERIALIZABLE </li></ul><ul><li>UPDLOCK - acquires update page locks instead of shared page locks while data being read </li></ul><ul><li>TABLOCK - acquires a shared lock on the table </li></ul><ul><ul><ul><li>Useful when you know you'll escalate to a table lock </li></ul></ul></ul><ul><ul><ul><li>Useful when you need to get a snapshot of a table </li></ul></ul></ul><ul><ul><ul><li>Combined with HOLDLOCK exclusively locks the table </li></ul></ul></ul><ul><li>PAGLOCK - acquires shared page locks </li></ul><ul><ul><ul><li>Combined with XLOCK exclusively lock a page </li></ul></ul></ul>
  33. 33. <ul><li>Lock Hints 2 </li></ul><ul><li>TABLOCKX - exclusively locks the table </li></ul><ul><ul><ul><li>Equal to (TABLOCK , XLOCK ) </li></ul></ul></ul><ul><li>ROWLOCK - Acquires a shared row lock </li></ul><ul><li>NOLOCK - Allows uncommitted reads (dirty reads) </li></ul><ul><ul><ul><li>Shared locks are not acquired when data is read </li></ul></ul></ul><ul><ul><ul><li>Exclusive locks of other processes are disregarded </li></ul></ul></ul><ul><ul><ul><li>Equivalent to READUNCOMMITTED </li></ul></ul></ul><ul><li>READPAST - Locked rows are skipped (read past) </li></ul><ul><ul><ul><li>Applies only to transactions at READ COMMITTED isolation level </li></ul></ul></ul><ul><ul><ul><li>Reads past row-level locks only </li></ul></ul></ul>
  34. 34. <ul><li>Lock Hints 3 </li></ul><ul><li>XLOCK - Acquires an exclusive lock on all data processed by the statement </li></ul><ul><ul><ul><li>If combined with ROWLOCK, PAGLOCK, or TABLOCK, the exclusive locks apply to the appropriate level of granularity </li></ul></ul></ul><ul><li>READUNCOMMITTED | REPEATABLEREAD | SERIALIZABLE - imply the same locking mechanisms as when the transaction isolation level is set to the level of the same name </li></ul><ul><li>READCOMMITTED – Read operations comply with the rules for the READ COMMITTED isolation level by using either locking or row versioning </li></ul><ul><ul><ul><li>If the database option READ_COMITTED_SNAPSHOT is OFF SQL Server uses shared locks and releases them as soon as the read operation is completed </li></ul></ul></ul><ul><ul><ul><li>If the database option READ_COMMITTED_SNAPSHOT is ON, SQL Server does not acquire locks and uses row versioning </li></ul></ul></ul><ul><li>READCOMMITTEDLOCK - Read operations use the locking version of READCOMMITTED isolation (the default) </li></ul><ul><ul><ul><li>Regardless the database setting for the READ_COMMITTED_SNAPSHOT </li></ul></ul></ul><ul><ul><ul><li>Shared locks acquired when data is read and released as soon as the read operation is completed </li></ul></ul></ul>
  35. 35. <ul><li>Setting a Lock Timeout </li></ul><ul><li>By default there is no time out when waiting for a lock </li></ul><ul><li>Typically there is a time out defined in the client library </li></ul><ul><ul><ul><li>When a connection exceeds the time out defined at the client library and gets terminated you do not know the reason for the disconnection </li></ul></ul></ul><ul><ul><ul><li>LOCK_TIMEOUT defined at the database level clearly informs the cause to the disconnection in the form of error number 1222 </li></ul></ul></ul><ul><li>LOCK_TIMEOUT = 0 means that SQL Server does not wait at all for locks </li></ul><ul><li>Watch example </li></ul>
  36. 36. <ul><li>Resolving Blocking </li></ul><ul><li>Writer/Writer </li></ul><ul><li>Make write transactions as short as possible which reduces the time exclusive locks are held </li></ul><ul><li>Verify you do not work in an isolation level stricter than required </li></ul><ul><li>Verify required indexes exist </li></ul><ul><li>Perform mass operation in chunks to avoid lock escalation and possibly at off peak hours </li></ul>
  37. 37. <ul><li>Resolving Blocking </li></ul><ul><li>Reader/Writer </li></ul><ul><li>Use the READ UNCOMMITTED isolation level </li></ul><ul><ul><ul><li>Only where applicable and after a through thinking </li></ul></ul></ul><ul><li>Use Hints where applicable (always with care) </li></ul><ul><li>Verify you do not work in an isolation level stricter than required </li></ul><ul><li>Direct heavy Read operations to read data from another database or another server </li></ul><ul><li>Use the row-versioning–based Snapshot Isolation levels </li></ul>
  38. 38. ?
  39. 39. SQL Server BOL (Books Online) sys.dm_os_wait_stats http://msdn.microsoft.com/en-us/library/ms179984.aspx sys.dm_tran_locks http://msdn.microsoft.com/en-us/library/ms190345.aspx Inside Microsoft SQL Server 2005 http://www.insidesqlserver.com/thebooks.html

×