I have an application which will be chosen as the "deadlock victim" in SQL Server. There are multiple threads which try to execute below queries.
Query
merge Table_X as target
using (values ('14410')) as source (CUST_ID) on target.CUST_ID = '14410'
when matched then
update
SET CUST_NAME = 'xyz', CLOSE_DATE = NULL,
xx = 2, COMPLETE = 'No',
qwert = CASE WHEN qwert is null and 'Low' = 'High' THEN getDate() ELSE null END,
ACTIVE = 1, xcount = '913af80db3f424e34a9055e0ea9bc391'
when not matched then
INSERT (CUST_ID, CUST_NAME, OPEN_DATE, CLOSE_DATE, xx, COMPLETE, qwert, ddd, ACTIVE, xcount )
VALUES ('14410', 'U.S. Robotics and Mechanical Men', '2007-08-31 15:14:23.0', NULL, 2, 'No', NULL, 0, 1, '913af80db3f424e34a9055e0ea9bc391') ;
DECLARE @54229 numeric(19,0)
SET @54229 = ( SELECT id from Table_X where CUST_ID = '14410' )
insert into Table_Y (xyz, abc, ax, ay, az, bx, bxz, bz, UPDATED, abcd)
select
xyz, abc, ax, ay, az, bx, bxz, bz, UPDATED, abcd
from
Table_Z
where
abcd = @54229
merge Table_Z as target
using (values (@54229)) as source (abcd) on target.abcd = @54229
when matched then
UPDATE
SET xyz = 1.1, abc = 1.1, ax = 1.1, ay = 1.1, az = 1.1,
bx = 1.1, bxz = 'Low', bz = 1.1, UPDATED = getdate()
when not matched then
INSERT (xyz, abc, ax, ay, az, IS_FORCE_EDD, bx, bxz, bz, UPDATED, abcd)
VALUES(1.1, 1.1, 1.1, 1.1, 1.1, 0, 1.1,'Low', 1.1, getdate(), @54229);
INSERT INTO Table_A(ACTION, DATE_TIME, PROFILE_ID, USER_ID)
VALUES('Profile Change', getdate(), @54229, 1)
Can anyone explain this deadlock diagram?
And provide me the solution to prevent this deadlock.
I have read multiple stuff and tried with (nolock)
, isolation level
, but I am not getting proper solution.
Is there any indexes related issue possible for a deadlock victim?