Presentation is loading. Please wait.

Presentation is loading. Please wait.

Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks

Similar presentations


Presentation on theme: "Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks"— Presentation transcript:

1 Locks, Blocks & Isolation Oh My!

2 About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks Email: tateke@yahoo.comtateke@yahoo.com Twitter: @keith_tate BLOG: http://www.thesqlchef.comhttp://www.thesqlchef.com

3 Objectives Brief overview of the A.C.I.D properties What is Isolation? What are the two ways of accomplishing Isolation In-depth discussion of the different Isolation levels in SQL Server. Demos, Demos, and more Demos

4 A.C.I.D Atomic – A transaction has to be “All or Nothing” Every statement is a transaction (Implicit or Explicit) Write Ahead Transaction Log Consistent – The committed data must adhere to all defined rules Unique, Foreign Key, Check Constraints Isolation – Behavior of concurrent transactions Locking Durable – Once committed, guarantee the data must survive Write Ahead Transaction Log

5 Isolation Prevent conflicts between concurrent transactions Everything statement operates within a transaction The only A.C.I.D property that can be “adjusted” by the end user ANSI SQL-92 defines isolation in terms of read phenomena: Lost Updates (P0) – Not possible in SQL Server Uncommitted Dependency (Dirty Read) (P1) Inconsistent Analysis (Nonrepeatable Read) (P2) Phantom Reads (P3)

6 Concurrency Control From BOL: Pessimistic concurrency control (Locking) – A system of locks prevents users from modifying data in a way that affects other users. After a user performs an action that causes a lock to be applied, other users cannot perform actions that would conflict with the lock until the owner releases it. This is called pessimistic control because it is mainly used in environments where there is high contention for data, where the cost of protecting data with locks is less than the cost of rolling back transactions if concurrency conflicts occur. Optimistic concurrency control (Multiversioning) - In optimistic concurrency control, users do not lock data when they read it. When a user updates data, the system checks to see if another user changed the data after it was read. If another user updated the data, an error is raised. Typically, the user receiving the error rolls back the transaction and starts over. This is called optimistic because it is mainly used in environments where there is low contention for data, and where the cost of occasionally rolling back a transaction is lower than the cost of locking data when read. Reference: http://bit.ly/P4a02l

7 LOCKS

8 Lock Modes Shared (S) – Read Operations that do not change data Update (U) – Avoids Deadlock Issue. Only one transaction can obtain an Update lock to a resource at a time Exclusive (X) – Used for Insert/Update/Delete operations. Intent (I) – Used to establish a lock hierarchy (IS) – Intent Shared (IX) – Intent Exclusive (SIX) – Shared with Intent Exclusive (IU) – Intent Update (SIU) – Shared Intent Update (UIX) – Update Intent Exclusive

9 Lock Modes (Cont.) Schema (Sch-M) – Schema Modification - Used during DDL operation (Sch-S) – Schema Stability – Used during compile and execution of queries Bulk Update (BU) – Used during bulk load operations. Allows multiple threads to bulk load data while preventing other operations (non-bulk loading) access to the table. Can be set by using either: TABLOCK hint Table lock on bulk load table option Key Range – Used by the serializable isolation level – locks a range of rows

10 Locking You can control locking (but not recommended) by using the following table hints: ROWLOCK PAGLOCK TABLOCK To view current locks use: sys.dm_tran_locks

11 ISOLATION LEVELS – PESSIMISTIC CONCURRENCY

12 READ UNCOMMITTED Read Phenomena: Uncommitted Dependency (Dirty Read) One transaction can read data from a row from a different transaction that has not yet committed Transaction level: SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; Table level: WITH (NOLOCK) Locking Behavior : No shared locks taken Also, no exclusive locks are honored

13 READ COMMITTED Read Phenomena: Inconsistent Analysis (Nonrepeatable read) Default isolation level in SQL 7.0 - 2012 One Transaction can read a row and if another transaction modifies the row the first transaction will read the new value. Transaction Level: SET TRANSACTION ISOLATION LEVEL READ COMMITTED Table Level: WITH (READCOMMITTED) – Will use RCSI WITH (READCOMMITTEDLOCK) – Will ignore RCSI Locking Behavior: Short lived share locks on a row-by-row basis

14 READ COMMITTED Author: Craig Freedman. © 2007 Microsoft. All Rights Reserved

15 REPEATABLE READS Read Phenomena: Phantom reads One transaction that reads a set of data may see additional records if it reads the data again. (Must use a search condition) Transaction Level: SET TRANSACTION ISOLATION LEVEL REPEATABLE READ Table Level: WITH (REPEATABLEREAD) Locking Behavior: Share locks held until the end of the transaction

16 REPEATABLE READS Author: Craig Freedman. © 2007 Microsoft. All Rights Reserved

17 SERIALIZABLE Read Phenomena: None Provides the effect of all transactions running in serial. As if each transaction runs until completion before the next runs Transaction Level: SET TRANSACTION ISOLATION LEVEL SERIALIZABLE Table Level: WITH (SERIALIZABLE) WITH (HOLDLOCK) Locking Behavior: Holds range locks on all data requested by the transaction (and more).

18 ISOLATION – OPTIMISTIC CONCURRENCY

19 Concurrency Control From BOL: Pessimistic concurrency control (Locking) – A system of locks prevents users from modifying data in a way that affects other users. After a user performs an action that causes a lock to be applied, other users cannot perform actions that would conflict with the lock until the owner releases it. This is called pessimistic control because it is mainly used in environments where there is high contention for data, where the cost of protecting data with locks is less than the cost of rolling back transactions if concurrency conflicts occur. Optimistic concurrency control (Multiversioning) - In optimistic concurrency control, users do not lock data when they read it. When a user updates data, the system checks to see if another user changed the data after it was read. If another user updated the data, an error is raised. Typically, the user receiving the error rolls back the transaction and starts over. This is called optimistic because it is mainly used in environments where there is low contention for data, and where the cost of occasionally rolling back a transaction is lower than the cost of locking data when read. Reference: http://bit.ly/P4a02l

20 Snapshot In SQL Server SQL Server (2005 and newer) implements optimistic concurrency using snapshot isolation Snapshot Isolation comes in two different flavors: Read Committed Snapshot Isolation & Snapshot Isolation Uses version stores inside TEMPDB to accomplish this There is no read phenomena associated with snapshot isolation, but they are different than Serializable Table hints override both of these options. Think (NOLOCK) You can avoid Deadlocks, but watch out for Update conflicts

21 READ COMMITTED SNAPSHOT ISOLATION Read Phenomena: None Makes the default isolation level READ COMMITTED SNAPSHOT ISOLATION Enable: ALTER DATABASE databasename SET READ_COMMITTED_SNAPSHOT ON; Become activated immediately!* All queries that run under the READ COMMITTED isolation level will now use row versioning.

22 SNAPSHOT ISOLATION Read Phenomena: None Allows the user to run queries in the SNAPSHOT isolation level Enable: ALTER DATABASE databasename SET ALLOW_SNAPSHOT_ISOLATION ON; The user must set the isolation level in order for it to take effect. This must be done for every connection you want to use Snapshot isolation SET TRANSACTION ISOLATION LEVEL SNAPSHOT;

23 Objectives Brief overview of the A.C.I.D properties What is Isolation? What are the two ways of accomplishing Isolation In-depth discussion of the different Isolation levels in SQL Server.

24 Q & A

25 Resources Isolation Levels in SQL Server Craig Freedman’s Blog Tag Isolation Level Transaction Phenomena Series ACID Properties By Example Lock Compatibility Next Level: The Transaction Concept Why Banks Are BASE Not ACID – Availability Is Revenue

26 Thank You! Please feel out a quick survey to let me know if you enjoyed this presentation: http://www.surveymonkey.com/s/QZF8VD6


Download ppt "Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks"

Similar presentations


Ads by Google