- WITH(NOLOCK) is used to unlock the data which is locked by the transaction that is not yet committed. This command is used before SELECT statement.
- When the transaction is committed or rolled back then there is no need to use NOLOCK function because the data is already released by the committed transaction.
- Syntax: WITH(NOLOCK)
- Example:
SELECT * FROM EmpDetails WITH(NOLOCK) - WITH(NOLCOK) is similar as READ UNCOMMITTED
- A deadlock is a situation where 2 spids have data locked and cannot release their lock until the opposing spid releases their lock. Depending on the severity of the deadlock, meaning the amount of data that is locked and the number of spids that are trying to access the same data, an entire chain of spids can have locks and cause a number of deadlocks, resulting in a performance issue.
- Deadlocks can be identified by Profiler in either textual, graphical or XML format.
- Deadlocks are a performance problem because they can prevent 2 or more processes from being able to process data. A deadlock chain can occur and impact hundreds of spids based on the data access patterns, number of users, object dependencies, etc.
- Deadlocks could require a database design change, T-SQL coding change to access the objects in the same order, separating reporting and OLTP applications, including NOLOCK statements in SELECT queries that can accept dirty data, etc.
Deadlocks and blocking are two common concurrency-related issues that
can occur in database systems, including SQL Server.
- Deadlock:
· Definition: A deadlock occurs when two or
more transactions are waiting for resources (such as locks on rows, tables, or
other database objects) held by each other, causing them to wait indefinitely.
This situation creates a circular dependency where none of the transactions can
proceed.
· Example: Transaction A holds a lock on Table X
and needs a lock on Table Y, while Transaction B holds a lock on Table Y and
needs a lock on Table X. Both transactions are waiting for each other to
release the required locks, resulting in a deadlock.
·
Impact: Deadlocks can lead to transactions being
terminated or rolled back by the database management system (DBMS), affecting
application performance and data consistency.
- Blocking:
· Definition: Blocking occurs when one
transaction holds a lock on a resource and another transaction attempts to
access the same resource but is forced to wait until the lock is released. The
waiting transaction is said to be blocked by the transaction holding the lock.
· Example: Transaction A updates rows in
Table X and holds exclusive locks on those rows. Meanwhile, Transaction B
attempts to read or update the same rows in Table X but is blocked because it
cannot acquire the necessary locks due to Transaction A's exclusive locks.
· Impact: Blocking can lead to decreased
application performance, as blocked transactions may experience delays or
timeouts waiting for locks to be released. It can also cause cascading effects
if multiple transactions are blocked by each other.
To find and diagnose deadlocks in SQL Server, you can use various
methods and tools provided by SQL Server itself. Here are some common
approaches:
- SQL Server
Management Studio (SSMS):
· Use
the Activity Monitor: In SSMS, go to the "Management" node,
right-click on "Activity Monitor," and select "Active
Deadlocks." This will show you the current deadlock graph and details.
· Review
the Error Log: SQL Server logs deadlock information in the error log. Look for
messages with Event ID 1205, which indicate deadlock occurrences.
- SQL Server
Profiler:
· Use
SQL Server Profiler to capture deadlock events. Create a trace that includes
the "Deadlock Graph" event or use the "TSQL_Locks"
template, which includes deadlock events by default.
- Extended Events:
· Use
Extended Events to capture deadlock information. Create an Extended Events
session with events like "xml_deadlock_report" to collect deadlock
graphs and related details.
- System Health
Session:
· SQL
Server maintains a System Health Extended Events session by default, which
includes deadlock information. Query the system health session to retrieve
deadlock events and details.
- Querying DMVs:
· Use
Dynamic Management Views (DMVs) such as sys.dm_exec_requests and sys.dm_tran_locks
to monitor active transactions and locks, which can provide insights into
potential deadlock situations.
- Deadlock Graphs:
· When
a deadlock occurs, SQL Server generates a deadlock graph that illustrates the
dependencies and conflicts between the involved transactions. You can retrieve
deadlock graphs from various sources mentioned above or by querying the
system_health Extended Events session.
locks/processid and you will be able to see all the locks related information.
Expand-server-management-double click Activity Monitor.
on left side you have three options to choose from, select those options and you can see all the locks related information.
3. select * from sys.sysprocesses ( in sql server 2005)
5. sp_who2 will also give you some good information.
Extent : Contiguous group of eight data pages or index pages.
Key: Row lock within an index.
Page: 8-kilobyte (KB) data page or index page.
RID :Row ID. Used to lock a single row within a table.
Table: Entire table, including all data and indexes.
Shared (S) – more than one Query can access the object.
Exclusive lock (X) – only one Query can access the object.
Update lock (U)
Intent share (IS)
Intent Exclusive (IX)
level 0. READ COMMITTED
level 1. READ UNCOMMITTED
level 2. REPEATABLE READ
level 3. SERIALIZABLE
This data will also read uncommitted data. Data which you have not comitted, you can still read that data.
Under this category you will not be able to read uncomitted data, this is also called as dirty data. Under this we will have shared locks.
create table example1 ( eid int, ename varchar(10))
insert into example1 values ( 1, ‘example’)
go
There are 3 kinds of locks in SQL Server
i.) Shared locks - they are used for operations which do not allow any change or update of data. For e.g. SELECT.
ii.) Update locks - they are used when SQL Server wants to modify a page. The update page lock is then promoted to an exclusive page lock before actually making the changes.
iii.) Exclusive locks - they are used for the data modification operations. For e.g. UPDATE, INSERT, or DELETE.
No comments:
Post a Comment