Question

I am experiencing an unclear sql server 2008 deadlock.

I have two tables:

T1: ID – bigint (PK), JOBID – bigint, X- bigint
T2: ID – bigint (PK), JOBID – bigint, X- bigint

The sql server error log describes the following:

Spid 233 is running this query: 
    SELECT TOP(500) FROM T1.X INNER JOIN T2 ON T1.X = T2.X WHERE T2.ID>-1 AND T2.ID<=10000
Spid 234 is running this query: 
    DELETE FROM T2 WHERE (X=1) OR (X=7) OR ( X=1233) OR X(=7878) OR (X=33) OR (X=776) OR (X=112)
Spid 233 is waiting for a Shared KEY lock on index t2.PK_T2.  (Spid 234 holds a conflicting X lock.)
Spid 234 is waiting for an eXclusive KEY lock on index t2.PK_T2.  (Spid 233 holds a conflicting S lock.)

PK_X is a Clustered ascending index on the identity column (ID which is auto increment column). T2 also has another index which is a non clustered index on JOBID and X.

How come both processes are owners of a lock on the same object (T2.PK_T2)?

Please help, Doron

Was it helpful?

Solution

How come both processes are owners of a lock on the same object (T2.PK_T2)?

The locks are not on the whole index. They are key locks and will be on different keys (rows) within the index.

OTHER TIPS

In your example, 233 is trying to pull the first 500 rows.

234 is trying to delete records in the same range.

234 needs an exclusive lock on the rows it deletes, so it locks down for example row 200.

233 is trying to read row 200 and pauses to wait for 234 to finish its lock, and moves on to the other 300 rows.

234 tries to delete more rows and 233 is blocking it by selecting them.

At this point 233 is blocking 234, and 234 is also blocking 233, so it's a deadlock.

It's a row-level locking issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top