Presentation is loading. Please wait.

Presentation is loading. Please wait.

Locks, Blocks, and Deadlocks; Tame the Sibling Rivalry SQL Server Family Management ~ Wolf ~ This template can be used as a starter file for presenting.

Similar presentations


Presentation on theme: "Locks, Blocks, and Deadlocks; Tame the Sibling Rivalry SQL Server Family Management ~ Wolf ~ This template can be used as a starter file for presenting."— Presentation transcript:

1 Locks, Blocks, and Deadlocks; Tame the Sibling Rivalry SQL Server Family Management
~ Wolf ~ This template can be used as a starter file for presenting training materials in a group setting. Sections Right-click on a slide to add sections. Sections can help to organize your slides or facilitate collaboration between multiple authors. Notes Use the Notes section for delivery notes or to provide additional details for the audience. View these notes in Presentation View during your presentation. Keep in mind the font size (important for accessibility, visibility, videotaping, and online production) Coordinated colors Pay particular attention to the graphs, charts, and text boxes. Consider that attendees will print in black and white or grayscale. Run a test print to make sure your colors work when printed in pure black and white and grayscale. Graphics, tables, and graphs Keep it simple: If possible, use consistent, non-distracting styles and colors. Label all graphs and tables.

2 About Wolf DBA for nearly 17 years
At RDX for over 6 years. “Manager – SQL Server Performance Tuning” Works with a variety of clients and challenges Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

3 Resources to use: #sqlsatChattanooga #sqlhelp #sqlserver
Add slides to each topic section as necessary, including slides with tables, graphs, and images. See next section for sample table, graph, image, and video layouts. #sqlsatChattanooga #sqlhelp #sqlserver

4

5 What we will focus on 1 2 3 ACID Isolation Levels/Concurrency Issues
Locking Explained 3 Deadlock Defined 4 This is another option for an Overview slide.

6 A.C.I.D ACID Use a section header for each of the topics, so there is a clear transition to the audience.

7 A C I Atomicity – All or None
Consistency – All intended to be affected, is affected C Isolation – Clean your own room I Durability – Your daughter spilled coke onto database, data still there D This is another option for an Overview slide.

8 Isolation Levels In SQL Server, and other RDBMS, the engine implements the configured isolation level which will affect the ACIDity when reading data. The isolation level will isolate the process and protect it from other transactions. To do this, locking is required. The locking is determined by the isolation level. Low levels of isolation will give high levels of concurrency and shared use of the data, but can result in lower levels of consistency with dirty or phantom reads. SQL Server will use isolation levels to command the read behavior of the database/transaction. Use a section header for each of the topics, so there is a clear transition to the audience.

9 Read Uncommitted Read data from other transactions yet completed
Dirty Reads Uncommitted updates \

10 Read Committed SQL Server Default
Prevents reading Data from uncommitted transactions Can result in Phantom Reads/Repeatable Reads

11 Repeatable Read This will ensure that if data is reread within a transaction, that the data does not change No transaction can modify the data until the transaction reading the data is complete This comes at the cost that all shared locks in the read will hold until the transaction is completed This will eliminate Dirty and Non-Repeatable reads, but Phantom reads may still occur.

12

13 Serializable This is putting the transactions in a single line.
This is the most consistent and best way to ensure for ACIDity. A read/write lock will be issued for all of the records affected by the transaction This includes Repeatable Read isolation and will also eliminate Phantom reads.

14

15 Snapshot This will read the data as it is at the beginning of the transaction. This will come at a high cost to tempdb as the data for the snapshot is stored in tempdb. This will eliminate Dirty, Phantom, Non-Repeatable Read and Lost updates Introduces a 14 byte addition to each row on the page. Could cause increased page splits, causing fragmentation.

16 Concurrency issues Lost Updates Dirty Reads Non-Repeatable Reads
Phantom Reads Lost Updates – Transaction A changes a value, then commits. Transaction B then also changes that value and commits. Transaction A then reads the data after Transaction B commits. Transaction A is expecting the first value, but obtains the second Dirty Reads – Reading data when another process is changing the data and the original process is reading uncommitted data. Non-repeatable Reads – This occurs when a process reads the same data multiple times within a session. Between reads of the data, another process may change the data and therefore different values are returned. Phantom Reads – This is similar to Non- Repeatable reads, but instead of changing(updating) the data, another process adds or removes records between the reads of the same data during a process.

17

18 Locking Explained Locking is a mechanism used by the SQL Server Database Engine to synchronize access by multiple users to the same piece of data at the same time.  Locks are managed internally by a part of the Database Engine called the lock manager. Each transaction will lock levels of resources such as rows, pages or tables for which the transaction is dependent.  The transaction will release the lock when it either commits or rolls back.  The amount of time the lock is held is dependent on the isolation level. The two basic types of locks are read locks and write locks. Use a section header for each of the topics, so there is a clear transition to the audience.

19 Locking Explained Use a section header for each of the topics, so there is a clear transition to the audience.

20 Shared Read Lock Shared -While a shared lock is being held other transactions can read but cannot modify locked data.  The lock is released after the locked data has been read unless the isolation level is at or higher than Repeatable Read or a locking hint such as READCOMMITED or READCOMMITTEDLOCK is used.

21 Concurrent Read Lock Concurrent - This is when you read data using read data held by exclusive lock only by specifying a NOLOCK hint or using a read uncommitted isolation level

22 Update Write Lock Update - Update locks are a mix of shared and exclusive locks. When an update statement is executed, SQL Server has to find the data it wants to modify first, so to avoid lock conversion deadlocks an update lock is used. Only one update lock can be held on the data at one time, similar to an exclusive lock. The update lock itself can't modify the underlying data, when the data is modified, it is transferred to an exclusive lock.

23 Exclusive Write Lock Exclusive - Exclusive locks are used to lock data being modified by one transaction thus preventing modifications by other concurrent transactions.  A transaction always gets an exclusive lock on any data it modifies, and holds that lock until the transaction completes, regardless of the isolation level set for that transaction.

24 Locking Example Use a section header for each of the topics, so there is a clear transition to the audience.

25 Lock Granularity/Escalation
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. Lock granularity consists of DATABASE, TABLE, PAGE and ROW locks.  As soon as you connect to a SQL Server database, a shared lock is issued on the database level itself. This prevents another user from performing an action such as "DROP DATABASE" while there is a connection to it. SQL Server will perform it's lock granularity from top down. First, it will check if a table lock is necessary, then page then row. An Intent(Shared or Exclusive), dependent if the operation is read or write, is issued on the table and page and a shared lock on the row. If the amount of data necessary is more than a row and on a page. Once SQL Server needs more than 5,000 row locks, it will escalate(by default) to a table lock. 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. Lock granularity consists of DATABASE, TABLE, PAGE and ROW locks.  As soon as you connect to a SQL Server database, a shared lock is issued on the database level itself. This prevents another user from performing an action such as "DROP DATABASE" while there is a connection to it. SQL Server will perform it's lock granularity from top down. First, it will check if a table lock is necessary, then page then row. An Intent(Shared or Exclusive), dependent if the operation is read or write, is issued on the table and page and a shared lock on the row. If the amount of data necessary is more than a row and on a page. Once SQL Server needs more than 5,000 row locks, it will escalate(by default) to a table lock.

26 ALTER TABLE MyTable SET LOCK_ESCALATION = <AUTO, TABLE, DISABLE>
You can alter these defaults by either using query hints such as ROWLOCK, PAGLOCK or TABLOCK. You can also alter the lock escalation of each table by using the following: ALTER TABLE MyTable SET LOCK_ESCALATION = <AUTO, TABLE, DISABLE> This is generally not recommended as it is best to allow the Database Engine to escalate locks accordingly. Lower level locks increase concurrency, but consume more memory Add slides to each topic section as necessary, including slides with tables, graphs, and images. See next section for sample table, graph, image, and video layouts.

27

28 Deadlocking Use a section header for each of the topics, so there is a clear transition to the audience.

29 Deadlocking Use a section header for each of the topics, so there is a clear transition to the audience.

30 Which child is easiest to deal with when they don’t get what they want?
The Database Engine will check for deadlocks in intervals of 5 seconds as a default.  The transaction that is chosen as the victim is the one that either has the lower DEADLOCK_PRIORITY which may be set on the session explicitly or the transaction that is least expensive to rollback. Add slides to each topic section as necessary, including slides with tables, graphs, and images. See next section for sample table, graph, image, and video layouts.

31 Reporting on Deadlocks
Use a section header for each of the topics, so there is a clear transition to the audience.

32 Trace Flag There are various ways to report on deadlocks.  The simplest is the using the trace flag  This will record the deadlock in the SQL Server Error Log in XML format.  To turn the trace on or off, use the following. DBCC TRACEON(1222,-1); DBCC TRACEOFF(1222,-1); Once the trace flag is set, when a deadlock occurs, you can read from the SQL Server Error Log Add slides to each topic section as necessary, including slides with tables, graphs, and images. See next section for sample table, graph, image, and video layouts.

33 Default Extended Events
Another way to report on deadlocks is to use the default extended event from the ring buffer.  I tend to avoid the ring buffer as I find it less than dependable. Add slides to each topic section as necessary, including slides with tables, graphs, and images. See next section for sample table, graph, image, and video layouts.

34 @SQLWAREWOLF Custom Reporting
I have written a full deadlock reporting structure for which I am going to have here to share.  To be clear, these scripts and procedures are written by myself and may be shared with that copyright knowledge.  They are AS IS and no warranty or support is included.  I am NOT liable for ANY damages using these scripts may cause.  Use at your own risk and always test THOROUGHLY on a development system before implementing in production. This process will use a defined extended event to record deadlocks to a configurable disk location.  A job will read the events and parse the XML into a reporting structure.  A final stored procedure is then ran to see the results of the deadlock report. The deadlock analysis also has the capability to obfuscate the results.  What that means is that if the same query that is the victim 5 times has only a string difference, the report should show the result only once. Add slides to each topic section as necessary, including slides with tables, graphs, and images. See next section for sample table, graph, image, and video layouts.

35 Deadlocking Example Use a section header for each of the topics, so there is a clear transition to the audience.

36 Avoiding Deadlocks Use a section header for each of the topics, so there is a clear transition to the audience.

37 References: to-locking-in-sql-server#sthash.ujDqLUPK.dpuf 02/20/concurrency-series-basics-of-transaction- isolation-levels.aspx us/library/jj856598(v=sql.110).aspx Add slides to each topic section as necessary, including slides with tables, graphs, and images. See next section for sample table, graph, image, and video layouts.

38 Microsoft Engineering Excellence
Questions? Microsoft Confidential


Download ppt "Locks, Blocks, and Deadlocks; Tame the Sibling Rivalry SQL Server Family Management ~ Wolf ~ This template can be used as a starter file for presenting."

Similar presentations


Ads by Google