Avoid DEADLOCK for concurrent DELETE

  • 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)

  • 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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

  • 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

  • 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

  • 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

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply