Тёмный

SQL Server deadlock victim selection 

kudvenkat
Подписаться 835 тыс.
Просмотров 86 тыс.
50% 1

Text version of the video
csharp-video-tu...
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our RU-vid channel. Hope you can help.
/ @aarvikitchen5572
Slides
csharp-video-tu...
All SQL Server Text Articles
csharp-video-tu...
All SQL Server Slides
csharp-video-tu...
All Dot Net and SQL Server Tutorials in English
www.youtube.co...
All Dot Net and SQL Server Tutorials in Arabic
/ kudvenkatarabic
In this video we will discuss
1. How SQL Server detects deadlocks
2. What happens when a deadlock is detected
3. What is DEADLOCK_PRIORITY
4. What is the criteria that SQL Server uses to choose a deadlock victim when there is a deadlock
This is continuation to Part 78, please watch Part 78 before proceeding.
How SQL Server detects deadlocks
Lock monitor thread in SQL Server, runs every 5 seconds by default to detect if there are any deadlocks. If the lock monitor thread finds deadlocks, the deadlock detection interval will drop from 5 seconds to as low as 100 milliseconds depending on the frequency of deadlocks. If the lock monitor thread stops finding deadlocks, the Database Engine increases the intervals between searches to 5 seconds.
What happens when a deadlock is detected
When a deadlock is detected, the Database Engine ends the deadlock by choosing one of the threads as the deadlock victim. The deadlock victim's transaction is then rolled back and returns a 1205 error to the application. Rolling back the transaction of the deadlock victim releases all locks held by that transaction. This allows the other transactions to become unblocked and move forward.
What is DEADLOCK_PRIORITY
By default, SQL Server chooses a transaction as the deadlock victim that is least expensive to roll back. However, a user can specify the priority of sessions in a deadlock situation using the SET DEADLOCK_PRIORITY statement. The session with the lowest deadlock priority is chosen as the deadlock victim.
Example : SET DEADLOCK_PRIORITY NORMAL
DEADLOCK_PRIORITY
1. The default is Normal
2. Can be set to LOW, NORMAL, or HIGH
3. Can also be set to a integer value in the range of -10 to 10.
LOW : -5
NORMAL : 0
HIGH : 5
What is the deadlock victim selection criteria
1. If the DEADLOCK_PRIORITY is different, the session with the lowest priority is selected as the victim
2. If both the sessions have the same priority, the transaction that is least expensive to rollback is selected as the victim
3. If both the sessions have the same deadlock priority and the same cost, a victim is chosen randomly
SQL Script to setup the tables for the examples
Create table TableA
(
Id int identity primary key,
Name nvarchar(50)
)
Go
Insert into TableA values ('Mark')
Insert into TableA values ('Ben')
Insert into TableA values ('Todd')
Insert into TableA values ('Pam')
Insert into TableA values ('Sara')
Go
Create table TableB
(
Id int identity primary key,
Name nvarchar(50)
)
Go
Insert into TableB values ('Mary')
Go
Open 2 instances of SQL Server Management studio. From the first window execute Transaction 1 code and from the second window execute Transaction 2 code. We have not explicitly set DEADLOCK_PRIORITY, so both the sessions have the default DEADLOCK_PRIORITY which is NORMAL. So in this case SQL Server is going to choose Transaction 2 as the deadlock victim as it is the least expensive one to rollback.
-- Transaction 1
Begin Tran
Update TableA Set Name = Name + ' Transaction 1' where Id IN (1, 2, 3, 4, 5)
-- From Transaction 2 window execute the first update statement
Update TableB Set Name = Name + ' Transaction 1' where Id = 1
-- From Transaction 2 window execute the second update statement
Commit Transaction
-- Transaction 2
Begin Tran
Update TableB Set Name = Name + ' Transaction 2' where Id = 1
-- From Transaction 1 window execute the second update statement
Update TableA Set Name = Name + ' Transaction 2' where Id IN (1, 2, 3, 4, 5)
-- After a few seconds notice that this transaction will be chosen as the deadlock
-- victim as it is less expensive to rollback this transaction than Transaction 1
Commit Transaction
In the following example we have set DEADLOCK_PRIORITY of Transaction 2 to HIGH. Transaction 1 will be chosen as the deadlock victim, because it's DEADLOCK_PRIORITY (Normal) is lower than the DEADLOCK_PRIORITY of Transaction 2.

Опубликовано:

 

29 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 21   
@nikhilc3655
@nikhilc3655 6 лет назад
Kudvenkat you r doing a great job by educating people. I am sure its helping many people in shaping their career.. Keep it up & God bless!!👍👍
@ndbweurt34485
@ndbweurt34485 Год назад
Hi, I have a question. If read committed isolation only restrict dirty read and allow all other issues on database, then why it is acquiring lock on update statements also?
@nayanamtanuj
@nayanamtanuj 6 лет назад
7:41 Slide states Transaction 2 is the victim that is a typo, transaction 1 is the victim.
@pasito287
@pasito287 Месяц назад
useful information
@EldhoLikesIt
@EldhoLikesIt Месяц назад
Thanks Kudvenkat
@amarsharma3684
@amarsharma3684 3 года назад
sir how to resolve the System default Memory error 'Dufault' error code 701
@tinkudutta3961
@tinkudutta3961 4 года назад
Sir, if the 2nd deadlock priority is set to low instead of high, then what will happen? Will it be chosen as the deadlock victim and will be rolled back ? Bcz the other deadlock situation is normal. If deadlock priority is set to @deadlock_var, then what will happen? Sir kindly explain with the help of an example. It shall be of gr8 help.
@ramanathk695
@ramanathk695 7 лет назад
Mr. Kudvenkat, we are all thankful for your effort . Great explanation
@Csharp-video-tutorialsBlogspot
Thank you very much for taking time to give feedback. This means a lot. I am very glad you found the videos useful. I have organised all the Dot Net & SQL Server videos in to playlists, which could be useful to you ru-vid.complaylists?view=1&sort=dd If you need DVDs or to download all the videos for offline viewing please visit www.pragimtech.com/order.aspx Slides and Text Version of the videos can be found on my blog csharp-video-tutorials.blogspot.com Tips to effectively use my youtube channel. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-y780MwhY70s.html If you want to receive email alerts, when new videos are uploaded, please subscribe to my youtube channel. ru-vid.com If you like these videos, please click on the THUMBS UP button below the video. May I ask you for a favor. I want these tutorials to be helpful for as many people as possible. Please share the link with your friends and family who you think would also benefit from them. Good Luck Venkat
@hemantkumar-cl1yp
@hemantkumar-cl1yp 2 года назад
you got one more subscriber. easy explanation of difficult concepts
@alidakhil6028
@alidakhil6028 9 лет назад
Dear Mr. Kudvenkat, we are all thankful for your effort. I think there is something missing about Triggers, which is (Logon Trigger) when you talked about triggers. We hope that you can make one for that. Many thanks.
@kylewalker7919
@kylewalker7919 Год назад
Thank you 🙏 very easy to understand the way you set this up.
@kathleencita1
@kathleencita1 8 лет назад
Good Job thanks :)
@PinasPiliNa999
@PinasPiliNa999 9 лет назад
Also , please make a video on how to effectively use triggers in inventory management. There has been a debate on whether to use or not to use triggers, many does not want triggers, but also many prefer to use it. Hope you can make a stand on this by providing examples on different scenarios. Thanks!
@krzysztofs8535
@krzysztofs8535 7 лет назад
Thank you for educating the community. You are the best teacher I know. I go to the next video session immediately.
@nys8260
@nys8260 3 года назад
Thank you venkat, you are the best one in youtube
@raqibulAlam-su3jo
@raqibulAlam-su3jo 9 лет назад
Thanks
@pramodmaurya6610
@pramodmaurya6610 5 лет назад
Thanks for your knowledge sharing
@danezubro
@danezubro 4 года назад
Excelllent job ! Superb :) !
@aaronaaronaaron5922
@aaronaaronaaron5922 5 лет назад
you are really great, SIr :D
@viruandveera44
@viruandveera44 6 лет назад
Superb Sir
Далее
Logging deadlocks in sql server
4:25
Просмотров 78 тыс.
Radxa X4: An N100 Pi
20:48
Просмотров 43 тыс.
Трудности СГОРЕВШЕЙ BMW M4!
49:41
Просмотров 1,6 млн
ПОЮ ВЖИВУЮ🎙
3:19:12
Просмотров 880 тыс.
SQL Server deadlock analysis and prevention
8:58
Просмотров 107 тыс.
How do SQL Indexes Work
12:12
Просмотров 621 тыс.
Window functions in SQL Server
11:00
Просмотров 220 тыс.
SQL Server  Working with Locks
4:13
Просмотров 30 тыс.
Snapshot isolation level in sql server
9:16
Просмотров 126 тыс.