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 

Slides:



Advertisements
Similar presentations
Database Systems (資料庫系統)
Advertisements

Chapter 16 Concurrency. Topics in this Chapter Three Concurrency Problems Locking Deadlock Serializability Isolation Levels Intent Locking Dropping ACID.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Managing Hierarchies of Database Elements (18.6) 1 Presented by Sarat Dasika (114) February 16, 2012.
Concurrency Control II. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Physical Design.
Concurrency Control Part 2 R&G - Chapter 17 The sequel was far better than the original! -- Nobody.
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Lock Tuning. overview © Dennis Shasha, Philippe Bonnet 2001 Sacrificing Isolation for Performance A transaction that holds locks during a screen interaction.
Module 20 Troubleshooting Common SQL Server 2008 R2 Administrative Issues.
1 Data Concurrency David Konopnicki 1997 Revised by Mordo Shalom 2004.
Data and Database Administration Chapter 12. Outline What is Concurrency Control? Background Serializability  Locking mechanisms.
Transactions and Locking Rose-Hulman Institute of Technology Curt Clifton.
Transactions and Locks
Managing Hierarchies of Database Elements (18.6) -Neha Saxena Class Id: 214.
Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks.
Concurrency Control Managing Hierarchies of Database Elements (18.6) 1 Presented by Ronak Shah (214) March 9, 2009.
Transaction Management and Concurrency Control
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
GRANULARITY OF LOCKS IN SHARED DATA BASE J.N. Gray, R.A. Lorie and G.R. Putzolu.
9 Copyright © 2009, Oracle. All rights reserved. Managing Data Concurrency.
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
Microsoft SQL Server Administration for SAP Performance Monitoring and Tuning.
Managing Concurrency in Web Applications. DBI 2007 HUJI-CS 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses.
Transaction Management and Concurrency Control
Chapter 9 Overview  Reasons to monitor SQL Server  Performance Monitoring and Tuning  Tools for Monitoring SQL Server  Common Monitoring and Tuning.
Transactions and Locks Lesson 22. Skills Matrix Transaction A transaction is a series of steps that perform a logical unit of work. Transactions must.
Transaction Concurrency Control and Locking
Managing Transaction and Lock Vu Tuyet Trinh Hanoi University of Technology 1.
Transactions and Exception Handling Eric Allsop SQLBits 6 th October 2007.
1099 Why Use InterBase? Bill Todd The Database Group, Inc.
Oracle Locking Michael Messina Principal Database Analyst Indiana University.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
Department of Computer Science and Engineering, HKUST 1 More on Isolation.
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
9 Copyright © 2007, Oracle. All rights reserved. Managing Data and Concurrency.
DB2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Session Plan Introduction to Concurrency Control Different types.
Module 11 Creating Highly Concurrent SQL Server® 2008 R2 Applications.
8 Copyright © 2005, Oracle. All rights reserved. Managing Data.
Transactions and Locks A Quick Reference and Summary BIT 275.
Random Logic l Forum.NET l Transaction Isolation Levels Forum.NET Meeting ● Nov
Enterprise Database Administration & Deployment SIG ▪ 313M ▪ Sept 29, 2005 ▪ 10:15 AM SQL Server 2005 Performance Diagnosis and Tuning using SQL Tools.
Session 1 Module 1: Introduction to Data Integrity
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
1 Copyright © 2005, Oracle. All rights reserved. Following a Tuning Methodology.
Module 11: Managing Transactions and Locks
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Module 14: Managing Transactions and Locks. Overview Introducing Transactions and Locks Managing Transactions Understanding SQL Server Locking Architecture.
18 September 2008CIS 340 # 1 Last Covered (almost)(almost) Variety of middleware mechanisms Gain? Enable n-tier architectures while not necessarily using.
In this session, you will learn to: Implement triggers Implement transactions Objectives.
Deadlocks 3.0. Final Edition. Everything that developer needs to know Denis Reznik Microsoft SQL Server MVP Director of R&D at Intapp Kyiv.
Does the Optimistic Concurrency resolve your blocking problems Margarita Naumova, SQL Master Academy.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks
SQLintersection Locks, Blocks, and Deadlocks Oh My! Randy Knight Wednesday, 2:15-3:15.
Concurrency Control Managing Hierarchies of Database Elements (18.6)
Transaction Management and Concurrency Control
Concurrency Control More !
Tech Ed North America /23/2018 1:32 AM Required Slide
Isolation Levels Understanding Transaction Temper Tantrums
J.N GRAY R.A LORIE IBM RESEARCH LAB, SAN JOSE, CALIFORNIA
Transactions, Locking and Query Optimisation
මොඩියුල විශ්ලේෂණය SQL Server Waits. Tables රැසක් එකට එකතු කිරීම.
Chapter 15 : Concurrency Control
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Transactions and Concurrency
Sioux Falls, SD | Hosted by (605) SQL
About Wolf DBA for over 19 years At RDX for nearly 9
Isolation Levels Understanding Transaction Temper Tantrums
Module 13: Creating Highly Concurrent SQL Server 2012 Applications
Inside the Database Engine
Presentation transcript:

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  6+ years supporting SQL Server as a Senior PFE at Microsoft Corporation  Austin Data Pros (Austindatapros.com)

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.

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

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

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, …)

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!

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

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.

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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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.

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…

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.

Q & A?