Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to optimize the delete statement

Author  Topic 

jessiefun
Starting Member

35 Posts

Posted - 2010-02-24 : 05:57:02
There is a delete statement as below:
DELETE
FROM tbTestA
WHERE (tbTestA.ID IN
(SELECT ID FROM tbTestB WHERE tbTestB.Name = 'Jessie'))AND(tbTestA.Name IS Null)

PS. I have query the number for the records that need deleting, not more than 100. While it executes fast when query the results. However executing the "delete" statement, much more time will be needed, about 4-5 minutes.

So my question is: is there any way to improve deleting - performance?

Thanks guys!

Sachin.Nand

2937 Posts

Posted - 2010-02-24 : 06:08:48
Try this

DELETE TA
FROM tbTestA TA inner join tbTestB TB on TA.ID=TB.ID and TB.Name='Jessie' and TA.Name IS NULL



Please take a backup before applying the above delete statement in the production enviroment just in case.

PBUH
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2010-02-24 : 07:59:01
Does Select run Faster on this query? then Delete should be faster as well. Do you have lots of indexes in this tables that can slow down speed as it has to delete in index pages as well.
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2010-02-24 : 09:13:43
have you (m)any triggers for CASCADE deletes?
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-24 : 11:27:35
Or lots of Foreign Key constraints? (worse still if they are FKeys with cascade deletes)
Go to Top of Page
   

- Advertisement -