SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Avoid DEADLOCK for concurrent DELETE


Avoid DEADLOCK for concurrent DELETE

Author
Message
VASUforSQL
VASUforSQL
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 Visits: 25
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)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)

Group: General Forum Members
Points: 101547 Visits: 18188
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 Modens 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)
VASUforSQL
VASUforSQL
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 Visits: 25
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
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65944 Visits: 20214
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
gofrancesc
gofrancesc
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1367 Visits: 812
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
TheSQLGuru
TheSQLGuru
SSC Guru
SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)

Group: General Forum Members
Points: 52056 Visits: 8795
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 on googles mail service
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search