Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I SET DEADLOCK_PRIORITY in my stored procedure?

I am unsure as to where I can use the

SET DEADLOCK_PRIORITY...

command in my stored procedure. Does it need to be before I begin a transaction? Or can it be anywhere in the transaction?

Thanks all!

like image 744
ManOnAMission Avatar asked May 16 '13 19:05

ManOnAMission


People also ask

What is set Deadlock_priority?

SET DEADLOCK_PRIORITY is set at execute or run time and not at parse time. This syntax is not supported in dedicated SQL pools in Azure Synapse Analytics. It is supported in serverless SQL pools in Azure Synapse Analytics.

How do we select deadlock victims?

If the priority of all the transactions involved in the deadlock is same, the transaction that is least expensive is chosen as deadlock victim. In the unlikely event that both the deadlock priority and the cost of the transactions involved in the deadlock are equal, then deadlock victim will be selected randomly.

How can we avoid deadlock while updating SQL Server?

Update lock (U) is used to avoid deadlocks. Unlike the Exclusive lock, the Update lock places a Shared lock on a resource that already has another shared lock on it.


1 Answers

I'm not sure I understand the question: you can change the priority wherever you need to. If you're unsure then you can just put it at the start of the procedure, unless perhaps it's a very long procedure and there's only one specific query that is prone to deadlocks.

Although a better solution would probably be to avoid the deadlock if possible.

You may also want to note that any priority change inside a stored procedure is reset to the priority of the calling session when the procedure exits:

set deadlock_priority high
go
select deadlock_priority from sys.dm_exec_sessions where session_id = @@spid
go

create proc dbo.p 
as
begin
select deadlock_priority as 'PriorityBefore' from sys.dm_exec_sessions where session_id = @@spid 
set deadlock_priority low
select deadlock_priority as 'PriorityAfter' from sys.dm_exec_sessions where session_id = @@spid 
end
go

exec dbo.p

select deadlock_priority from sys.dm_exec_sessions where session_id = @@spid

drop proc dbo.p
go

set deadlock_priority normal
go
like image 105
Pondlife Avatar answered Sep 22 '22 04:09

Pondlife