Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Locking and Blocking with SQL Server<br />Choosing a concurrency Model –<br />Optimistic or Pessimistic<br />Vinod Kumar M...
Why Study Concurrency?<br />Contention Degrades Performance<br />Most Tuning Tools Don’t Consider Concurrency Issues<br />...
Concurrency Models<br />Pessimistic Concurrency<br />Preventative approach<br />Limited concurrent access<br />Uses lockin...
Transactions in SQL Server <br />Transaction Basics<br />Preventable Phenomena <br />
Transaction Basics<br />Allow Correctness of Operations To Be Verified<br />Exhibit Four Properties (ACID):<br />Atomicity...
PreventablePhenomena<br />Lost Updates<br />Dirty Reads<br />Non-Repeatable Reads<br />Phantoms<br />
Read X (10)<br />Compute X=X+15<br />(25)<br />Write X<br />Compute X=X+10<br />(20)<br />Read X (10)<br />Write X<br />Cl...
Read X (10)<br />Compute<br />X=X+15<br />(25)<br />Rollback<br />Use value ofX that was nevercommitted to DB<br />Read X ...
Read X (10)<br />Compute X=X+15<br />(25)<br />Commit<br />Read X (10)<br />Read X (25)<br />Non-Repeatable Read<br />Tran...
Jones,6<br />Insert<br />The “Phantom” Problem<br />Transaction’s input set defined  by a predicate<br />Transaction shoul...
IsolationLevels<br />True Isolation Is Expensive <br />Trade off between correctness and concurrency<br />ANSI SQL Defines...
SQL2008IsolationLevels<br /><ul><li>SET TRANSACTION ISOLATION LEVEL …</li></ul>Phenomena Allowed<br />
Locking Decision<br />Current Activity<br />Isolationlevel<br />Scan type(Range, Table, Probe)<br />Locking strategy(Table...
Pessimistic Concurrency Control<br />Aspects of Locking<br />Blocking<br />Controlling Locking<br />
Aspects of Locking<br />Type of Lock<br />Duration of Lock<br />Granularity of Lock<br />
Blocking<br />Occurs when one process requests a lock on the same resource held by another process in an incompatible mode...
Deadlock<br />What is Deadlock?<br />Handling Deadlock<br />
Process 1<br />X<br />Resource A<br />Resource B<br />X<br />Process 2<br />WhatisDeadlock?<br />Two Processes Mutually Bl...
Blocked Process Threshold<br />Generates an event when a task(s) blocked for greater than ‘threshold’<br />‘Threshold’ is ...
Demo: Blocked Process Threshold<br />
Summary<br />Concurrency Models<br />Transactions in SQL Server <br />Isolation Levels<br />Pessimistic Concurrency Contro...
Additional Resources<br />Inside SQL Server 2005:        The Storage Engine<br />(Microsoft Press, available now)<br />Blo...
Upcoming SlideShare
Loading in …5
×

Choosing A Concurrency Model, Optimistic Or Pessimistic

3,987 views

Published on

PPT used for UG Event on Locking and Blocking with SQL Server

Published in: Education, Technology
  • Follow the link, new dating source: ❤❤❤ http://bit.ly/2F90ZZC ❤❤❤
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Sex in your area is here: ❶❶❶ http://bit.ly/2F7hN3u ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating direct: ❤❤❤ http://bit.ly/2F90ZZC ❤❤❤
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating for everyone is here: ♥♥♥ http://bit.ly/2F7hN3u ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Choosing A Concurrency Model, Optimistic Or Pessimistic

  1. 1. Locking and Blocking with SQL Server<br />Choosing a concurrency Model –<br />Optimistic or Pessimistic<br />Vinod Kumar M<br />Technology Evangelist - Microsoft<br />www.ExtremeExperts.com<br />http://blogs.sqlxml.org/vinodkumar<br />
  2. 2. Why Study Concurrency?<br />Contention Degrades Performance<br />Most Tuning Tools Don’t Consider Concurrency Issues<br />You Can Write Better Applications <br />
  3. 3. Concurrency Models<br />Pessimistic Concurrency<br />Preventative approach<br />Limited concurrent access<br />Uses locking to avoid conflicts<br />Optimistic Concurrency<br />Allows full concurrent access<br />Row versioning allows access to consistent data<br />Conflict detection avoids inconsistent updates<br />
  4. 4. Transactions in SQL Server <br />Transaction Basics<br />Preventable Phenomena <br />
  5. 5. Transaction Basics<br />Allow Correctness of Operations To Be Verified<br />Exhibit Four Properties (ACID):<br />Atomicity<br />Consistency<br />Isolation<br />Durability <br />
  6. 6. PreventablePhenomena<br />Lost Updates<br />Dirty Reads<br />Non-Repeatable Reads<br />Phantoms<br />
  7. 7. Read X (10)<br />Compute X=X+15<br />(25)<br />Write X<br />Compute X=X+10<br />(20)<br />Read X (10)<br />Write X<br />Classic unsynchronized update<br />T1<br />X=20<br />X=25<br />X=10<br />X=25<br />Time<br />T2<br />Lost Update<br />
  8. 8. Read X (10)<br />Compute<br />X=X+15<br />(25)<br />Rollback<br />Use value ofX that was nevercommitted to DB<br />Read X (25)<br />Write X<br />T2 sees effects of T1 that were never committed<br />T1<br />X=10<br />X=25<br />X=10<br />Time<br />T2<br />Dirty Read<br />
  9. 9. Read X (10)<br />Compute X=X+15<br />(25)<br />Commit<br />Read X (10)<br />Read X (25)<br />Non-Repeatable Read<br />Transaction sees only committed data but data changes when referenced multiple times<br />T1<br />Write X<br />X=10<br />X=25<br />Time<br />T2<br />
  10. 10. Jones,6<br />Insert<br />The “Phantom” Problem<br />Transaction’s input set defined by a predicate<br />Transaction should not see additions/deletions from input set<br />Must prevent other transactions from inserting even if we don’t see the effects!<br />T1<br />Jones,6<br />Smith,4<br />Smith,4<br />Time<br />Brewer,7<br />Brewer,7<br />Select count (*) where rank > 3<br />(2)<br />Select count (*) where rank > 3<br />(3)<br />T2<br />
  11. 11. IsolationLevels<br />True Isolation Is Expensive <br />Trade off between correctness and concurrency<br />ANSI SQL Defines Four Isolation Levels based on which phenomena are allowed<br />No specification as to how to achieve isolation<br />SQL Server 2000 implements all ANSI isolation levels using Pessimistic Concurrency Model<br />SQL Server 2005 provides Optimistic Concurrency Alternatives<br />Optimistic implementation of read committed<br />New Snapshot Isolation<br />
  12. 12. SQL2008IsolationLevels<br /><ul><li>SET TRANSACTION ISOLATION LEVEL …</li></ul>Phenomena Allowed<br />
  13. 13. Locking Decision<br />Current Activity<br />Isolationlevel<br />Scan type(Range, Table, Probe)<br />Locking strategy(Table, Page, Row)<br /># of rows<br />in scan<br /># of rows/page<br />Operation type (scan, update)<br />Lock hints<br />sp_IndexOption Settings<br />
  14. 14. Pessimistic Concurrency Control<br />Aspects of Locking<br />Blocking<br />Controlling Locking<br />
  15. 15. Aspects of Locking<br />Type of Lock<br />Duration of Lock<br />Granularity of Lock<br />
  16. 16. Blocking<br />Occurs when one process requests a lock on the same resource held by another process in an incompatible mode<br />Blocking and Isolation Summary for Pessimistic Concurrency<br />Writers block writers in all levels<br />Writers block readers in Read Committed and higher<br />Readers block writers in Repeatable Read and higher<br />
  17. 17. Deadlock<br />What is Deadlock?<br />Handling Deadlock<br />
  18. 18. Process 1<br />X<br />Resource A<br />Resource B<br />X<br />Process 2<br />WhatisDeadlock?<br />Two Processes Mutually Blocking Each Other<br />
  19. 19. Blocked Process Threshold<br />Generates an event when a task(s) blocked for greater than ‘threshold’<br />‘Threshold’ is a configurable option<br />There is a background thread (Lock Monitor) that periodically checks tasks that are blocked.<br />You can use event to identify tasks <br />
  20. 20. Demo: Blocked Process Threshold<br />
  21. 21. Summary<br />Concurrency Models<br />Transactions in SQL Server <br />Isolation Levels<br />Pessimistic Concurrency Control<br />Optimistic Concurrency Control<br />
  22. 22. Additional Resources<br />Inside SQL Server 2005: The Storage Engine<br />(Microsoft Press, available now)<br />Blogs:<br />http://blogs.msdn.com/sqlserverstorageengine<br />http://blogs.msdn.com/sqltips<br />http://blogs.msdn.com/sqlcat<br />http://sqlblog.com/blogs/kalen_delaney<br />Scripts available at <br />www.InsideSQLServer.com/conferences<br />http://sqlblog.com/blogs/kalen_delaney<br />

×