Delete statement in SQL is very slow

前端 未结 15 696
自闭症患者
自闭症患者 2020-12-22 23:23

I have statements like this that are timing out:

DELETE FROM [table] WHERE [COL] IN ( \'1\', \'2\', \'6\', \'12\', \'24\', \'7\', \'3\', \'5\')
15条回答
  •  一整个雨季
    2020-12-23 00:19

    Preventive Action

    Check with the help of SQL Profiler for the root cause of this issue. There may be Triggers causing the delay in Execution. It can be anything. Don't forget to Select the Database Name and Object Name while Starting the Trace to exclude scanning unnecessary queries...

    Database Name Filtering

    Table/Stored Procedure/Trigger Name Filtering

    Corrective Action

    As you said your table contains 260,000 records...and IN Predicate contains six values. Now, each record is being search 260,000 times for each value in IN Predicate. Instead it should be the Inner Join like below...

    Delete K From YourTable1 K
    Inner Join YourTable2 T on T.id = K.id
    

    Insert the IN Predicate values into a Temporary Table or Local Variable

提交回复
热议问题