Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Avoid DEADLOCK for concurrent DELETE Expand / Collapse
Author
Message
Posted Monday, February 25, 2013 11:17 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 8, 2013 4:42 AM
Points: 28, Visits: 24
Hi ALL,



I have a table called Products which has many columns.

It is a temporary table used for reporting purpose. The data will be processed to this table concurrently by multiple user requests.(seperate Stored procedure to make DML operations to this table)

Table Structure: Create table Products (instance uniqueidentifier, inserted datetime, col1, col2,...)

Inserted column will be populated with GETDATE() to have when the data are inserted. And instance column have the newid() value. one user request will have one unique id; may have million rows. The below are the queries which will be executed concurrently, which causing the deadlock. Please advise me

Query1: "Set transaction isolation readuncommitted Delete P from Products (Nolock) where instance = 'XXXX-xxx-xxx-xx'"

Query2: "Set transaction isolation readuncommitted Delete P from Products (Nolock) where inserted<=DATEADD(hh, -10, getdate())"

Note: The nonclustered index is created on instance column.

PLEASE ADVISE me which lock I can use in this scenario.

Note I couldnt able to primary key as it is consuming time when I insert 10 million rows to the table (this for one transaction; there are 20 concurrent transations). The report should be generated sooner. And my procedure has multiple 35 DML statments, there are around 15 DELETE statements for instance column with other columns( DElete from table where instance = @instance and col1 = @col1)
Post #1423714
Posted Monday, February 25, 2013 12:13 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:21 PM
Points: 13,083, Visits: 11,918
VASUforSQL (2/25/2013)
Hi ALL,



I have a table called Products which has many columns.

It is a temporary table used for reporting purpose. The data will be processed to this table concurrently by multiple user requests.(seperate Stored procedure to make DML operations to this table)

Table Structure: Create table Products (instance uniqueidentifier, inserted datetime, col1, col2,...)

Inserted column will be populated with GETDATE() to have when the data are inserted. And instance column have the newid() value. one user request will have one unique id; may have million rows. The below are the queries which will be executed concurrently, which causing the deadlock. Please advise me

Query1: "Set transaction isolation readuncommitted Delete P from Products (Nolock) where instance = 'XXXX-xxx-xxx-xx'"

Query2: "Set transaction isolation readuncommitted Delete P from Products (Nolock) where inserted<=DATEADD(hh, -10, getdate())"

Note: The nonclustered index is created on instance column.

PLEASE ADVISE me which lock I can use in this scenario.

Note I couldnt able to primary key as it is consuming time when I insert 10 million rows to the table (this for one transaction; there are 20 concurrent transations). The report should be generated sooner. And my procedure has multiple 35 DML statments, there are around 15 DELETE statements for instance column with other columns( DElete from table where instance = @instance and col1 = @col1)


You should look into isolation instead of locking. Snapshot isolation sounds like it would be a good fit.

http://msdn.microsoft.com/en-us/library/ms173763.aspx


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1423737
Posted Tuesday, February 26, 2013 2:27 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 8, 2013 4:42 AM
Points: 28, Visits: 24
HI I tried with Snapshot. It still raising DEADLOCK issue. and some records are not getting updated properly. Please suggest:
1. Isolation Level
2. Locks for DELETE statments
Post #1423937
Posted Tuesday, February 26, 2013 3:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:56 AM
Points: 7,120, Visits: 13,491
VASUforSQL (2/25/2013)
Hi ALL,



I have a table called Products which has many columns.

It is a temporary table used for reporting purpose. The data will be processed to this table concurrently by multiple user requests.(seperate Stored procedure to make DML operations to this table)

....

And instance column have the newid() value. one user request will have one unique id; may have million rows. ...


Instead of forcing all users to share a single huge dynamic "temporary" table with all of the hardship you are currently facing, why not give them one each, with none?


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1423976
Posted Tuesday, February 26, 2013 5:22 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, May 8, 2014 6:52 AM
Points: 634, Visits: 809
Hello,
maybe you can try to delete rows in small blocks, replacing your DELETE instruction by something like (I cannot check the syntax now)
Delete TOP 1000 Products where instance = 'XXXX-xxx-xxx-xx'
WHILE @@rowcount > 0
Delete TOP 1000 Products where instance = 'XXXX-xxx-xxx-xx'

It should help you to minimize deadlocks.

Regards,
Francesc
Post #1424021
Posted Wednesday, February 27, 2013 6:20 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 3:19 AM
Points: 4,320, Visits: 6,113
recommendations:


1) see here:

http://blogs.msdn.com/bartd/archive/2006/09/09/Deadlock-Troubleshooting_2C00_-Part-1.aspx

Note there are 2 additional parts to this blog series.

2) as someone else said, you will likely need to batch up the DELETE.

3) you might also need a NC index on the date you are using for one of the deletes.

4) get a professional on board to help you. That sounds like a very complicated process and you might need some difficult refactoring to get everything to play nicely.


Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #1424489
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse