Deadlock while inserting into single table by multiple threads at the same time

Malkesh Sheth 46 Reputation points
2021-12-29T06:01:28.81+00:00

Hello,
I am facing deadlock issue while insert into single table using multiple threads. It is simple insert SP which has code like

Insert into table A output inserted.value into @inserted values(x,y,z).
select @value = value from @inserted

This table A has one PK clustered index on identity column which we get using above output clause and return to app by using an output parameter. Other information to consider is that this sp takes only a second to execute. This table has one PK clustered index and 9 non clustered indexes.

Since this insert sp called by multiple thread at the same time we are facing massive deadlock with wait resource = Object which is the same table. Below is partial deadlock information extracted from deadlock xml file.

-<objectlock id="lock14d48a27e80" associatedObjectId="1013578649" mode="X" objectname="dbo.A" dbid="5" subresource="FULL" objid="1013578649" lockPartition="0">
-<owner-list>
<owner id="process15250169848" mode="IX" requestType="wait"/>
</owner-list>
-<waiter-list>

<waiter id="process151ae699088" mode="X" requestType="wait"/>

</waiter-list>

</objectlock>

-<objectlock id="lock14d48a27e80" associatedObjectId="1013578649" mode="X" objectname="dbo.A" dbid="5" subresource="FULL" objid="1013578649" lockPartition="0">

-<owner-list>

<owner id="process15250169848" mode="IX" requestType="wait"/>

</owner-list>

-<waiter-list>

<waiter id="process153993c2ca8" mode="X" requestType="wait"/>

</waiter-list>

Appreciate if any help from you guys.

Developer technologies Transact-SQL
SQL Server Other
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2021-12-29T06:42:33.503+00:00

    Below is partial deadlock information extracted from deadlock xml file.

    Partial informations of the deadlock graph are useless, especially the part you picked out.
    Also you provided to less on information.
    What's the table design and SP code? How many records does the table contains?


  2. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-12-30T07:47:56.267+00:00

    The index on line 8 is your problem. Either drop this index entirely, or drop it and recreate it with ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON. (These are the default settings.)

    If there are more indexes with 0 in any of these columns, you need to perform the same operation with these indexes.

    Because of these settings, the INSERT statement needs to take a table lock. With all indexes permitting row locks, you will get row locks, and you should not see deadlocks from concurrents inserts. You may still get latch contention, but that's another story.

    350 indexes are just way too many. Although, some of these may have is_hypothetical = 1, which means that they don't really exist, and I should have included a filter on this column. How many rows do you get with:

    SELECT  name, index_ix, type, allow_row_locks, allow_page_locks 
    FROM    [ORCA-QUEUE].sys.indexes
    WHERE  object_id = object_id('[ORCA-QUEUE].CPRTS.Job')
      AND  is_hypothetical = 0
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.