SlideShare a Scribd company logo
1 of 74
Download to read offline
SQL Night
Concurrency
in SQL Server
Jun 10, 2014
24th
I have been started with computers.
I started my professional carrier in computers industry.
I have been started to work with SQL Server version 6.0
I earned my first certification at Microsoft as Microsoft Certified
Solution Developer (3rd in Greece) and started my carrier as Microsoft
Certified Trainer (MCT) with more than 20.000 hours of training until
now!
I became for first time Microsoft MVP on SQL Server
I created the SQL School Greece (www.sqlschool.gr)
I became MCT Regional Lead by Microsoft Learning Program.
I was certified as MCSE : Data Platform, MCSE: Business Intelligence
Antonios Chatzipavlis
Solution Architect •SQL Server Evangelist • Trainer • Speaker
MCT, MCSE, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS, MCAD, MCP, OCA, ITIL-F
 1982
 1988
 1996
 1998
 2010
 2012
 2013
Twitter
Facebook
YouTube
LinkedIn
Pinterest
@antoniosch / @sqlschool
fb/sqlschoolgr
yt/user/achatzipavlis
SQL School Greece group
pi/SQLschool/
• Introduction to SQL Server locking
• Basics of locking
• Advanced Concepts of locking
• Controlling locking
• Working with Pessimistic Concurrency
• Optimistic Concurrency
Presentation Outline
Introduction
to SQL Server
locking
Concurrency in SQL Server
• A session is a single connection to SQL Server
• Identified by a unique SessionID value
• It is initiated through an application when the open method is
used on a connection object or through a tool like SSMS
• Multiple sessions may originate from the same application
• Each query windows opened by the same user using the same
SSMS instance is a separate session
Session
• Occurred when a SQL Server session takes "ownership" of a
resource by acquiring a lock prior to performing a particular
action on that resource
• Locking will stay in effect until SQL Server releases the locks.
• Locking itself is not a problem
Locking
• Occurred when at least two sessions desire concurrent access to
the same resource.
• One session acquires a lock on the resource in order to perform
some action, and so renders that resource temporarily
unavailable to other sessions.
• As a result, other sessions requiring the same resource are
temporarily blocked.
• Typically, the blocked sessions will gain control of the resource after the
blocking session releases the locks, so that access to the resource is serialized.
• Not all concurrent access will cause blocking it is dependent on the
operations being performed by the sessions, which determines the type of
locks that are acquired.
Blocking
• Occurred when two sessions mutually block each other.
• Neither one can release the resources it holds until it acquires a
lock on the resource the other session holds.
• A deadlock can also involve more than two sessions, trapped in
a circular chain of dependencies.
Deadlock
• This term used to indicate a state where competition for access
to a certain resource is causing performance issues.
• In a database with well-designed tables and queries,
• SQL Server acquires and releases locks quickly
• and any blocking is fleeting,
• and undetectable by the end-user.
• However, in certain circumstances blocking issues can escalate
to the point where one session that is blocked, in turn blocks
other sessions, which in turn block others
• long-running transactions hold locks on a resource for a long time
• a very high number of sessions all require access to the same shared resource
Pressure
• A transaction is a sequence of T-SQL statements performed in
an all-or-nothing fashion by SQL Server
• Transactions are commonly created in two ways:
• Autocommit transactions.
• Individual data modification statements (e.g., INSERT, UPDATE, and DELETE)
submitted separately from other commands are automatically wrapped in a
transaction by SQL Server.
• Explicit transactions.
• User-initiated transactions are created through the use of transaction control
language (TCL) commands that begin, commit, or roll back work based on user-
issued code. TCL is a subset of T-SQL.
Transactions
• Atomicity
• Consistency
• Isolation
• Durability
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.
Transactions Properties
• Atomicity
• Consistency
• Isolation
• Durability
A transaction will leave data in a
meaningful state when it
completes.
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.
Transactions Properties
• Atomicity
• Consistency
• Isolation
• Durability
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
Transactions Properties
• Atomicity
• Consistency
• Isolation
• 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.
Transactions Properties
• READ UNCOMMITTED
• READ COMMITTED
• REPEATABLE READ
• SERIALIZABE
Allows dirty reads
Allows non-
repeatable reads
Allows phantom
reads
Transactions Isolation
• READ UNCOMMITTED
• READ COMMITTED
• REPEATABLE READ
• SERIALIZABE
Prevents dirty reads
Allows non-
repeatable reads
Allows phantom
reads
Transactions Isolation
• READ UNCOMMITTED
• READ COMMITTED
• REPEATABLE READ
• SERIALIZABE
Prevents dirty reads
Prevents non-
repeatable reads
Allows phantom
reads
Transactions Isolation
• READ UNCOMMITTED
• READ COMMITTED
• REPEATABLE READ
• SERIALIZABE
Prevents all read
phenomena
Transactions Isolation
• This behavior occurs 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.
• By default, SQL Server does not allow dirty reads.
Dirty 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.
Non-repeatable reads
• This behavior occurs when membership in a set changes.
• It can happen only when a query with a predicate, such as
WHERE clause is involved.
• A phantom occurs if two SELECT operations using the same
predicate in the same transaction return a different number of
rows.
Phantom reads
Isolation Levels Summary Table
Isolation Level
Dirty
Read
Non-repeatable
read Phantoms Concurrency
READ COMMITTED No Yes Yes Pessimistic
Optimistic
READ UNCOMMITTED Yes Yes Yes Pessimistic
REPEATABLE READ No No Yes Pessimistic
SNAPSHOOT No No No Optimistic
SERIALIAZABLE No No No Pessimistic
• One session accidentally overwrites modifications performed by
another
• A lost update occurs when the results of one update overwrites
the effects of another update, such that it's as if the first update
never happened
• None of the transaction isolation levels will permit lost updates
because it's impossible for two transactions to update the same
data, simultaneously
• Lost updates occurred when an application first reads data and
then, at some later point, updates it.
• This could be occurred in a system that does not manage concurrency
properly, and it can be considered an application bug
Lost Updates
Demo
Isolation Levels
Concurrency in SQL Server
Basics of locking
Concurrency in SQL Server
• Locking is an essential mechanism for allowing multiple users to
access and operate on data in a way that avoids inconsistencies
in that data
• Designed to help support the ACID properties of our
transactions, so that our data stays consistent
• 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
• Locks are a good and necessary database mechanism.
Locking Overview
Lock resources
Resource Description
RID Row identifier. Used to lock a single row within a table.
Key
Row lock within an index.
Used to protect key ranges in serializable transactions.
Page 8 kilobyte –(KB) data page or index page.
Extent Contiguous group of eight data pages or index pages.
Table Entire table, including all data and indexes.
DB Database.
Lock modes
Lock mode Description
Shared (S) Used for operations that do not change or update data (read-
only operations), such as a SELECT statement.
Update (U) Used on resources that can be updated. Prevents a common
form of deadlock that occurs when multiple sessions are
reading, locking, and potentially updating resources later.
Exclusive (X) Used for data-modification operations, such as INSERT,
UPDATE, or DELETE. Ensures that multiple updates cannot be
made to the same resource at the same time.
Intent Used to establish a lock hierarchy. The types of intent locks
are: intent shared (IS), intent exclusive (IX), and shared with
intent exclusive (SIX).
Schema Used when an operation dependent on the schema of a table
is executing. The types of schema locks are: schema
modification (Sch-M) and schema stability (Sch-S).
Bulk Update (BU) Used when bulk-copying data into a table and the TABLOCK
hint is specified.
• The length of time that SQL Server holds a lock depends 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.
• Holds an X lock until the end of the transaction, whether the transaction is
committed or rolled back.
• 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/SERIALIZABLE
• S locks have the same duration as X locks.
• SQL Server does not release them until the transaction is over.
• In addition to changing the transaction isolation level, we can control
the lock duration by using lock hints.
Lock duration
• Lock ownership is the scope of the lock
• We can observe lock ownership values in the request_owner_type
column of the sys.dm_tran_locks DMV
• TRANSACTION
• SHARED_TRANSACTION_WORKSPACE
• EXCLUSIVE_TRANSACTION_WORKSPACE
• CURSOR
• SESSION
Lock ownership
• sys.dm_tran_locks
• sp_lock
• SSMS Activity Monitor
• SSMS Reports on each database
Locking metadata
• Columns with prefix resource_ describe the resource holding the
lock or the resource on which the lock is requested
• Columns with prefix request_ describe the requesting session,
along with the mode of lock requested
Analyzing the sys.dm_tran_locks
Resource columns
Resource type Resource description Resource_associated_entity_id
DATABASE Represents a database. Not applicable
FILE
Represents a database file. This file can be either a data or
a log file.
Not applicable
OBJECT
Represents a database object. This object can be a data
table, view, stored procedure, extended stored procedure,
or any object that has an object ID.
Object ID
PAGE Represents a single page in a data file.
HoBt ID. This value corresponds to sys.partitions.hobt_id. The
HoBt ID is not always available for PAGE resources because
the HoBt ID is extra information that can be provided by the
caller, and not all callers can provide this information.
KEY Represents a row in an index. HoBt ID. This value corresponds to sys.partitions.hobt_id.
EXTENT
Represents a data file extent. An extent is a group of eight
contiguous pages.
Not applicable
RID Represents a physical row in a heap.
HoBt ID. This value corresponds to sys.partitions.hobt_id. The
HoBt ID is not always available for RID resources because the
HoBt ID is extra information that can be provided by the
caller, and not all callers can provide this information.
APPLICATION Represents an application specified resource. Not applicable
METADATA Represents metadata information. Not applicable
HOBT
Represents a heap or a B-tree. These are the basic access
path structures.
HoBt ID. This value corresponds to sys.partitions.hobt_id.
ALLOCATION_UNIT
Represents a set of related pages, such as an index
partition. Each allocation unit covers a single Index
Allocation Map (IAM) chain.
Allocation Unit ID. This value corresponds to
sys.allocation_units.allocation_unit_id.
Request columns
Name Description
request_mode Indicates whether the granted or requested lock is shared (S), exclusive (X), intent shared (IX), update (U)
request_type Types of resources that can be requested
request_status Status can be one of three values: GRANT, CONVERT, or WAIT.
• CONVERT indicates that the requestor has already been granted a request for the same resource in a
different mode and is currently waiting for an upgrade (convert) from the current lock mode to be
granted. (For example, SQL Server can convert a U lock to X.)
• WAIT indicates that the requestor does not currently hold a granted request on the resource.
request_session_id This value is the ID of the session that has requested the lock.
The owning session ID can change for distributed (DTC) and bound transactions.
request_reference_count This value is a rough count of the number of times the same requestor has requested this resource, and
applies only to resources that are not automatically released at the end of a transaction
request_exec_context_id This value is the execution context ID of the process that currently owns this request.
A value greater than 0 indicates that this is a sub-thread used to execute a parallel query.
request_owner_type This value refers to the owner discussed earlier, which indicates the scope of the lock. The five possible
values are:
• TRANSACTION,
• SHARED_TRANSACTION_WORKSPACE,
• EXCLUSIVE_TRANSACTION_WORKSPACE,
• CURSOR
• SESSION.
request_owner_id This value is currently used only for requests with an owner of TRANSACTION, and the owner ID is the
transaction ID.
This column can be joined with the transaction_id column in the sys.dm_tran_active_transactions view
lock_owner_address This value is the memory address of the internal data structure that is used to track this request. This
column can be joined with the resource_address column in sys.dm_os_waiting_tasks if this request is in the
WAIT or CONVERT state.
Demo
Locking examples
Concurrency in SQL Server
Advanced
Concepts of locking
Concurrency in SQL Server
Lock Compatibility
Lock Compatibility
Demo
Lock Mode
Conversion
Concurrency in SQL Server
• SQL Server acquires anintent lock ona high-level resource
• When it holds a lock on a component of that resource
• When SQL Server holds an S lock on a row, then it also holds IS locks on the page and the table containing that
row.
• SQL Server acquires an IU lock on an index page, when the component (a key) of that index had a U lock
• Shared Intent Exclusive(SIX)
• When SQL Server has one or more rows locked with X locks, the pages and the table that contains
the rows will acquire IX locks.
• When the same transaction performs an operation that requires an S lock, SQL Server will acquire a
SIX lock on the table.
• Update IntentExclusive (UIX)
• SQL Server never acquires U locks at the table level, so the only way to get a U lock and an IX lock
together is on a page.
• Shared Intent Update (SIU)
• SQL Server holds IU locks only at the page level and the corresponding table will have an IX lock.
• If in the same transaction, we then acquire an S lock on the page, the result will be an SIU lock
Special Intent Locks
• Akey-rangelockisassociatedwithaspecificindexkey
• RangeS-S(sharedkey-rangeandsharedresourcelock)
• When transactions arerunning in SERIALIZABLE isolation level, SQL Server will hold onto individual shared key locks on the selected data, and if anindex is
used to access the data, it will hold onto shared key-range locks onthe intervals between index keys.
• RangeS-U(sharedkey-rangeandupdateresourcelock)
• Ifanon-clustered index is used tolocate and update rows in aheap, while in SERIALIZABLE isolation level, and ifthe column being updated is not the
indexed column used for access, the SQL Server will acquire alock ofType RangeS-U.
• This means that there is anS lock onthe range between the index keys, but the index key itself has aUlock.
• The rows in the heap will have the expected X lock onthe RID.
• RangeX-X(exclusivekey-rangeandexclusiveresourcelock)
• Ifupdating rows in anindex while in SERIALIZABLE isolation level, the session will acquire exclusive key-range locks.
• Inorder to observe RangeX-X locks, the updated rows must beindex keys
• When the table has aclustered index, and would also occur when updating oneofthe key columns ofanon-clustered index.
• RangeI-N(insertkey-rangeandnoresourcelock)
• This kind oflock indicates anexclusive lock to prevent inserts onthe range between keys and nolock on the keys themselves.
• The lock on the range is aspecial type, I, which only occurs as part ofakey-range lock, and since there is noexisting resource tolock, the second part of
the name is N(for Null).
• SQL Server acquires RangeI-N locks when it attempts to insert values into the range between keys in SERIALIZABLE isolation level.
• Ifone transaction scans arange ofdata using the SERIALIZABLE isolation level and then another transaction tries toINSERT into that range, the second
transaction will have alock request in aWAIT state, with the RangeI-N mode.
Key-Range Locks
• SQL Serverwillacquirethe finest-grainlockpossible, in orderto attainthe greatest concurrency.
• SQL Server can acquire hundreds or thousands of individual locks on data in a single table without causing any
problems.
• If SQL Server determines that a query will access a range of rows within a clustered index, it may instead acquire
page locks.
• If every row on a page is going to be accessed, it's easier to manage a single page lock than dozens, or hundreds,
of row locks.
• When there is no usable index to help process a query, SQL Server may lock an entire table right at the beginning
of processing a query.
• Escalation based on SQL Serverinstance resource usage
• The memory required for each lock is about 96 bytes per lock
• SQL Server escalates any session holding locks from fine-grained locks into a table lock when using more than 24%
of its buffer pool
• Specifying LOCKS configuration option at instance level SQL Server will start choosing sessions to have their locks
escalated as soon as it has acquired 40% of that configured total number of locks.
• No control over which sessions will have their locks escalated to table locks
• As long as the memory use remains over the instance-wide threshold, for every 1,250 new locks, SQL Server will
again start escalating fine-grained locks into table locks.
• Escalation based on number of locks held by a single statement
• SQL Server will also escalate locks when any individual session acquires more than 5,000 locks in a single statement.
Lock Escalation
• Aresimilarto locks
• But they areapplied at the physical level
• Arenot as "expensive" to maintain and manage as locks
• Because usefewer system resources and theirduration is usually quiteshort.
• Latches and locks seem verysimilarbecause both of them can show up as the last_wait_type
column inthe sys.dm_exec_requests view
• SQL Traceand Windows PerformanceMonitorhave dozens of counters for monitoringlatches
• Like locks, latches can be shared or exclusive,and can be grantedor in a wait state.
• Latches do not show up inthe sys.dm_tran_locks view.
• Used to protect an internalstructureforbrief periodswhileit is being read or modified,not to
ensure correct transactionbehavior.
• Both thedatapage itself and thebuffer that thedata is occupying areprotected by latches.
Other Types of Locks - Latches
• Only one copy of a compiled stored procedure plan is generally
in cache at any given time
• Compile locks are acquired during the parts of the compilation
process that must be serialized.
• SQL Server holds the locks for a very short period but
• when many sessions are trying to execute the same procedure
simultaneously, and if the procedure is not schema-qualified when called
• we may end up with noticeable concurrency problems due to compile locks
• The Rolling blocking situation.
• LCK_M_X [COMPILE]
• This type of blocking has a very easy fix
• always schema qualify your stored procedure names
Other Types of Locks - Compile locks
Controlling locking
Concurrency in SQL Server
SET TRANSACTION ISOLATION LEVEL
READ UNCOMMITTED
READ COMMITTED
REPEATABLE READ
SNAPSHOT
SERIALIZABLE
Controlling Isolation Level
READ UNCOMMITTED
READ COMMITTED
REPEATABLE READ
SERIALIZABLE
SNAPSHOT
Takes no locks while
performing SELECT
operations so it cannot
block on locks held by
other transactions
Isolation Levels and locking
READ UNCOMMITTED
READ COMMITTED
REPEATABLE READ
SERIALIZABLE
SNAPSHOT
The default isolation level,
SQL Server holds shared
locks only until the data
has been read
Holds exclusive locks until
the end of the transaction
Isolation Levels and locking
READ UNCOMMITTED
READ COMMITTED
REPEATABLE READ
SERIALIZABLE
SNAPSHOT
Keeps shared locks
and
exclusive locks until the
end of the transaction
Isolation Levels and locking
READ UNCOMMITTED
READ COMMITTED
REPEATABLE READ
SERIALIZABLE
SNAPSHOT
The most restrictive
isolation level
Adopts a special locking
mechanism
Using key-range locks,
and holds all locks until
the end of the transaction
Isolation Levels and locking
READ UNCOMMITTED
READ COMMITTED
REPEATABLE READ
SERIALIZABLE
SNAPSHOT
Has the outward
appearance of
SERIALIZABLE
but
operates under a
completely different
concurrency model
(optimistic concurrency)
Isolation Levels and locking
• By default, SQL Server operates as though any session will eventually release
any locked data and will continue to wait until it is, regardless of how long that
might be.
• SET LOCK_TIMEOUT
• Tells SQL Server not to wait more than a specified number of milliseconds for a session to
release a lock
• Setting LOCK_TIMEOUT to -1 returns it to the default behavior of waiting indefinitely
• We can check the current value by examining the parameterless function
@@lock_timeout.
• If a session stays blocked for longer than the LOCK_TIMEOUT setting, SQL
Server generates a lock timeout error.
• This error doesn'tautomatically roll back a transaction.
• NOWAIT hint
• This hint sets a lock timeout of 0 for a single table in a single statement
Lock timeout
Locking Hints
Locking hint Hint description
GRANULARITY
ROWLOCK
Use row-level locks when reading or modifying data. These are acquired and
released as appropriate. SELECT operations take S locks on rows.
PAGLOCK
Use page-level locks when reading or modifying data. These are acquired and
released as appropriate. SELECT operations take S locks on pages.
TABLOCK
Use a table lock when reading or modifying data. This lock is held until the end of
the statement. SELECT operations take S locks on tables.
DBLOCK
Use a database lock when reading or modifying data. This lock is held until the end
of the statement. SELECT operations take S locks on databases.
LOCKMODES
UPDLOCK
Use update locks instead of shared locks while reading a table, and use hold locks
until the end of the statement or transaction. UPDLOCK lets you read data without
blocking other readers, and to update it later with the assurance that the data has
not changed since you last read it. SELECT operations take U locks. The default
granularity is ROWLOCK.
XLOCK
Use exclusive locks instead of shared locks while reading a table, and use hold locks
until the end of the statement or transaction. SELECT operations take X locks. The
default granularity is ROWLOCK.
DURATION
HOLDLOCK
Use a hold lock to hold a lock until completion of the transaction, instead of
releasing the lock as soon as the required table, row, or data page is no longer
required. If no granularity is specified, ROWLOCK is applied.
NOLOCK Does not issue any locks. Enables Read Uncommitted behavior.
Working with
Pessimistic
Concurrency
Concurrency in SQL Server
• SQL Server is in a pessimistic fashion
• In other words, SQL Server controls access to a shared resource by acquiring
locks on that resource, which ensure that readers of the resource block writers
and writers block readers (as well as other writers)
• SQL Server locks resources to ensure the logical consistency of
the database, during concurrent access of those shared
database resources
• Locking in SQL Server does not physically affect a data resource
such as a row, page, table, or index
• it is more like a reservation system that all tasks respect when they want
access to some resource within the database
• Excessive numbers of locks, or locks of very long duration, can
lead to blocking and other problems
Overview
• Detecting lock escalation
• The easiest way is to use the Lock:Escalation event class in SQL Trace/Profiler.
• Querying the sys.dm_tran_locks DMV to find table locks
• Resolving lock escalation
• Tune queries, ensuring that appropriate indexes are used and as few pages as
possible need to be accessed, and as few locks as possible need to be
acquired.
• Always keep transactions as short as possible, so that SQL Server doesn't
acquire and hold any non-essential locks.
• Reduce the batch sizes of mass inserts, updates, or deletes such that we
prevent unwanted lock escalation.
• Controlling escalation
ALTER TABLE <table_name>
SET (LOCK_ESCALATION = [TABLE | AUTO | DISABLE);
Troubleshooting Lock Escalation
• PerfMon counters
• The Processes blocked counter in the SQLServer:General Statistics object will show
the number of blocked processes.
• The Lock Waits counter from the SQLServer:Wait Statistics object to determine the
number of locks being held, and the duration of the locks.
• DMVs
• The sys.dm_os_waiting_tasks DMV returns a formatted list of all currently waiting
tasks, along with the blocking task
• The sys.dm_tran_locks of course
• Resolving blocking problems
• Killing a session (The easiest way to resolve a blocking situation)
• Make data modification transactions shorter
• Reduce the number of locks taken by writers
• Lower the isolation level to READ UNCOMMITTED
• Separate readers from writers
Troubleshooting Blocking
Optimistic
Concurrency
Concurrency in SQL Server
• SQL Server relies exclusively on locking to enforce the ACID-
compliance of its transactions
• In a pessimistic concurrency environment
• locks are acquired in order to avoid read phenomena such as dirty reads,
non-repeatable reads and phantom reads
• depending on the required ANSI isolation level
• Under the optimistic concurrency model SQL Server can prevent
some or all of these read phenomena without the need to
acquire locks, therefore greatly reducing blocking in the
database.
• Optimistic concurrency enabled via snapshot-based isolation
• First time introduced in SQL Server 2005
Overview
• Instead of blocking when unable to acquire a shared lock the reader
retrieves, from the version store, the previously committed values of
the set of rows it needs
• This is the big difference between optimistic and pessimistic
concurrency: with the former, writers and readers will not block each
other
• SQL Server still acquires locks during data modification operations, so
writers will still block writers, and everything we've discussed previously
about lock types, lock modes, and lock duration is still relevant to
optimistic concurrency and row versioning
• To support the storing of multiple older versions of rows in the version
store may require a lot of additional disk space in the tempdb database
Row Versioning
How Row Versioning Works
• Each old version of arow inthe version store contain a pointer to aneven older version of the same row.
• All the old versions of a particular row are chained together in a linked list
• SQL Server might need tofollow several pointers ina list to reach the right version.
• The version store must retain versioned rows for as long as there are operations that might require them.
• As long as a transaction is open, all versions of rows that havebeen modified bythat transaction must be kept in the version store
• Version of rows read bya statement (RCSI) or transaction (SI) must be kept in the version store as long as that statement or transaction is
open.
• In addition, the version store must also retain versions of rows modified by now-completed transactions if there are anyolder versions of
the same rows.
• Read committed snapshot isolation (RCSI)
• Queries return committed data as of the beginning of the current statement
• Enabling RCSI
• ALTER DATABASE AdventureWorks SET READ_COMMITTED_SNAPSHOT ON;
• Snapshot isolation (SI)
• Queries return committed data as of the beginning of the current transaction.
• Enabling SI
• ALTER DATABASE AdventureWorks SET ALLOW_SNAPSHOT_ISOLATION ON;
Snapshot-based Isolation Levels
Working with RCSI
Time Transaction 1 Transaction 2
0 SELECT ListPrice FROM
Products WHERE ProductID=10;
(Returns 90)
SELECT ListPrice FROM
Products WHERE ProductID=10;
(Returns 90)
1 BEGIN TRAN
UPDATE Products
SET UnitPrice= 100.00
WHERE ProductID=10;
BEGIN TRAN
2 SELECT ListPrice FROM
Products WHERE ProductID=10;
(Returns 90)
3 COMMIT TRAN SELECT ListPrice FROM
Products WHERE ProductID=10;
(Returns 100)
4 COMMIT TRAN
Working with SI
Time Transaction 1 Transaction 2
0 SELECT ListPrice FROM Products WHERE
ProductID=10;
(Returns 90)
SELECT ListPrice FROM Products
WHERE ProductID=10;
(Returns 90)
1 BEGIN TRAN
UPDATE Products
SET UnitPrice= 100.00
WHERE ProductID=10;
SET TRANSACTION ISOLATION LEVEL
SNAPSHOT
3 BEGIN TRAN
4 SELECT ListPrice FROM Products
WHERE ProductID=10;
(Returns 90)
5 COMMIT TRAN
6 SELECT ListPrice FROM Products
WHERE ProductID=10;
(Returns 90)
7 COMMIT TRAN
8 SELECT ListPrice FROM Products
WHERE ProductID=10;
(Returns 100)
• The sys.dm_tran_version_store DMV, contains information about
the actual rows in the version store.
• The version store maintains a linked list of previously committed
versions of each row in the database
• The current row points to the next older row, which can point to
an older row, and so on.
• SQL Server manages the version store size automatically
• Maintains a cleanup thread to make sure it does not keep
versioned rows around longer than needed
The Version Store
• For SELECT statements running under SI
• The version store retains the row versions until the transaction that modified
the data completes
• and the transactions containing any statements that reference the modified
data complete.
• For SELECT statements running under RCSI
• a particular row version is no longer required
• and is removed, once the SELECT statement has executed.
• Two other important DMVs for observing snapshot transaction
behavior are
• sys.dm_tran_transactions_snapshot
• sys.dm_tran_active_snapshot_database_transactions
The Version Store
• If application code invokes READ UNCOMMITTED isolation by
using the NOLOCK hint or the equivalent READUNCOMMITTED
hint, changing the database to RCSI will have no effect.
• The NOLOCK hint will override the database setting, and SQL
Server will continue to read the uncommitted (dirty) data.
• The only solution is to update the code to remove the hints.
The NOLOCK hint and RCSI
• In most situations, RCSI is recommended over SI for several
reasons
• RCSI consumes less tempdb space than SI.
• RCSI works with distributed transactions; SI does not.
• RCSI does not produce update conflicts.
• RCSI does not require any change in your applications
• Use of SI can be considered in the following situations:
• The probability is low that any transactions will have to be rolled back
because of an update conflict.
• Reports, based on long-running, multi-statement queries, need to be
generated with point-in-time consistency.
• Snapshot isolation provides the benefit of repeatable reads without being
blocked by concurrent modification operations.
RCSI vs SI
• Introduction to SQL Server locking
• Basics of locking
• Advanced Concepts of locking
• Controlling locking
• Working with Pessimistic Concurrency
• Optimistic Concurrency
Summary
help@sqlschool.gr
you need ?
SELECT
KNOWLEDGE
FROM
SQL SERVER
http://www.sqlschool.gr
Copyright © 2014 SQL School Greece

More Related Content

What's hot

Understanding Presto - Presto meetup @ Tokyo #1
Understanding Presto - Presto meetup @ Tokyo #1Understanding Presto - Presto meetup @ Tokyo #1
Understanding Presto - Presto meetup @ Tokyo #1Sadayuki Furuhashi
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An IntroductionSmita Prasad
 
Analyzing and Interpreting AWR
Analyzing and Interpreting AWRAnalyzing and Interpreting AWR
Analyzing and Interpreting AWRpasalapudi
 
MySQL Parallel Replication: inventory, use-case and limitations
MySQL Parallel Replication: inventory, use-case and limitationsMySQL Parallel Replication: inventory, use-case and limitations
MySQL Parallel Replication: inventory, use-case and limitationsJean-François Gagné
 
Performance Tuning And Optimization Microsoft SQL Database
Performance Tuning And Optimization Microsoft SQL DatabasePerformance Tuning And Optimization Microsoft SQL Database
Performance Tuning And Optimization Microsoft SQL DatabaseTung Nguyen Thanh
 
Sharding Methods for MongoDB
Sharding Methods for MongoDBSharding Methods for MongoDB
Sharding Methods for MongoDBMongoDB
 
Oracle sql high performance tuning
Oracle sql high performance tuningOracle sql high performance tuning
Oracle sql high performance tuningGuy Harrison
 
Apache Iceberg: An Architectural Look Under the Covers
Apache Iceberg: An Architectural Look Under the CoversApache Iceberg: An Architectural Look Under the Covers
Apache Iceberg: An Architectural Look Under the CoversScyllaDB
 
Ms sql server architecture
Ms sql server architectureMs sql server architecture
Ms sql server architectureAjeet Singh
 
How a Developer can Troubleshoot a SQL performing poorly on a Production DB
How a Developer can Troubleshoot a SQL performing poorly on a Production DBHow a Developer can Troubleshoot a SQL performing poorly on a Production DB
How a Developer can Troubleshoot a SQL performing poorly on a Production DBCarlos Sierra
 
Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.Mydbops
 
How many ways to monitor oracle golden gate - OOW14
How many ways to monitor oracle golden gate - OOW14How many ways to monitor oracle golden gate - OOW14
How many ways to monitor oracle golden gate - OOW14Bobby Curtis
 
Materialized Views and Secondary Indexes in Scylla: They Are finally here!
Materialized Views and Secondary Indexes in Scylla: They Are finally here!Materialized Views and Secondary Indexes in Scylla: They Are finally here!
Materialized Views and Secondary Indexes in Scylla: They Are finally here!ScyllaDB
 
What is in a Lucene index?
What is in a Lucene index?What is in a Lucene index?
What is in a Lucene index?lucenerevolution
 

What's hot (20)

Understanding Presto - Presto meetup @ Tokyo #1
Understanding Presto - Presto meetup @ Tokyo #1Understanding Presto - Presto meetup @ Tokyo #1
Understanding Presto - Presto meetup @ Tokyo #1
 
Oracle query optimizer
Oracle query optimizerOracle query optimizer
Oracle query optimizer
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
 
Analyzing and Interpreting AWR
Analyzing and Interpreting AWRAnalyzing and Interpreting AWR
Analyzing and Interpreting AWR
 
MySQL Parallel Replication: inventory, use-case and limitations
MySQL Parallel Replication: inventory, use-case and limitationsMySQL Parallel Replication: inventory, use-case and limitations
MySQL Parallel Replication: inventory, use-case and limitations
 
Performance tuning in sql server
Performance tuning in sql serverPerformance tuning in sql server
Performance tuning in sql server
 
Performance Tuning And Optimization Microsoft SQL Database
Performance Tuning And Optimization Microsoft SQL DatabasePerformance Tuning And Optimization Microsoft SQL Database
Performance Tuning And Optimization Microsoft SQL Database
 
Trigger
TriggerTrigger
Trigger
 
Sharding Methods for MongoDB
Sharding Methods for MongoDBSharding Methods for MongoDB
Sharding Methods for MongoDB
 
Oracle sql high performance tuning
Oracle sql high performance tuningOracle sql high performance tuning
Oracle sql high performance tuning
 
Apache Iceberg: An Architectural Look Under the Covers
Apache Iceberg: An Architectural Look Under the CoversApache Iceberg: An Architectural Look Under the Covers
Apache Iceberg: An Architectural Look Under the Covers
 
Ms sql server architecture
Ms sql server architectureMs sql server architecture
Ms sql server architecture
 
SQLServer Database Structures
SQLServer Database Structures SQLServer Database Structures
SQLServer Database Structures
 
How a Developer can Troubleshoot a SQL performing poorly on a Production DB
How a Developer can Troubleshoot a SQL performing poorly on a Production DBHow a Developer can Troubleshoot a SQL performing poorly on a Production DB
How a Developer can Troubleshoot a SQL performing poorly on a Production DB
 
Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.
 
How many ways to monitor oracle golden gate - OOW14
How many ways to monitor oracle golden gate - OOW14How many ways to monitor oracle golden gate - OOW14
How many ways to monitor oracle golden gate - OOW14
 
Sql server basics
Sql server basicsSql server basics
Sql server basics
 
MySQL SQL Tutorial
MySQL SQL TutorialMySQL SQL Tutorial
MySQL SQL Tutorial
 
Materialized Views and Secondary Indexes in Scylla: They Are finally here!
Materialized Views and Secondary Indexes in Scylla: They Are finally here!Materialized Views and Secondary Indexes in Scylla: They Are finally here!
Materialized Views and Secondary Indexes in Scylla: They Are finally here!
 
What is in a Lucene index?
What is in a Lucene index?What is in a Lucene index?
What is in a Lucene index?
 

Viewers also liked

Troubleshooting Deadlocks in SQL Server 2000
Troubleshooting Deadlocks in SQL Server 2000Troubleshooting Deadlocks in SQL Server 2000
Troubleshooting Deadlocks in SQL Server 2000elliando dias
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony DavisUncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony DavisRed Gate Software
 
Sql server performance tuning and optimization
Sql server performance tuning and optimizationSql server performance tuning and optimization
Sql server performance tuning and optimizationManish Rawat
 
SQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet SinghSQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet SinghRishu Mehra
 
SCOM 2012 & SCCM 2012
SCOM 2012 & SCCM 2012SCOM 2012 & SCCM 2012
SCOM 2012 & SCCM 2012Amit Gatenyo
 
10 Strategies for Resolving Common Fleet Management Challenges
10 Strategies for Resolving Common Fleet Management Challenges10 Strategies for Resolving Common Fleet Management Challenges
10 Strategies for Resolving Common Fleet Management ChallengesAssetWorks
 
MySQL Atchitecture and Concepts
MySQL Atchitecture and ConceptsMySQL Atchitecture and Concepts
MySQL Atchitecture and ConceptsTuyen Vuong
 
Fleet Management Basics
Fleet Management BasicsFleet Management Basics
Fleet Management Basicsjcade75834
 

Viewers also liked (10)

Troubleshooting Deadlocks in SQL Server 2000
Troubleshooting Deadlocks in SQL Server 2000Troubleshooting Deadlocks in SQL Server 2000
Troubleshooting Deadlocks in SQL Server 2000
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony DavisUncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony Davis
 
Sql server performance tuning and optimization
Sql server performance tuning and optimizationSql server performance tuning and optimization
Sql server performance tuning and optimization
 
SQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet SinghSQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet Singh
 
SCOM 2012 & SCCM 2012
SCOM 2012 & SCCM 2012SCOM 2012 & SCCM 2012
SCOM 2012 & SCCM 2012
 
10 Strategies for Resolving Common Fleet Management Challenges
10 Strategies for Resolving Common Fleet Management Challenges10 Strategies for Resolving Common Fleet Management Challenges
10 Strategies for Resolving Common Fleet Management Challenges
 
MySQL Atchitecture and Concepts
MySQL Atchitecture and ConceptsMySQL Atchitecture and Concepts
MySQL Atchitecture and Concepts
 
OpenERP / Odoo Fleet management
OpenERP / Odoo Fleet managementOpenERP / Odoo Fleet management
OpenERP / Odoo Fleet management
 
Fleet Management Basics
Fleet Management BasicsFleet Management Basics
Fleet Management Basics
 
Fleet Management
Fleet Management Fleet Management
Fleet Management
 

Similar to Concurrency in SQL Server (SQL Night #24)

Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Bob Pusateri
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Bob Pusateri
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...Bob Pusateri
 
Managing Memory & Locks - Series 2 Transactions & Lock management
Managing  Memory & Locks - Series 2 Transactions & Lock managementManaging  Memory & Locks - Series 2 Transactions & Lock management
Managing Memory & Locks - Series 2 Transactions & Lock managementDAGEOP LTD
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...Bob Pusateri
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...Bob Pusateri
 
Sql server-dba
Sql server-dbaSql server-dba
Sql server-dbaNaviSoft
 
Чурюканов Вячеслав, “Code simple, but not simpler”
Чурюканов Вячеслав, “Code simple, but not simpler”Чурюканов Вячеслав, “Code simple, but not simpler”
Чурюканов Вячеслав, “Code simple, but not simpler”EPAM Systems
 
SQL Server Wait Types Everyone Should Know
SQL Server Wait Types Everyone Should KnowSQL Server Wait Types Everyone Should Know
SQL Server Wait Types Everyone Should KnowDean Richards
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12Syed Asrarali
 
Roman Rehak: 24/7 Database Administration + Database Mail Unleashed
Roman Rehak: 24/7 Database Administration + Database Mail UnleashedRoman Rehak: 24/7 Database Administration + Database Mail Unleashed
Roman Rehak: 24/7 Database Administration + Database Mail UnleashedMSDEVMTL
 
Scaling Systems: Architectures that grow
Scaling Systems: Architectures that growScaling Systems: Architectures that grow
Scaling Systems: Architectures that growGibraltar Software
 
Locking and concurrency
Locking and concurrencyLocking and concurrency
Locking and concurrencyRumeysaDinsoy
 
Understanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database SystemsUnderstanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database Systemskiansahafi
 
High Performance Mysql
High Performance MysqlHigh Performance Mysql
High Performance Mysqlliufabin 66688
 
Sql server update-lock
Sql server update-lockSql server update-lock
Sql server update-lockShah Faisal
 
Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Oliersqlserver.co.il
 
Concurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptxConcurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptxMArshad35
 

Similar to Concurrency in SQL Server (SQL Night #24) (20)

Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASSDC User Gr...
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (New England SQ...
 
Managing Memory & Locks - Series 2 Transactions & Lock management
Managing  Memory & Locks - Series 2 Transactions & Lock managementManaging  Memory & Locks - Series 2 Transactions & Lock management
Managing Memory & Locks - Series 2 Transactions & Lock management
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (SQL Saturday M...
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (Chicago Suburb...
 
Sql server-dba
Sql server-dbaSql server-dba
Sql server-dba
 
Чурюканов Вячеслав, “Code simple, but not simpler”
Чурюканов Вячеслав, “Code simple, but not simpler”Чурюканов Вячеслав, “Code simple, but not simpler”
Чурюканов Вячеслав, “Code simple, but not simpler”
 
SQL Server Wait Types Everyone Should Know
SQL Server Wait Types Everyone Should KnowSQL Server Wait Types Everyone Should Know
SQL Server Wait Types Everyone Should Know
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12
 
Roman Rehak: 24/7 Database Administration + Database Mail Unleashed
Roman Rehak: 24/7 Database Administration + Database Mail UnleashedRoman Rehak: 24/7 Database Administration + Database Mail Unleashed
Roman Rehak: 24/7 Database Administration + Database Mail Unleashed
 
Scaling Systems: Architectures that grow
Scaling Systems: Architectures that growScaling Systems: Architectures that grow
Scaling Systems: Architectures that grow
 
Pl sql-ch3
Pl sql-ch3Pl sql-ch3
Pl sql-ch3
 
Locking and concurrency
Locking and concurrencyLocking and concurrency
Locking and concurrency
 
Understanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database SystemsUnderstanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database Systems
 
High Performance Mysql
High Performance MysqlHigh Performance Mysql
High Performance Mysql
 
Sql server update-lock
Sql server update-lockSql server update-lock
Sql server update-lock
 
Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Olier
 
Rails DB migrate SAFE.pdf
Rails DB migrate SAFE.pdfRails DB migrate SAFE.pdf
Rails DB migrate SAFE.pdf
 
Concurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptxConcurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptx
 

More from Antonios Chatzipavlis

Workload Management in SQL Server 2019
Workload Management in SQL Server 2019Workload Management in SQL Server 2019
Workload Management in SQL Server 2019Antonios Chatzipavlis
 
Loading Data into Azure SQL DW (Synapse Analytics)
Loading Data into Azure SQL DW (Synapse Analytics)Loading Data into Azure SQL DW (Synapse Analytics)
Loading Data into Azure SQL DW (Synapse Analytics)Antonios Chatzipavlis
 
Building diagnostic queries using DMVs and DMFs
Building diagnostic queries using DMVs and DMFs Building diagnostic queries using DMVs and DMFs
Building diagnostic queries using DMVs and DMFs Antonios Chatzipavlis
 
Designing a modern data warehouse in azure
Designing a modern data warehouse in azure   Designing a modern data warehouse in azure
Designing a modern data warehouse in azure Antonios Chatzipavlis
 
Modernizing your database with SQL Server 2019
Modernizing your database with SQL Server 2019Modernizing your database with SQL Server 2019
Modernizing your database with SQL Server 2019Antonios Chatzipavlis
 
Designing a modern data warehouse in azure
Designing a modern data warehouse in azure   Designing a modern data warehouse in azure
Designing a modern data warehouse in azure Antonios Chatzipavlis
 
Azure SQL Database for the SQL Server DBA - Azure Bootcamp Athens 2018
Azure SQL Database for the SQL Server DBA - Azure Bootcamp Athens 2018 Azure SQL Database for the SQL Server DBA - Azure Bootcamp Athens 2018
Azure SQL Database for the SQL Server DBA - Azure Bootcamp Athens 2018 Antonios Chatzipavlis
 
Introduction to Machine Learning on Azure
Introduction to Machine Learning on AzureIntroduction to Machine Learning on Azure
Introduction to Machine Learning on AzureAntonios Chatzipavlis
 

More from Antonios Chatzipavlis (20)

Data virtualization using polybase
Data virtualization using polybaseData virtualization using polybase
Data virtualization using polybase
 
SQL server Backup Restore Revealed
SQL server Backup Restore RevealedSQL server Backup Restore Revealed
SQL server Backup Restore Revealed
 
Migrate SQL Workloads to Azure
Migrate SQL Workloads to AzureMigrate SQL Workloads to Azure
Migrate SQL Workloads to Azure
 
Machine Learning in SQL Server 2019
Machine Learning in SQL Server 2019Machine Learning in SQL Server 2019
Machine Learning in SQL Server 2019
 
Workload Management in SQL Server 2019
Workload Management in SQL Server 2019Workload Management in SQL Server 2019
Workload Management in SQL Server 2019
 
Loading Data into Azure SQL DW (Synapse Analytics)
Loading Data into Azure SQL DW (Synapse Analytics)Loading Data into Azure SQL DW (Synapse Analytics)
Loading Data into Azure SQL DW (Synapse Analytics)
 
Introduction to DAX Language
Introduction to DAX LanguageIntroduction to DAX Language
Introduction to DAX Language
 
Building diagnostic queries using DMVs and DMFs
Building diagnostic queries using DMVs and DMFs Building diagnostic queries using DMVs and DMFs
Building diagnostic queries using DMVs and DMFs
 
Exploring T-SQL Anti-Patterns
Exploring T-SQL Anti-Patterns Exploring T-SQL Anti-Patterns
Exploring T-SQL Anti-Patterns
 
Designing a modern data warehouse in azure
Designing a modern data warehouse in azure   Designing a modern data warehouse in azure
Designing a modern data warehouse in azure
 
Modernizing your database with SQL Server 2019
Modernizing your database with SQL Server 2019Modernizing your database with SQL Server 2019
Modernizing your database with SQL Server 2019
 
Designing a modern data warehouse in azure
Designing a modern data warehouse in azure   Designing a modern data warehouse in azure
Designing a modern data warehouse in azure
 
Sqlschool 2017 recap - 2018 plans
Sqlschool 2017 recap - 2018 plansSqlschool 2017 recap - 2018 plans
Sqlschool 2017 recap - 2018 plans
 
Azure SQL Database for the SQL Server DBA - Azure Bootcamp Athens 2018
Azure SQL Database for the SQL Server DBA - Azure Bootcamp Athens 2018 Azure SQL Database for the SQL Server DBA - Azure Bootcamp Athens 2018
Azure SQL Database for the SQL Server DBA - Azure Bootcamp Athens 2018
 
Microsoft SQL Family and GDPR
Microsoft SQL Family and GDPRMicrosoft SQL Family and GDPR
Microsoft SQL Family and GDPR
 
Statistics and Indexes Internals
Statistics and Indexes InternalsStatistics and Indexes Internals
Statistics and Indexes Internals
 
Introduction to Azure Data Lake
Introduction to Azure Data LakeIntroduction to Azure Data Lake
Introduction to Azure Data Lake
 
Azure SQL Data Warehouse
Azure SQL Data Warehouse Azure SQL Data Warehouse
Azure SQL Data Warehouse
 
Introduction to azure document db
Introduction to azure document dbIntroduction to azure document db
Introduction to azure document db
 
Introduction to Machine Learning on Azure
Introduction to Machine Learning on AzureIntroduction to Machine Learning on Azure
Introduction to Machine Learning on Azure
 

Recently uploaded

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Concurrency in SQL Server (SQL Night #24)

  • 1. SQL Night Concurrency in SQL Server Jun 10, 2014 24th
  • 2. I have been started with computers. I started my professional carrier in computers industry. I have been started to work with SQL Server version 6.0 I earned my first certification at Microsoft as Microsoft Certified Solution Developer (3rd in Greece) and started my carrier as Microsoft Certified Trainer (MCT) with more than 20.000 hours of training until now! I became for first time Microsoft MVP on SQL Server I created the SQL School Greece (www.sqlschool.gr) I became MCT Regional Lead by Microsoft Learning Program. I was certified as MCSE : Data Platform, MCSE: Business Intelligence Antonios Chatzipavlis Solution Architect •SQL Server Evangelist • Trainer • Speaker MCT, MCSE, MCITP, MCPD, MCSD, MCDBA, MCSA, MCTS, MCAD, MCP, OCA, ITIL-F  1982  1988  1996  1998  2010  2012  2013
  • 4. • Introduction to SQL Server locking • Basics of locking • Advanced Concepts of locking • Controlling locking • Working with Pessimistic Concurrency • Optimistic Concurrency Presentation Outline
  • 6. • A session is a single connection to SQL Server • Identified by a unique SessionID value • It is initiated through an application when the open method is used on a connection object or through a tool like SSMS • Multiple sessions may originate from the same application • Each query windows opened by the same user using the same SSMS instance is a separate session Session
  • 7. • Occurred when a SQL Server session takes "ownership" of a resource by acquiring a lock prior to performing a particular action on that resource • Locking will stay in effect until SQL Server releases the locks. • Locking itself is not a problem Locking
  • 8. • Occurred when at least two sessions desire concurrent access to the same resource. • One session acquires a lock on the resource in order to perform some action, and so renders that resource temporarily unavailable to other sessions. • As a result, other sessions requiring the same resource are temporarily blocked. • Typically, the blocked sessions will gain control of the resource after the blocking session releases the locks, so that access to the resource is serialized. • Not all concurrent access will cause blocking it is dependent on the operations being performed by the sessions, which determines the type of locks that are acquired. Blocking
  • 9. • Occurred when two sessions mutually block each other. • Neither one can release the resources it holds until it acquires a lock on the resource the other session holds. • A deadlock can also involve more than two sessions, trapped in a circular chain of dependencies. Deadlock
  • 10. • This term used to indicate a state where competition for access to a certain resource is causing performance issues. • In a database with well-designed tables and queries, • SQL Server acquires and releases locks quickly • and any blocking is fleeting, • and undetectable by the end-user. • However, in certain circumstances blocking issues can escalate to the point where one session that is blocked, in turn blocks other sessions, which in turn block others • long-running transactions hold locks on a resource for a long time • a very high number of sessions all require access to the same shared resource Pressure
  • 11. • A transaction is a sequence of T-SQL statements performed in an all-or-nothing fashion by SQL Server • Transactions are commonly created in two ways: • Autocommit transactions. • Individual data modification statements (e.g., INSERT, UPDATE, and DELETE) submitted separately from other commands are automatically wrapped in a transaction by SQL Server. • Explicit transactions. • User-initiated transactions are created through the use of transaction control language (TCL) commands that begin, commit, or roll back work based on user- issued code. TCL is a subset of T-SQL. Transactions
  • 12. • Atomicity • Consistency • Isolation • Durability 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. Transactions Properties
  • 13. • Atomicity • Consistency • Isolation • Durability A transaction will leave data in a meaningful state when it completes. 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. Transactions Properties
  • 14. • Atomicity • Consistency • Isolation • Durability 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 Transactions Properties
  • 15. • Atomicity • Consistency • Isolation • 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. Transactions Properties
  • 16. • READ UNCOMMITTED • READ COMMITTED • REPEATABLE READ • SERIALIZABE Allows dirty reads Allows non- repeatable reads Allows phantom reads Transactions Isolation
  • 17. • READ UNCOMMITTED • READ COMMITTED • REPEATABLE READ • SERIALIZABE Prevents dirty reads Allows non- repeatable reads Allows phantom reads Transactions Isolation
  • 18. • READ UNCOMMITTED • READ COMMITTED • REPEATABLE READ • SERIALIZABE Prevents dirty reads Prevents non- repeatable reads Allows phantom reads Transactions Isolation
  • 19. • READ UNCOMMITTED • READ COMMITTED • REPEATABLE READ • SERIALIZABE Prevents all read phenomena Transactions Isolation
  • 20. • This behavior occurs 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. • By default, SQL Server does not allow dirty reads. Dirty Reads
  • 21. • 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. Non-repeatable reads
  • 22. • This behavior occurs when membership in a set changes. • It can happen only when a query with a predicate, such as WHERE clause is involved. • A phantom occurs if two SELECT operations using the same predicate in the same transaction return a different number of rows. Phantom reads
  • 23. Isolation Levels Summary Table Isolation Level Dirty Read Non-repeatable read Phantoms Concurrency READ COMMITTED No Yes Yes Pessimistic Optimistic READ UNCOMMITTED Yes Yes Yes Pessimistic REPEATABLE READ No No Yes Pessimistic SNAPSHOOT No No No Optimistic SERIALIAZABLE No No No Pessimistic
  • 24. • One session accidentally overwrites modifications performed by another • A lost update occurs when the results of one update overwrites the effects of another update, such that it's as if the first update never happened • None of the transaction isolation levels will permit lost updates because it's impossible for two transactions to update the same data, simultaneously • Lost updates occurred when an application first reads data and then, at some later point, updates it. • This could be occurred in a system that does not manage concurrency properly, and it can be considered an application bug Lost Updates
  • 27. • Locking is an essential mechanism for allowing multiple users to access and operate on data in a way that avoids inconsistencies in that data • Designed to help support the ACID properties of our transactions, so that our data stays consistent • 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 • Locks are a good and necessary database mechanism. Locking Overview
  • 28. Lock resources Resource Description RID Row identifier. Used to lock a single row within a table. Key Row lock within an index. Used to protect key ranges in serializable transactions. Page 8 kilobyte –(KB) data page or index page. Extent Contiguous group of eight data pages or index pages. Table Entire table, including all data and indexes. DB Database.
  • 29. Lock modes Lock mode Description Shared (S) Used for operations that do not change or update data (read- only operations), such as a SELECT statement. Update (U) Used on resources that can be updated. Prevents a common form of deadlock that occurs when multiple sessions are reading, locking, and potentially updating resources later. Exclusive (X) Used for data-modification operations, such as INSERT, UPDATE, or DELETE. Ensures that multiple updates cannot be made to the same resource at the same time. Intent Used to establish a lock hierarchy. The types of intent locks are: intent shared (IS), intent exclusive (IX), and shared with intent exclusive (SIX). Schema Used when an operation dependent on the schema of a table is executing. The types of schema locks are: schema modification (Sch-M) and schema stability (Sch-S). Bulk Update (BU) Used when bulk-copying data into a table and the TABLOCK hint is specified.
  • 30. • The length of time that SQL Server holds a lock depends 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. • Holds an X lock until the end of the transaction, whether the transaction is committed or rolled back. • 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/SERIALIZABLE • S locks have the same duration as X locks. • SQL Server does not release them until the transaction is over. • In addition to changing the transaction isolation level, we can control the lock duration by using lock hints. Lock duration
  • 31. • Lock ownership is the scope of the lock • We can observe lock ownership values in the request_owner_type column of the sys.dm_tran_locks DMV • TRANSACTION • SHARED_TRANSACTION_WORKSPACE • EXCLUSIVE_TRANSACTION_WORKSPACE • CURSOR • SESSION Lock ownership
  • 32. • sys.dm_tran_locks • sp_lock • SSMS Activity Monitor • SSMS Reports on each database Locking metadata
  • 33. • Columns with prefix resource_ describe the resource holding the lock or the resource on which the lock is requested • Columns with prefix request_ describe the requesting session, along with the mode of lock requested Analyzing the sys.dm_tran_locks
  • 34. Resource columns Resource type Resource description Resource_associated_entity_id DATABASE Represents a database. Not applicable FILE Represents a database file. This file can be either a data or a log file. Not applicable OBJECT Represents a database object. This object can be a data table, view, stored procedure, extended stored procedure, or any object that has an object ID. Object ID PAGE Represents a single page in a data file. HoBt ID. This value corresponds to sys.partitions.hobt_id. The HoBt ID is not always available for PAGE resources because the HoBt ID is extra information that can be provided by the caller, and not all callers can provide this information. KEY Represents a row in an index. HoBt ID. This value corresponds to sys.partitions.hobt_id. EXTENT Represents a data file extent. An extent is a group of eight contiguous pages. Not applicable RID Represents a physical row in a heap. HoBt ID. This value corresponds to sys.partitions.hobt_id. The HoBt ID is not always available for RID resources because the HoBt ID is extra information that can be provided by the caller, and not all callers can provide this information. APPLICATION Represents an application specified resource. Not applicable METADATA Represents metadata information. Not applicable HOBT Represents a heap or a B-tree. These are the basic access path structures. HoBt ID. This value corresponds to sys.partitions.hobt_id. ALLOCATION_UNIT Represents a set of related pages, such as an index partition. Each allocation unit covers a single Index Allocation Map (IAM) chain. Allocation Unit ID. This value corresponds to sys.allocation_units.allocation_unit_id.
  • 35. Request columns Name Description request_mode Indicates whether the granted or requested lock is shared (S), exclusive (X), intent shared (IX), update (U) request_type Types of resources that can be requested request_status Status can be one of three values: GRANT, CONVERT, or WAIT. • CONVERT indicates that the requestor has already been granted a request for the same resource in a different mode and is currently waiting for an upgrade (convert) from the current lock mode to be granted. (For example, SQL Server can convert a U lock to X.) • WAIT indicates that the requestor does not currently hold a granted request on the resource. request_session_id This value is the ID of the session that has requested the lock. The owning session ID can change for distributed (DTC) and bound transactions. request_reference_count This value is a rough count of the number of times the same requestor has requested this resource, and applies only to resources that are not automatically released at the end of a transaction request_exec_context_id This value is the execution context ID of the process that currently owns this request. A value greater than 0 indicates that this is a sub-thread used to execute a parallel query. request_owner_type This value refers to the owner discussed earlier, which indicates the scope of the lock. The five possible values are: • TRANSACTION, • SHARED_TRANSACTION_WORKSPACE, • EXCLUSIVE_TRANSACTION_WORKSPACE, • CURSOR • SESSION. request_owner_id This value is currently used only for requests with an owner of TRANSACTION, and the owner ID is the transaction ID. This column can be joined with the transaction_id column in the sys.dm_tran_active_transactions view lock_owner_address This value is the memory address of the internal data structure that is used to track this request. This column can be joined with the resource_address column in sys.dm_os_waiting_tasks if this request is in the WAIT or CONVERT state.
  • 41. • SQL Server acquires anintent lock ona high-level resource • When it holds a lock on a component of that resource • When SQL Server holds an S lock on a row, then it also holds IS locks on the page and the table containing that row. • SQL Server acquires an IU lock on an index page, when the component (a key) of that index had a U lock • Shared Intent Exclusive(SIX) • When SQL Server has one or more rows locked with X locks, the pages and the table that contains the rows will acquire IX locks. • When the same transaction performs an operation that requires an S lock, SQL Server will acquire a SIX lock on the table. • Update IntentExclusive (UIX) • SQL Server never acquires U locks at the table level, so the only way to get a U lock and an IX lock together is on a page. • Shared Intent Update (SIU) • SQL Server holds IU locks only at the page level and the corresponding table will have an IX lock. • If in the same transaction, we then acquire an S lock on the page, the result will be an SIU lock Special Intent Locks
  • 42. • Akey-rangelockisassociatedwithaspecificindexkey • RangeS-S(sharedkey-rangeandsharedresourcelock) • When transactions arerunning in SERIALIZABLE isolation level, SQL Server will hold onto individual shared key locks on the selected data, and if anindex is used to access the data, it will hold onto shared key-range locks onthe intervals between index keys. • RangeS-U(sharedkey-rangeandupdateresourcelock) • Ifanon-clustered index is used tolocate and update rows in aheap, while in SERIALIZABLE isolation level, and ifthe column being updated is not the indexed column used for access, the SQL Server will acquire alock ofType RangeS-U. • This means that there is anS lock onthe range between the index keys, but the index key itself has aUlock. • The rows in the heap will have the expected X lock onthe RID. • RangeX-X(exclusivekey-rangeandexclusiveresourcelock) • Ifupdating rows in anindex while in SERIALIZABLE isolation level, the session will acquire exclusive key-range locks. • Inorder to observe RangeX-X locks, the updated rows must beindex keys • When the table has aclustered index, and would also occur when updating oneofthe key columns ofanon-clustered index. • RangeI-N(insertkey-rangeandnoresourcelock) • This kind oflock indicates anexclusive lock to prevent inserts onthe range between keys and nolock on the keys themselves. • The lock on the range is aspecial type, I, which only occurs as part ofakey-range lock, and since there is noexisting resource tolock, the second part of the name is N(for Null). • SQL Server acquires RangeI-N locks when it attempts to insert values into the range between keys in SERIALIZABLE isolation level. • Ifone transaction scans arange ofdata using the SERIALIZABLE isolation level and then another transaction tries toINSERT into that range, the second transaction will have alock request in aWAIT state, with the RangeI-N mode. Key-Range Locks
  • 43. • SQL Serverwillacquirethe finest-grainlockpossible, in orderto attainthe greatest concurrency. • SQL Server can acquire hundreds or thousands of individual locks on data in a single table without causing any problems. • If SQL Server determines that a query will access a range of rows within a clustered index, it may instead acquire page locks. • If every row on a page is going to be accessed, it's easier to manage a single page lock than dozens, or hundreds, of row locks. • When there is no usable index to help process a query, SQL Server may lock an entire table right at the beginning of processing a query. • Escalation based on SQL Serverinstance resource usage • The memory required for each lock is about 96 bytes per lock • SQL Server escalates any session holding locks from fine-grained locks into a table lock when using more than 24% of its buffer pool • Specifying LOCKS configuration option at instance level SQL Server will start choosing sessions to have their locks escalated as soon as it has acquired 40% of that configured total number of locks. • No control over which sessions will have their locks escalated to table locks • As long as the memory use remains over the instance-wide threshold, for every 1,250 new locks, SQL Server will again start escalating fine-grained locks into table locks. • Escalation based on number of locks held by a single statement • SQL Server will also escalate locks when any individual session acquires more than 5,000 locks in a single statement. Lock Escalation
  • 44. • Aresimilarto locks • But they areapplied at the physical level • Arenot as "expensive" to maintain and manage as locks • Because usefewer system resources and theirduration is usually quiteshort. • Latches and locks seem verysimilarbecause both of them can show up as the last_wait_type column inthe sys.dm_exec_requests view • SQL Traceand Windows PerformanceMonitorhave dozens of counters for monitoringlatches • Like locks, latches can be shared or exclusive,and can be grantedor in a wait state. • Latches do not show up inthe sys.dm_tran_locks view. • Used to protect an internalstructureforbrief periodswhileit is being read or modified,not to ensure correct transactionbehavior. • Both thedatapage itself and thebuffer that thedata is occupying areprotected by latches. Other Types of Locks - Latches
  • 45. • Only one copy of a compiled stored procedure plan is generally in cache at any given time • Compile locks are acquired during the parts of the compilation process that must be serialized. • SQL Server holds the locks for a very short period but • when many sessions are trying to execute the same procedure simultaneously, and if the procedure is not schema-qualified when called • we may end up with noticeable concurrency problems due to compile locks • The Rolling blocking situation. • LCK_M_X [COMPILE] • This type of blocking has a very easy fix • always schema qualify your stored procedure names Other Types of Locks - Compile locks
  • 47. SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED READ COMMITTED REPEATABLE READ SNAPSHOT SERIALIZABLE Controlling Isolation Level
  • 48. READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE SNAPSHOT Takes no locks while performing SELECT operations so it cannot block on locks held by other transactions Isolation Levels and locking
  • 49. READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE SNAPSHOT The default isolation level, SQL Server holds shared locks only until the data has been read Holds exclusive locks until the end of the transaction Isolation Levels and locking
  • 50. READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE SNAPSHOT Keeps shared locks and exclusive locks until the end of the transaction Isolation Levels and locking
  • 51. READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE SNAPSHOT The most restrictive isolation level Adopts a special locking mechanism Using key-range locks, and holds all locks until the end of the transaction Isolation Levels and locking
  • 52. READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE SNAPSHOT Has the outward appearance of SERIALIZABLE but operates under a completely different concurrency model (optimistic concurrency) Isolation Levels and locking
  • 53. • By default, SQL Server operates as though any session will eventually release any locked data and will continue to wait until it is, regardless of how long that might be. • SET LOCK_TIMEOUT • Tells SQL Server not to wait more than a specified number of milliseconds for a session to release a lock • Setting LOCK_TIMEOUT to -1 returns it to the default behavior of waiting indefinitely • We can check the current value by examining the parameterless function @@lock_timeout. • If a session stays blocked for longer than the LOCK_TIMEOUT setting, SQL Server generates a lock timeout error. • This error doesn'tautomatically roll back a transaction. • NOWAIT hint • This hint sets a lock timeout of 0 for a single table in a single statement Lock timeout
  • 54. Locking Hints Locking hint Hint description GRANULARITY ROWLOCK Use row-level locks when reading or modifying data. These are acquired and released as appropriate. SELECT operations take S locks on rows. PAGLOCK Use page-level locks when reading or modifying data. These are acquired and released as appropriate. SELECT operations take S locks on pages. TABLOCK Use a table lock when reading or modifying data. This lock is held until the end of the statement. SELECT operations take S locks on tables. DBLOCK Use a database lock when reading or modifying data. This lock is held until the end of the statement. SELECT operations take S locks on databases. LOCKMODES UPDLOCK Use update locks instead of shared locks while reading a table, and use hold locks until the end of the statement or transaction. UPDLOCK lets you read data without blocking other readers, and to update it later with the assurance that the data has not changed since you last read it. SELECT operations take U locks. The default granularity is ROWLOCK. XLOCK Use exclusive locks instead of shared locks while reading a table, and use hold locks until the end of the statement or transaction. SELECT operations take X locks. The default granularity is ROWLOCK. DURATION HOLDLOCK Use a hold lock to hold a lock until completion of the transaction, instead of releasing the lock as soon as the required table, row, or data page is no longer required. If no granularity is specified, ROWLOCK is applied. NOLOCK Does not issue any locks. Enables Read Uncommitted behavior.
  • 56. • SQL Server is in a pessimistic fashion • In other words, SQL Server controls access to a shared resource by acquiring locks on that resource, which ensure that readers of the resource block writers and writers block readers (as well as other writers) • SQL Server locks resources to ensure the logical consistency of the database, during concurrent access of those shared database resources • Locking in SQL Server does not physically affect a data resource such as a row, page, table, or index • it is more like a reservation system that all tasks respect when they want access to some resource within the database • Excessive numbers of locks, or locks of very long duration, can lead to blocking and other problems Overview
  • 57. • Detecting lock escalation • The easiest way is to use the Lock:Escalation event class in SQL Trace/Profiler. • Querying the sys.dm_tran_locks DMV to find table locks • Resolving lock escalation • Tune queries, ensuring that appropriate indexes are used and as few pages as possible need to be accessed, and as few locks as possible need to be acquired. • Always keep transactions as short as possible, so that SQL Server doesn't acquire and hold any non-essential locks. • Reduce the batch sizes of mass inserts, updates, or deletes such that we prevent unwanted lock escalation. • Controlling escalation ALTER TABLE <table_name> SET (LOCK_ESCALATION = [TABLE | AUTO | DISABLE); Troubleshooting Lock Escalation
  • 58. • PerfMon counters • The Processes blocked counter in the SQLServer:General Statistics object will show the number of blocked processes. • The Lock Waits counter from the SQLServer:Wait Statistics object to determine the number of locks being held, and the duration of the locks. • DMVs • The sys.dm_os_waiting_tasks DMV returns a formatted list of all currently waiting tasks, along with the blocking task • The sys.dm_tran_locks of course • Resolving blocking problems • Killing a session (The easiest way to resolve a blocking situation) • Make data modification transactions shorter • Reduce the number of locks taken by writers • Lower the isolation level to READ UNCOMMITTED • Separate readers from writers Troubleshooting Blocking
  • 60. • SQL Server relies exclusively on locking to enforce the ACID- compliance of its transactions • In a pessimistic concurrency environment • locks are acquired in order to avoid read phenomena such as dirty reads, non-repeatable reads and phantom reads • depending on the required ANSI isolation level • Under the optimistic concurrency model SQL Server can prevent some or all of these read phenomena without the need to acquire locks, therefore greatly reducing blocking in the database. • Optimistic concurrency enabled via snapshot-based isolation • First time introduced in SQL Server 2005 Overview
  • 61. • Instead of blocking when unable to acquire a shared lock the reader retrieves, from the version store, the previously committed values of the set of rows it needs • This is the big difference between optimistic and pessimistic concurrency: with the former, writers and readers will not block each other • SQL Server still acquires locks during data modification operations, so writers will still block writers, and everything we've discussed previously about lock types, lock modes, and lock duration is still relevant to optimistic concurrency and row versioning • To support the storing of multiple older versions of rows in the version store may require a lot of additional disk space in the tempdb database Row Versioning
  • 62. How Row Versioning Works • Each old version of arow inthe version store contain a pointer to aneven older version of the same row. • All the old versions of a particular row are chained together in a linked list • SQL Server might need tofollow several pointers ina list to reach the right version. • The version store must retain versioned rows for as long as there are operations that might require them. • As long as a transaction is open, all versions of rows that havebeen modified bythat transaction must be kept in the version store • Version of rows read bya statement (RCSI) or transaction (SI) must be kept in the version store as long as that statement or transaction is open. • In addition, the version store must also retain versions of rows modified by now-completed transactions if there are anyolder versions of the same rows.
  • 63. • Read committed snapshot isolation (RCSI) • Queries return committed data as of the beginning of the current statement • Enabling RCSI • ALTER DATABASE AdventureWorks SET READ_COMMITTED_SNAPSHOT ON; • Snapshot isolation (SI) • Queries return committed data as of the beginning of the current transaction. • Enabling SI • ALTER DATABASE AdventureWorks SET ALLOW_SNAPSHOT_ISOLATION ON; Snapshot-based Isolation Levels
  • 64. Working with RCSI Time Transaction 1 Transaction 2 0 SELECT ListPrice FROM Products WHERE ProductID=10; (Returns 90) SELECT ListPrice FROM Products WHERE ProductID=10; (Returns 90) 1 BEGIN TRAN UPDATE Products SET UnitPrice= 100.00 WHERE ProductID=10; BEGIN TRAN 2 SELECT ListPrice FROM Products WHERE ProductID=10; (Returns 90) 3 COMMIT TRAN SELECT ListPrice FROM Products WHERE ProductID=10; (Returns 100) 4 COMMIT TRAN
  • 65. Working with SI Time Transaction 1 Transaction 2 0 SELECT ListPrice FROM Products WHERE ProductID=10; (Returns 90) SELECT ListPrice FROM Products WHERE ProductID=10; (Returns 90) 1 BEGIN TRAN UPDATE Products SET UnitPrice= 100.00 WHERE ProductID=10; SET TRANSACTION ISOLATION LEVEL SNAPSHOT 3 BEGIN TRAN 4 SELECT ListPrice FROM Products WHERE ProductID=10; (Returns 90) 5 COMMIT TRAN 6 SELECT ListPrice FROM Products WHERE ProductID=10; (Returns 90) 7 COMMIT TRAN 8 SELECT ListPrice FROM Products WHERE ProductID=10; (Returns 100)
  • 66. • The sys.dm_tran_version_store DMV, contains information about the actual rows in the version store. • The version store maintains a linked list of previously committed versions of each row in the database • The current row points to the next older row, which can point to an older row, and so on. • SQL Server manages the version store size automatically • Maintains a cleanup thread to make sure it does not keep versioned rows around longer than needed The Version Store
  • 67. • For SELECT statements running under SI • The version store retains the row versions until the transaction that modified the data completes • and the transactions containing any statements that reference the modified data complete. • For SELECT statements running under RCSI • a particular row version is no longer required • and is removed, once the SELECT statement has executed. • Two other important DMVs for observing snapshot transaction behavior are • sys.dm_tran_transactions_snapshot • sys.dm_tran_active_snapshot_database_transactions The Version Store
  • 68. • If application code invokes READ UNCOMMITTED isolation by using the NOLOCK hint or the equivalent READUNCOMMITTED hint, changing the database to RCSI will have no effect. • The NOLOCK hint will override the database setting, and SQL Server will continue to read the uncommitted (dirty) data. • The only solution is to update the code to remove the hints. The NOLOCK hint and RCSI
  • 69. • In most situations, RCSI is recommended over SI for several reasons • RCSI consumes less tempdb space than SI. • RCSI works with distributed transactions; SI does not. • RCSI does not produce update conflicts. • RCSI does not require any change in your applications • Use of SI can be considered in the following situations: • The probability is low that any transactions will have to be rolled back because of an update conflict. • Reports, based on long-running, multi-statement queries, need to be generated with point-in-time consistency. • Snapshot isolation provides the benefit of repeatable reads without being blocked by concurrent modification operations. RCSI vs SI
  • 70.
  • 71. • Introduction to SQL Server locking • Basics of locking • Advanced Concepts of locking • Controlling locking • Working with Pessimistic Concurrency • Optimistic Concurrency Summary
  • 73.