I have simple table T which have Col1, Col2,Col3 and Col4. columns. Col1 is primary key. Now I am running some concurrent code,
Parallel.For(0, list.Count, new ParallelOptions { MaxDegreeOfParallelism = -1 }, j =>
{
var obj= list[j];
// Do some work
// UPDATE T SET Col2, Col3, Col4 WHERE Col1 =@Col1
Each value of col1 is different. But when I run this code I am getting too much deadlocks. Since Col1 is PK(Clustered Index), I am now confuse why this is happening?
In the deadlock vistim. I see Owner Mode: U
Whenever I run,
USE master
GO
DROP TABLE temp_sp_who2
GO
CREATE TABLE temp_sp_who2 ( SPID INT, Status VARCHAR(1000) NULL, Login SYSNAME NULL, HostName SYSNAME NULL, BlkBy SYSNAME NULL,DBName SYSNAME NULL,Command VARCHAR(1000) NULL, CPUTime INT NULL,DiskIO BIGINT NULL, LastBatch VARCHAR(1000) NULL, ProgramName VARCHAR(1000) NULL,SPID2 INT , RequestId INT NULL )
GO
INSERT INTO temp_sp_who2
EXEC sp_who2
SELECT BlkBy ,count(*) FROM
temp_sp_who2 WHERE CAST(BlkBy AS NVARCHAR(MAX)) <> CAST(SPID AS NVARCHAR(MAX))
GROUP By BlkBy
ORDER BY Count(*) DESC
GO
I see BlkBy is different with high numbers. But BlkBy is always high when app is running.
// Do some work
involve? Is it doing some other DML under the same transaction? Will the sessions have already done anything else in this transaction before they run this update?