SlideShare a Scribd company logo
1 of 25
 Concurrency and Transactions
 Locking Basics
 Advanced Locking Concepts
 Controlling Locking
 Troubleshooting Pessimistic Concurrency
 Optimistic Concurrency
 Introduction of Concurrency
We can define concurrency as the ability of multiple sessions to access or change
shared data, at the same time.
Introduction of Concurrency
• The greater the number of concurrent processes that can be active without
interfering with each other, the greater the concurrency of the database
system and the more scalable the system will be.
High Concurrency
Low Concurrency
• Concurrency is reduced when a session that is changing data prevents other
processes from reading that data.
• When a session that is reading data prevents other sessions from changing that
data.
Introduction of Concurrency
Current Concurrency Models Supported by SQL Server Database
• Pessimistic(Default)
• Optimistic
 Pessimistic concurrency avoids conflicts by acquiring locks on data that is
being read, so no other processes can modify that data.
 For example, when querying data into a form, for subsequent modification,
the application logic should store the original values and then check them
against the values that exist at the point the modification is issued.
 It also acquires locks on data being modified, so no other processes can access
that data for either reading or modifying.
 In other words, readers block writers and writers block readers in a pessimistic
concurrency environment.
 A programmer will need to enforce optimistic concurrency by the use of
intelligent programming techniques.
 If they are different, logic must exist to resolve this conflict and/or handle any
error raised.
Concurrency and Transactions
• The ANSI SQL Standard defines three phenomena (dirty reads, non-repeatable reads and
phantom reads), which can be allowed or prevented, depending on the ANSI-standard
transaction isolation level in use: READ UNCOMMITTED, READ COMMITTED(the default),
REPEATABLE READ, or SERIALIZABLE
Preventable read phenomena
Lost updates
• One session accidentally overwrites modifications performed by another.
Excessive blocking
• A "queue" of blocked processes forms, causing pressure on the resource and
unacceptable delays to end-users
• Mutual blocking between sessions such that further progress is
impossible.
Transaction
Deadlocks
• a transaction is that it is a single unit of work; a task or set of tasks that
together form an "all-or-nothing" operation.
• Atomicity– A transaction is treated as a single unit of work. Either it completes
entirely, or the system has no "memory" of it happening at all. This applies to transactions
of any size, whether two rows are being inserted, or 10 million rows are being updated.
• Consistency– A transaction will leave data in a meaningful state when it completes. In
a relational database, all constraints will be applied to the transaction's modifications to
maintain data integrity. Internal data structures, such as the trees and linked lists used for
maintaining indexes, will be correct at the end of a transaction. A transaction cannot
leave data in a state that violates uniqueness or referential integrity.
• Isolation– The changes that one transaction makes should not interfere with the
changes that another transaction makes; each transaction should be executed as if it were
the only work that the database system was performing.
• Durability– Once a transaction completes, its effects are permanent and recoverable.
If the system shuts down, either intentionally or because of a system crash, any time after
a transaction was completed (or committed) then, when the system starts again, the
changes made by completed transactions are available.
Concurrency and Transactions
However, by default, SQL Server guarantees only three out of the four:
atomicity, consistency and durability.
Transactions have four basic properties
• auto-commit transactions– An auto-commit transaction is any single data
modification operation. In other words, any INSERT, UPDATE or DELETE statement (as well
as others, such as MERGE and BULK INSERT), by itself, is automatically a transaction.
• explicit transactions– An explicit transaction uses the BEGIN TRANSACTION(or BEGIN
TRAN) statement to indicate the beginning of the transaction, and either a COMMIT
TRANSACTION or a ROLLBACK TRANSACTION statement to indicate the end. In between,
the transaction can include any number of statements.
• implicit transactions – implicit transactions, a session must be in implicit transaction
mode, invoked with a SET option: SET IMPLICIT_TRANSACTIONS ON. In implicit
transaction mode, the start of any transaction is implied.
Although, in this mode, the start of the transaction is implied, the
end of the transaction must be explicit, and the transaction is not finished until we issue
either a ROLLBACK TRAN or COMMIT TRAN.
Concurrency and Transactions
Transaction scope
The default types of transactions are auto-commit transactions and explicit
transactions. The non-default types of transactions are implicit transactions and
batch-scoped transactions.
• batch-scoped transactions– we invoke batch-scoped transactions by requesting the
option Multiple Active Result Sets(or MARS) in the client connection string. In those
connections, SQL Server will roll back any batch that includes a BEGIN TRAN but does not
include a COMMIT TRAN. The purpose of MARS is to avoid a problem called "application
deadlock,"
Concurrency and Transactions
Transaction scope
Transactions isolation levels
• READ UNCOMMITTED– allows dirty reads, non-repeatable reads and phantom reads
• READ COMMITTED– prevents dirty reads, allows non-repeatable reads and phantom
reads
• REPEATABLE READ– prevents dirty reads and non-repeatable reads but allows phantom
reads and doesn't allow another process to update values the first one has read.
• SERIALIZABLE– prevents all read phenomena.
Every transaction runs in one particular transaction isolation level, which
determines how sensitive your application is to changes made by other users'
transactions, and how long SQL Server must hold locks to protect against these
changes.
The ANSI SQL standard defines four levels of isolation for transactions.
• With the exception of READ UNCOMMITTED, each of these isolations levels is
pessimistic in nature. In other words, when transactions are operating in one of these
modes, SQL Server will acquire shared and exclusive locks in order to prevent data being
read that is currently being modified by another transaction, and to prevent other
transactions modifying data that is currently being read.
• In addition, SQL Server 2005 (and later) offers a new optimistic isolation level, called
SNAPSHOT isolation, plus an optimistic alternative to READ COMMITTED isolation
(READ_COMMITTED_SNAPSHOT), both of which can ensure consistent results without
the need to acquire shared locks, and so can enhance concurrency.
SQL Server's default isolation level is READ COMMITTED
Preventable read phenomena
• Dirty reads
 when a transaction reads uncommitted data.
If one transaction has changed data but not committed the change, and
another transaction is allowed to read that changed data, then there is
a strong possibility that the data will be read in an inconsistent state
 SQL Server does not allow dirty reads.
 the transaction updating the data has no control over whether or not
another transaction can read its data before it's committed.
 The decision regarding whether or not to read "dirty" data lies entirely in
the hands of the reading transaction.
• Non-repeatable reads
 This behavior is also called inconsistent analysis.
 A read is non-repeatable if a query might get different values when reading
the same data in two separate reads within the same transaction.
 This can happen when a separate transaction updates the same data, after
the first read but before the second read.
• Phantom reads.
 A phantom occurs if two SELECT operations using the same predicate in the
same transaction return a different number of rows.
Design Consideration of Pessimistic Concurrency Model
 While preventing blocking, by selecting the READ UNCOMMITTED level, might seem
attractive from a concurrency perspective, the price to pay is the prospect of reading
incorrect data.
 At the same time, while preventing all read phenomena, and so guaranteeing more
consistent data, is a "good thing," be aware of the tradeoffs in setting your isolation
level too high, which is the added cost of acquiring and managing locks, and blocking
other processes while those locks are held.
Locking Basics
 Locking is the activity that occurs when a SQL Server session takes "ownership" of a
resource prior to performing a particular action on that resource, such as reading or
updating it.
 SQL Server makes all locking decisions internally and will usually make the best choices.
 If we write code in such a way that it forces SQL Server to acquire a very large number of
locks, or to hold them for unnecessarily long periods, then
 this will cause resource contention in the database,
 other users' transactions will be blocked from accessing the data they need,
 the performance of these blocked transactions will be affected.
 The unit of data locked (lock resource) – such as row, page, or table
 The type of locks acquired (lock mode) – shared, exclusive, update, and so on
 The duration of the lock – how long the lock is held
 Lock ownership – the "scope" of the lock (most locks are transaction scoped)
 Lock metadata – how to review current locking using the Dynamic Management
 Fundamental aspects of this locking mechanism:
Lock Resources
 SQL Server can lock user data resources at the row, page, or table level
 SQL Server will attempt to acquire row-level locks, to allow the highest degree of
concurrency.
 SQL Server to acquire locks on larger units of data either initially, or through a process of
"lock escalation."
 When SQL Server locks a row in an index,
 it refers to it, and displays it, as a KEY lock,
 SQL Server locks the entire index row, not just the key column.
 SQL Server can also lock ranges of index rows called key-range locks
 Key-range locks are not perfect, but they do give much greater concurrency than locking
a whole page or the entire table.
 A key-range lock implies a locked range of index rows including all values greater than
the value of the index key that precedes the locked row, and ends with the locked row.
Lock Resources
 When SQL Server Locks on rows in a heap table (one without a clustered index) ,it might
acquire KEY locks for the non-clustered index rows and RID locks for the data rows.
 If there is no index on the column specifying the range (i.e LastName), SQL Server would
acquire row or page locks, even in recent versions.
For example, suppose we have an index on the lastname column in the Employees table. A
transaction, shown below, is running under the SERIALIZABLE isolation level, and reads a
range of rows in the Employees table.
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRAN
SELECT *
FROM Employees
WHERE LastName BETWEEN 'MacDougal' AND 'McDougall'
No one should be able to insert a row with McDonald, but a row with Mabry would be
fine.
Lock Modes
 Shared locks,
 Exclusive locks, and
 Update locks
The lock mode specifes how restrictive the lock is and what other actions are possible while
the lock is held.
SQL Server uses following lock modes to achieve the four required ANSI modes of
transaction isolation.
Shared locks
 By default, SQL Server acquires shared (S) locks automatically when it reads data.
 A table, page, or individual row of a table or index can hold an S lock.
 In addition, to support SERIALIZABLE transaction isolation, SQL Server can place S locks
on a range of index rows.
 SQL Server releases S locks as soon as it has finished reading the data.
 However, use of a higher transaction isolation level, either REPEATABLE READ or
SERIALIZABLE, changes this behavior, so that SQL Server holds S locks until the end of
the transaction.
 As the name implies, many processes can hold S locks on the same data, but no process
can acquire an exclusive lock on data that has an S lock on it
Exclusive locks
 SQL Server automatically acquires exclusive (X) locks on data in order to modify that
data, during an INSERT, UPDATE, or DELETE operation.
 Only one transaction at a time can hold an X lock on a particular data resource, and X
locks remain until the end of the transaction.
 The changed data is usually unavailable to any other process until the trans-action
holding the lock either commits or rolls back.
 However, if a transaction uses the READ UNCOMMITTED transaction isolation level, it
can read data exclusively locked by another transaction.
Update locks
 Update (U) locks are not really a separate kind of lock, but rather a hybrid of S and X
locks.
 A transaction acquires a U lock when SQL Server executes a data modification operation,
but first needs to perform a search to find the resource (for example, the row of data) to
modify.
 SQL Server doesn't need to place an X lock on the row until it is ready to perform the
modification, but it does need to apply some sort of lock as it searches, to protect that
same data from modification by another transaction in the time between finding the
data and modifying it. Therefore, SQL Server places a U lock on the row, checks the row
and, if it meets the criteria, converts it to an X lock.
Intent locks
 An intent lock is used on a "higher" level object to indicate that a lock (one of the types
of locks described) is being taken within that object.
 We can have intent shared (IS) locks, intent exclusive locks (IX), and even intent update
locks (IU)
 For example, a transaction that holds an X lock on a row in the Customers table will also
hold IX locks on both the page containing that row, and the Customers table
 These Intent locks will prevent another transaction from locking the entire Customers
table (acquiring an X lock on the table).
 Whenever a transaction acquires a lock at a lower level of granularity, it also acquires
higher-level intent locks for the same object.
Lock duration
 The length of time that SQL Server holds a lock depends primarily on the mode of the lock
and the transaction isolation level that is in effect.
READ COMMITTED
 SQL Server releases S locks as soon as it has read and processed the locked data.
 It holds an X lock until the end of the transaction, whether the transaction is
committed or rolled back.
 It holds a U lock until the end of the transaction, unless it promoted the U lock to an
X lock, in which case the X lock, as with all X locks, remains for the duration of the
transaction.
REPEATABLE READ or SERIALIZABLE
 S locks have the same duration as X locks.
 That is, SQL Server does not release them until the trans-action is over.
 In addition to changing the transaction isolation level, we can control the lock duration by
using lock hints.
Lock ownership
 Lock duration is directly affected by lock ownership
 Ownership == scope of the lock
Four types of lock scopes
 Transactions – Durations depend on
isolation level and lock mode (shared locks
and update/exclusive locks)
 SHARED_TRANSACTION_WORKSPACE – Every connection in any database (other
than master or tempdb) acquires a lock with this owner by. By observing these
locks, SQL Server can tell when a database is in use.
SHARED_TRANSACTION_WORKSPACE locks are held as long as a connection is
using a database.
 EXCLUSIVE_TRANSACTION_WORKSPACE – SQL Server acquires a lock with this owner
whenever it needs exclusive access to the database. This includes activities such as
dropping the database, restoring the database, or changing certain database
properties, such as the READ_ONLY status.
 Cursors – requested explicitly when a cursor is declared. If a cursor is opened using
a locking mode of SCROLL_LOCKS, a cursor lock is held on every row fetched until
the next row is fetched or the cursor is closed. Even if the transaction commits
before the next fetch, the cursor lock is not released
Mahabubur Rahaman
Senior Database Architect
Orion Informatics Ltd

More Related Content

What's hot

Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyBoris Hristov
 
Chapter-7 Relational Calculus
Chapter-7 Relational CalculusChapter-7 Relational Calculus
Chapter-7 Relational CalculusKunal Anand
 
Query decomposition in data base
Query decomposition in data baseQuery decomposition in data base
Query decomposition in data baseSalman Memon
 
Understanding isolation levels
Understanding isolation levelsUnderstanding isolation levels
Understanding isolation levelsHieu Nguyen Trung
 
SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction ManagementMark Ginnebaugh
 
introduction to NOSQL Database
introduction to NOSQL Databaseintroduction to NOSQL Database
introduction to NOSQL Databasenehabsairam
 
Microsoft SQL Server internals & architecture
Microsoft SQL Server internals & architectureMicrosoft SQL Server internals & architecture
Microsoft SQL Server internals & architectureKevin Kline
 
Oracle database performance tuning
Oracle database performance tuningOracle database performance tuning
Oracle database performance tuningYogiji Creations
 
Code scheduling constraints
Code scheduling constraintsCode scheduling constraints
Code scheduling constraintsArchanaMani2
 
Chapter-4 Enhanced ER Model
Chapter-4 Enhanced ER ModelChapter-4 Enhanced ER Model
Chapter-4 Enhanced ER ModelKunal Anand
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sqlUmang Gupta
 
SQL Server High Availability Solutions (Pros & Cons)
SQL Server High Availability Solutions (Pros & Cons)SQL Server High Availability Solutions (Pros & Cons)
SQL Server High Availability Solutions (Pros & Cons)Hamid J. Fard
 
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contentionTroubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contentionTanel Poder
 

What's hot (20)

Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server Concurrency
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Chapter-7 Relational Calculus
Chapter-7 Relational CalculusChapter-7 Relational Calculus
Chapter-7 Relational Calculus
 
Locking in SQL Server
Locking in SQL ServerLocking in SQL Server
Locking in SQL Server
 
Shadow paging
Shadow pagingShadow paging
Shadow paging
 
Concurrency Control.
Concurrency Control.Concurrency Control.
Concurrency Control.
 
Query decomposition in data base
Query decomposition in data baseQuery decomposition in data base
Query decomposition in data base
 
Understanding isolation levels
Understanding isolation levelsUnderstanding isolation levels
Understanding isolation levels
 
SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction Management
 
Acid properties
Acid propertiesAcid properties
Acid properties
 
introduction to NOSQL Database
introduction to NOSQL Databaseintroduction to NOSQL Database
introduction to NOSQL Database
 
Exadata Backup
Exadata BackupExadata Backup
Exadata Backup
 
Microsoft SQL Server internals & architecture
Microsoft SQL Server internals & architectureMicrosoft SQL Server internals & architecture
Microsoft SQL Server internals & architecture
 
Oracle database performance tuning
Oracle database performance tuningOracle database performance tuning
Oracle database performance tuning
 
Code scheduling constraints
Code scheduling constraintsCode scheduling constraints
Code scheduling constraints
 
Chapter-4 Enhanced ER Model
Chapter-4 Enhanced ER ModelChapter-4 Enhanced ER Model
Chapter-4 Enhanced ER Model
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sql
 
SQL Server High Availability Solutions (Pros & Cons)
SQL Server High Availability Solutions (Pros & Cons)SQL Server High Availability Solutions (Pros & Cons)
SQL Server High Availability Solutions (Pros & Cons)
 
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contentionTroubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contention
 
Lecture 1 ddbms
Lecture 1 ddbmsLecture 1 ddbms
Lecture 1 ddbms
 

Similar to Sql server concurrency

Transaction conccurency
Transaction conccurencyTransaction conccurency
Transaction conccurencyEsraa Farrag
 
Transaction management
Transaction managementTransaction management
Transaction managementArchanaMani2
 
Overview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed DatabasesOverview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed DatabasesMeghaj Mallick
 
Concurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptxConcurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptxMArshad35
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12Syed Asrarali
 
Locking and concurrency
Locking and concurrencyLocking and concurrency
Locking and concurrencyRumeysaDinsoy
 
Optimistic concurrency control in Distributed Systems
Optimistic concurrency control in Distributed SystemsOptimistic concurrency control in Distributed Systems
Optimistic concurrency control in Distributed Systemsmridul mishra
 
Presentation on Transaction
Presentation on TransactionPresentation on Transaction
Presentation on TransactionRahul Prajapati
 
Concurrency control
Concurrency controlConcurrency control
Concurrency controlkansel85
 
Database consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MXDatabase consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MXFrans Jongma
 
Svetlin Nakov - Database Transactions
Svetlin Nakov - Database TransactionsSvetlin Nakov - Database Transactions
Svetlin Nakov - Database TransactionsSvetlin Nakov
 
Acid Properties In Database Management System
Acid Properties In Database Management SystemAcid Properties In Database Management System
Acid Properties In Database Management SystemAshish Kumar
 

Similar to Sql server concurrency (20)

Dbms voc 5 unit
Dbms voc 5 unitDbms voc 5 unit
Dbms voc 5 unit
 
Transaction conccurency
Transaction conccurencyTransaction conccurency
Transaction conccurency
 
chp13.pdf
chp13.pdfchp13.pdf
chp13.pdf
 
Transaction management
Transaction managementTransaction management
Transaction management
 
Overview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed DatabasesOverview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed Databases
 
Concurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptxConcurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptx
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Locking and concurrency
Locking and concurrencyLocking and concurrency
Locking and concurrency
 
Chapter 4 u
Chapter 4 uChapter 4 u
Chapter 4 u
 
Optimistic concurrency control in Distributed Systems
Optimistic concurrency control in Distributed SystemsOptimistic concurrency control in Distributed Systems
Optimistic concurrency control in Distributed Systems
 
Vani dbms
Vani dbmsVani dbms
Vani dbms
 
Presentation on Transaction
Presentation on TransactionPresentation on Transaction
Presentation on Transaction
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Database consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MXDatabase consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MX
 
Svetlin Nakov - Database Transactions
Svetlin Nakov - Database TransactionsSvetlin Nakov - Database Transactions
Svetlin Nakov - Database Transactions
 
Acid Properties In Database Management System
Acid Properties In Database Management SystemAcid Properties In Database Management System
Acid Properties In Database Management System
 
Process coordination
Process coordinationProcess coordination
Process coordination
 
Lecture 5 inter process communication
Lecture 5 inter process communicationLecture 5 inter process communication
Lecture 5 inter process communication
 

More from Mahabubur Rahaman

Transaction isolationexamples
Transaction isolationexamplesTransaction isolationexamples
Transaction isolationexamplesMahabubur Rahaman
 
supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation
supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentationsupporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation
supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentationMahabubur Rahaman
 
supporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered tablesupporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered tableMahabubur Rahaman
 
Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexingMahabubur Rahaman
 
Introduction to Apache Hadoop Ecosystem
Introduction to Apache Hadoop EcosystemIntroduction to Apache Hadoop Ecosystem
Introduction to Apache Hadoop EcosystemMahabubur Rahaman
 

More from Mahabubur Rahaman (6)

Transaction isolationexamples
Transaction isolationexamplesTransaction isolationexamples
Transaction isolationexamples
 
Lock basicsexamples
Lock basicsexamplesLock basicsexamples
Lock basicsexamples
 
supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation
supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentationsupporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation
supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation
 
supporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered tablesupporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered table
 
Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexing
 
Introduction to Apache Hadoop Ecosystem
Introduction to Apache Hadoop EcosystemIntroduction to Apache Hadoop Ecosystem
Introduction to Apache Hadoop Ecosystem
 

Recently uploaded

Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusTimothy Spann
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxolyaivanovalion
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...shivangimorya083
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 

Recently uploaded (20)

Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptx
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 

Sql server concurrency

  • 1.
  • 2.  Concurrency and Transactions  Locking Basics  Advanced Locking Concepts  Controlling Locking  Troubleshooting Pessimistic Concurrency  Optimistic Concurrency  Introduction of Concurrency
  • 3. We can define concurrency as the ability of multiple sessions to access or change shared data, at the same time. Introduction of Concurrency • The greater the number of concurrent processes that can be active without interfering with each other, the greater the concurrency of the database system and the more scalable the system will be. High Concurrency Low Concurrency • Concurrency is reduced when a session that is changing data prevents other processes from reading that data. • When a session that is reading data prevents other sessions from changing that data.
  • 4. Introduction of Concurrency Current Concurrency Models Supported by SQL Server Database • Pessimistic(Default) • Optimistic  Pessimistic concurrency avoids conflicts by acquiring locks on data that is being read, so no other processes can modify that data.  For example, when querying data into a form, for subsequent modification, the application logic should store the original values and then check them against the values that exist at the point the modification is issued.  It also acquires locks on data being modified, so no other processes can access that data for either reading or modifying.  In other words, readers block writers and writers block readers in a pessimistic concurrency environment.  A programmer will need to enforce optimistic concurrency by the use of intelligent programming techniques.  If they are different, logic must exist to resolve this conflict and/or handle any error raised.
  • 5. Concurrency and Transactions • The ANSI SQL Standard defines three phenomena (dirty reads, non-repeatable reads and phantom reads), which can be allowed or prevented, depending on the ANSI-standard transaction isolation level in use: READ UNCOMMITTED, READ COMMITTED(the default), REPEATABLE READ, or SERIALIZABLE Preventable read phenomena Lost updates • One session accidentally overwrites modifications performed by another. Excessive blocking • A "queue" of blocked processes forms, causing pressure on the resource and unacceptable delays to end-users • Mutual blocking between sessions such that further progress is impossible. Transaction Deadlocks • a transaction is that it is a single unit of work; a task or set of tasks that together form an "all-or-nothing" operation.
  • 6. • Atomicity– A transaction is treated as a single unit of work. Either it completes entirely, or the system has no "memory" of it happening at all. This applies to transactions of any size, whether two rows are being inserted, or 10 million rows are being updated. • Consistency– A transaction will leave data in a meaningful state when it completes. In a relational database, all constraints will be applied to the transaction's modifications to maintain data integrity. Internal data structures, such as the trees and linked lists used for maintaining indexes, will be correct at the end of a transaction. A transaction cannot leave data in a state that violates uniqueness or referential integrity. • Isolation– The changes that one transaction makes should not interfere with the changes that another transaction makes; each transaction should be executed as if it were the only work that the database system was performing. • Durability– Once a transaction completes, its effects are permanent and recoverable. If the system shuts down, either intentionally or because of a system crash, any time after a transaction was completed (or committed) then, when the system starts again, the changes made by completed transactions are available. Concurrency and Transactions However, by default, SQL Server guarantees only three out of the four: atomicity, consistency and durability. Transactions have four basic properties
  • 7. • auto-commit transactions– An auto-commit transaction is any single data modification operation. In other words, any INSERT, UPDATE or DELETE statement (as well as others, such as MERGE and BULK INSERT), by itself, is automatically a transaction. • explicit transactions– An explicit transaction uses the BEGIN TRANSACTION(or BEGIN TRAN) statement to indicate the beginning of the transaction, and either a COMMIT TRANSACTION or a ROLLBACK TRANSACTION statement to indicate the end. In between, the transaction can include any number of statements. • implicit transactions – implicit transactions, a session must be in implicit transaction mode, invoked with a SET option: SET IMPLICIT_TRANSACTIONS ON. In implicit transaction mode, the start of any transaction is implied. Although, in this mode, the start of the transaction is implied, the end of the transaction must be explicit, and the transaction is not finished until we issue either a ROLLBACK TRAN or COMMIT TRAN. Concurrency and Transactions Transaction scope The default types of transactions are auto-commit transactions and explicit transactions. The non-default types of transactions are implicit transactions and batch-scoped transactions.
  • 8. • batch-scoped transactions– we invoke batch-scoped transactions by requesting the option Multiple Active Result Sets(or MARS) in the client connection string. In those connections, SQL Server will roll back any batch that includes a BEGIN TRAN but does not include a COMMIT TRAN. The purpose of MARS is to avoid a problem called "application deadlock," Concurrency and Transactions Transaction scope
  • 9. Transactions isolation levels • READ UNCOMMITTED– allows dirty reads, non-repeatable reads and phantom reads • READ COMMITTED– prevents dirty reads, allows non-repeatable reads and phantom reads • REPEATABLE READ– prevents dirty reads and non-repeatable reads but allows phantom reads and doesn't allow another process to update values the first one has read. • SERIALIZABLE– prevents all read phenomena. Every transaction runs in one particular transaction isolation level, which determines how sensitive your application is to changes made by other users' transactions, and how long SQL Server must hold locks to protect against these changes. The ANSI SQL standard defines four levels of isolation for transactions. • With the exception of READ UNCOMMITTED, each of these isolations levels is pessimistic in nature. In other words, when transactions are operating in one of these modes, SQL Server will acquire shared and exclusive locks in order to prevent data being read that is currently being modified by another transaction, and to prevent other transactions modifying data that is currently being read. • In addition, SQL Server 2005 (and later) offers a new optimistic isolation level, called SNAPSHOT isolation, plus an optimistic alternative to READ COMMITTED isolation (READ_COMMITTED_SNAPSHOT), both of which can ensure consistent results without the need to acquire shared locks, and so can enhance concurrency. SQL Server's default isolation level is READ COMMITTED
  • 10. Preventable read phenomena • Dirty reads  when a transaction reads uncommitted data. If one transaction has changed data but not committed the change, and another transaction is allowed to read that changed data, then there is a strong possibility that the data will be read in an inconsistent state  SQL Server does not allow dirty reads.  the transaction updating the data has no control over whether or not another transaction can read its data before it's committed.  The decision regarding whether or not to read "dirty" data lies entirely in the hands of the reading transaction. • Non-repeatable reads  This behavior is also called inconsistent analysis.  A read is non-repeatable if a query might get different values when reading the same data in two separate reads within the same transaction.  This can happen when a separate transaction updates the same data, after the first read but before the second read. • Phantom reads.  A phantom occurs if two SELECT operations using the same predicate in the same transaction return a different number of rows.
  • 11. Design Consideration of Pessimistic Concurrency Model  While preventing blocking, by selecting the READ UNCOMMITTED level, might seem attractive from a concurrency perspective, the price to pay is the prospect of reading incorrect data.  At the same time, while preventing all read phenomena, and so guaranteeing more consistent data, is a "good thing," be aware of the tradeoffs in setting your isolation level too high, which is the added cost of acquiring and managing locks, and blocking other processes while those locks are held.
  • 12.
  • 13. Locking Basics  Locking is the activity that occurs when a SQL Server session takes "ownership" of a resource prior to performing a particular action on that resource, such as reading or updating it.  SQL Server makes all locking decisions internally and will usually make the best choices.  If we write code in such a way that it forces SQL Server to acquire a very large number of locks, or to hold them for unnecessarily long periods, then  this will cause resource contention in the database,  other users' transactions will be blocked from accessing the data they need,  the performance of these blocked transactions will be affected.  The unit of data locked (lock resource) – such as row, page, or table  The type of locks acquired (lock mode) – shared, exclusive, update, and so on  The duration of the lock – how long the lock is held  Lock ownership – the "scope" of the lock (most locks are transaction scoped)  Lock metadata – how to review current locking using the Dynamic Management  Fundamental aspects of this locking mechanism:
  • 14. Lock Resources  SQL Server can lock user data resources at the row, page, or table level  SQL Server will attempt to acquire row-level locks, to allow the highest degree of concurrency.  SQL Server to acquire locks on larger units of data either initially, or through a process of "lock escalation."  When SQL Server locks a row in an index,  it refers to it, and displays it, as a KEY lock,  SQL Server locks the entire index row, not just the key column.  SQL Server can also lock ranges of index rows called key-range locks  Key-range locks are not perfect, but they do give much greater concurrency than locking a whole page or the entire table.  A key-range lock implies a locked range of index rows including all values greater than the value of the index key that precedes the locked row, and ends with the locked row.
  • 15. Lock Resources  When SQL Server Locks on rows in a heap table (one without a clustered index) ,it might acquire KEY locks for the non-clustered index rows and RID locks for the data rows.  If there is no index on the column specifying the range (i.e LastName), SQL Server would acquire row or page locks, even in recent versions. For example, suppose we have an index on the lastname column in the Employees table. A transaction, shown below, is running under the SERIALIZABLE isolation level, and reads a range of rows in the Employees table. SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRAN SELECT * FROM Employees WHERE LastName BETWEEN 'MacDougal' AND 'McDougall' No one should be able to insert a row with McDonald, but a row with Mabry would be fine.
  • 16. Lock Modes  Shared locks,  Exclusive locks, and  Update locks The lock mode specifes how restrictive the lock is and what other actions are possible while the lock is held. SQL Server uses following lock modes to achieve the four required ANSI modes of transaction isolation.
  • 17. Shared locks  By default, SQL Server acquires shared (S) locks automatically when it reads data.  A table, page, or individual row of a table or index can hold an S lock.  In addition, to support SERIALIZABLE transaction isolation, SQL Server can place S locks on a range of index rows.  SQL Server releases S locks as soon as it has finished reading the data.  However, use of a higher transaction isolation level, either REPEATABLE READ or SERIALIZABLE, changes this behavior, so that SQL Server holds S locks until the end of the transaction.  As the name implies, many processes can hold S locks on the same data, but no process can acquire an exclusive lock on data that has an S lock on it
  • 18. Exclusive locks  SQL Server automatically acquires exclusive (X) locks on data in order to modify that data, during an INSERT, UPDATE, or DELETE operation.  Only one transaction at a time can hold an X lock on a particular data resource, and X locks remain until the end of the transaction.  The changed data is usually unavailable to any other process until the trans-action holding the lock either commits or rolls back.  However, if a transaction uses the READ UNCOMMITTED transaction isolation level, it can read data exclusively locked by another transaction.
  • 19. Update locks  Update (U) locks are not really a separate kind of lock, but rather a hybrid of S and X locks.  A transaction acquires a U lock when SQL Server executes a data modification operation, but first needs to perform a search to find the resource (for example, the row of data) to modify.  SQL Server doesn't need to place an X lock on the row until it is ready to perform the modification, but it does need to apply some sort of lock as it searches, to protect that same data from modification by another transaction in the time between finding the data and modifying it. Therefore, SQL Server places a U lock on the row, checks the row and, if it meets the criteria, converts it to an X lock.
  • 20. Intent locks  An intent lock is used on a "higher" level object to indicate that a lock (one of the types of locks described) is being taken within that object.  We can have intent shared (IS) locks, intent exclusive locks (IX), and even intent update locks (IU)  For example, a transaction that holds an X lock on a row in the Customers table will also hold IX locks on both the page containing that row, and the Customers table  These Intent locks will prevent another transaction from locking the entire Customers table (acquiring an X lock on the table).  Whenever a transaction acquires a lock at a lower level of granularity, it also acquires higher-level intent locks for the same object.
  • 21. Lock duration  The length of time that SQL Server holds a lock depends primarily on the mode of the lock and the transaction isolation level that is in effect. READ COMMITTED  SQL Server releases S locks as soon as it has read and processed the locked data.  It holds an X lock until the end of the transaction, whether the transaction is committed or rolled back.  It holds a U lock until the end of the transaction, unless it promoted the U lock to an X lock, in which case the X lock, as with all X locks, remains for the duration of the transaction. REPEATABLE READ or SERIALIZABLE  S locks have the same duration as X locks.  That is, SQL Server does not release them until the trans-action is over.  In addition to changing the transaction isolation level, we can control the lock duration by using lock hints.
  • 22. Lock ownership  Lock duration is directly affected by lock ownership  Ownership == scope of the lock Four types of lock scopes  Transactions – Durations depend on isolation level and lock mode (shared locks and update/exclusive locks)  SHARED_TRANSACTION_WORKSPACE – Every connection in any database (other than master or tempdb) acquires a lock with this owner by. By observing these locks, SQL Server can tell when a database is in use. SHARED_TRANSACTION_WORKSPACE locks are held as long as a connection is using a database.  EXCLUSIVE_TRANSACTION_WORKSPACE – SQL Server acquires a lock with this owner whenever it needs exclusive access to the database. This includes activities such as dropping the database, restoring the database, or changing certain database properties, such as the READ_ONLY status.  Cursors – requested explicitly when a cursor is declared. If a cursor is opened using a locking mode of SCROLL_LOCKS, a cursor lock is held on every row fetched until the next row is fetched or the cursor is closed. Even if the transaction commits before the next fetch, the cursor lock is not released
  • 23.
  • 24.
  • 25. Mahabubur Rahaman Senior Database Architect Orion Informatics Ltd