Presentation is loading. Please wait.

Presentation is loading. Please wait.

Locking, Blocking, Latching, OH MY! SQL SERVER:. About me  16 years of SQL Server experience  SQL 6.5, 7.0, 2000, 2005, 2008, 2008 R2, 2012, 2014 

Similar presentations


Presentation on theme: "Locking, Blocking, Latching, OH MY! SQL SERVER:. About me  16 years of SQL Server experience  SQL 6.5, 7.0, 2000, 2005, 2008, 2008 R2, 2012, 2014 "— Presentation transcript:

1 Locking, Blocking, Latching, OH MY! SQL SERVER:

2 About me  16 years of SQL Server experience  SQL 6.5, 7.0, 2000, 2005, 2008, 2008 R2, 2012, 2014  6+ years supporting SQL Server as a Senior PFE at Microsoft Corporation  Austin Data Pros (Austindatapros.com)

3 What’s in this lesson?  Explain the basic concepts of locking, including lock mode, lock resources, and lock compatibility.  Define the impact of different transaction isolation levels.  Describe the features of transaction mode.  Troubleshoot and resolve blocking and locking issues.

4 Lesson 1: Locking Concepts and Mechanics o Concepts o Different lock modes o Lock escalation

5 Locking granularity  Many items can be locked in SQL Server  Databases  Schema  Objects  Some objects can be locked at different levels of granularity  SQL Server will automatically choose the granularity of the lock based on the estimated cost  Multiple levels of granularity are grouped into a lock hierarchy

6 Lock Modes  Shared (S)  Generally caused by “Select” operations  Exclusive (X)  Generally caused by “DML” operations  Update (U)  Converts to an exclusive (X)  Schema (Sch-S, Sch-M)  Generally seen when compiling queries or modifying the schema  Intent (IS, IX, …)

7 Intent Locks  Used to establish a lock hierarchy  Purposes  To prevent other transactions from modifying the higher-level resource in a way that would invalidate the lock at the lower level.  To improve the efficiency of the Database Engine in detecting lock conflicts at the higher level of granularity. DISASTER!

8 Lock Escalation  Used to lower the number of locks taken by a transaction  Lock manager attempts to replace one transaction's many row or page locks with a single table-level lock  Escalation never converts row locks to page locks  Lock escalation happens based on Lock escalation Thresholds  Lock de-escalation never occurs  Lock Escalation behavior can be controlled  At the server level via Trace Flag 1211  At the table level via ALTER TABLE in SQL 2008 and later AUTO TABLE DISABLE

9 Lock Escalation Threshold Lock escalation can be triggered in any of the following situations:  When a single Transact-SQL (T-SQL) statement acquires at least 5,000 locks on a single table or index.  When 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.

10 Latches and Spinlocks  Latches are lightweight synchronization mechanisms that protect access to in-memory structures  Spinlocks are like latches but burn CPU instead of yielding the scheduler  SYS.DM_OS_LATCH_STATS  SYS.DM_OS_SPINLOCK_STATS

11 Lesson 2: Viewing lock information o Dynamic Management Views (DMV) o Tools

12 Viewing Blocking by Using DMVs  sys.dm_tran_locks  Replaces syslockinfo and sp_lock  Each row has information about both the resource and the request  request_status = WAIT implies blocking  sys.dm_os_waiting_tasks  blocking_session_id > 0  sys.dm_exec_requests  status = suspended  blocking_session_id > 0  sys.dm_exec_sessions  Join with each of the above for session details (join on session_id)

13 Viewing Blocking using tools  Activity Monitor  Performance Monitor  SQLServer: General Statistics\Processes Blocked  Can compare blocking with hardware latency and such…

14 Lesson Review  What DMV shows blocking?  How can you determine what locks are held for a specific transaction? 14 Microsoft Confidential

15 Lesson 3: Isolation Levels o Different isolation levels (ANSI and non-ANSI) o How isolation levels affect locking behavior o Row-versioning

16 ANSI SQL Isolation Levels  True isolation is expensive in terms of concurrency  Trade-off between correctness and concurrency  ANSI SQL defines Four distinct isolation levels

17 Locking—Read Committed Scan  Shared locks are released behind the scan, allowing other transactions to update rows  Lock will not be released until lock on next page/row is acquired  Rescan may encounter modified or deleted rows 7 34691014 1 437346 7643

18 Locking—Repeatable Read Scan  Shared locks are retained on scanned rows that meet the search criteria  Rescan may see new rows in range, but scanned rows will not change 7 346910141 43 7 3 4 6

19 Locking—Serializable Read Scan  Shared locks are retained on scanned rows and on scanned ranges  Prevents update and phantom insertions into scanned range 3467910141 3467

20 Key Range Locking PPrevents phantom records to support serializable transactions. LLock sets of rows specified by the query predicate ““Where salary between 30,000 and 50,000” LLocks data that does not exist: IIf predicate doesn’t return any rows the first time, it shouldn’t return any on subsequent scans AA range is an open interval between instances of rows at the leaf level of an index TTo lock a key-range (ki,ki+1], associate a lock with the key value ki+1 10000 20000 60000 80000

21 Locking Hints  Can be specified using the SELECT, INSERT, UPDATE, and DELETE statements  Direct SQL Server to the type of locks to be used  Granularity hints: ROWLOCK, PAGLOCK,TABLOCK  Isolation LEVEL hints: HOLDLOCK, NOLOCK READCOMMITTED, REPEATABLEREAD, SERIALIZABLE, READUNCOMMITTED, READCOMMITTEDLOCK  UPDLOCK: Use update lock rather than shared lock when reading  XLOCK: Use exclusive lock instead  READPAST: Will “skip” rows that are currently locked  Used when a finer control of the types of locks acquired on an object is required  Override the current transaction-isolation level for the session

22 Application locks  Application locks place a lock on a “resource”  SP_GETAPPLOCK  SP_RELEASEAPPLOCK  DOES NOT HANDLE DEADLOCKS!

23 Lesson 4: Blocking o Blocking o Identification o Different causes of blocking o Compile blocking o Blocking Avoidance

24 Long-running queries or transactions  Statement may be waiting on resources  A status of runnable indicates CPU waits  A status of suspended indicates other resource waits wait_type column will indicate which resource blocking_session_id > 0 means the session is being blocked, keep following the chain to find the head blocker  May have high values for the following columns  cpu_time/total_elapsed_time  reads/writes  logical_reads  granted_query_memory  Can improve by  Optimizing the query to shorten its duration  Breaking the transaction up into smaller transactions  Changing the timing to prevent concurrency with other queries  Implementing Row versioning

25 Inappropriate transaction or transaction- isolation level  May appear as a long-running transaction, look for the same items as the previous slide  Look for hints such as HOLDLOCK or specific isolation level settings such as REPEATABLE READ or SERIALIZABLE  Look for excessive locks in sys.dm_tran_locks or specialized locks such as Range locks  Can improve by  Breaking the transaction up into smaller transactions  Changing the timing to prevent concurrency with other queries  Reducing the isolation level to the lowest level required by the application  Considering Row versioning

26 Orphaned Transactions  Session will not be in sys.dm_exec_requests  Status will be sleeping in sys.dm_exec_sessions  Open_transaction_count > 0  sys.dm_tran_session_transactions  sys.dm_exec_requests  sys.dm_exec_sessions

27 Lesson 5: Trial by fire!! o Use your skills to fix the problems

28 Case of the crashing export  Users are complaining that the bulk export process fails at random. Usually a second attempt will allow the export to complete.

29 Case of the billing bullies Users in the 24x7 call center are using an application to lookup orders. The billing department has the ability to process invoices by state. Until recently, the two were able to run together without delays; however, within the few weeks the call center has been unable to lookup data while the invoices are running.

30 Case of the crashing client  Epic Fail, LLC has a voucher redemption process. Client machines have crashed and users are unable to redeem their vouchers.  You are charged with resolving this issue  Yes, the CIO and your manager are standing over your shoulder…

31 Case of the Slow Application  An update was made to the “Processing” application after users complained of deadlocks. Now users are complaining of HUGE delays in the application.

32 Q & A?


Download ppt "Locking, Blocking, Latching, OH MY! SQL SERVER:. About me  16 years of SQL Server experience  SQL 6.5, 7.0, 2000, 2005, 2008, 2008 R2, 2012, 2014 "

Similar presentations


Ads by Google