Sql server how to speed up row delete

I have the following sql server query:

delete a from list a
inner join transactions b
on a.sono = b.sono

the list table has around 500,000 rows and the transaction table has around 5,000,000 rows
I have a non-clustered index on the sono field for each table. The sono field is not unique in each table.

the query is taking over 3 hours, is there any way to do the delete faster?
esak2000Asked:
Who is Participating?
 
Vitor MontalvãoConnect With a Mentor MSSQL Senior EngineerCommented:
If you already has an index then the only way is to run it by batches.
You can accomplish that with the help of EXISTS clause. The following example is for delete within a batch of 1000 transactions but you can change the number:
DELETE  FROM list a 
  WHERE EXISTS (SELECT TOP 1000
                  FROM transactions b
                 WHERE  b.sono = a.sono)

Open in new window

If there's a date field you can create batches by date instead of number of records.
0
 
Mike EghtebasDatabase and Application DeveloperCommented:
Truncate is faster than delete. I am looking for more information on that .

Please take a look at:  https://databaseskills.wordpress.com/2014/05/20/compare-truncate-and-delete-statement/
0
 
esak2000Author Commented:
truncate will remove all the data from the list table, I only want to remove the rows from the list table that match the transaction table
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
FarWestCommented:
use where exists instead it will be faster
0
 
FarWestCommented:
Delete
  FROM list a
 WHERE EXISTS (SELECT 1
                 FROM transactions b
                WHERE  b.sono = a.sono)
since this will not use temp tables for results
0
 
Mike EghtebasDatabase and Application DeveloperCommented:
Delete from list a
 WHERE a.sono in (select aa.sono From list aa
Intersect
Select b.sono FROM transactions b)

I am not even sure such a query works, but, you can paste in query pane and see if you are getting a better estimated execution plan.
0
 
Mike EghtebasDatabase and Application DeveloperCommented:
re:> I have a non-clustered index on the sono field for each table

You may need to rebuild these indexes.
0
 
esak2000Author Commented:
The "WHERE EXISTS" perhaps would work well on a smaller table, but for my query it was taking over an hour before I cancelled it.
Also, it may be correct that a reindex would help, but that would also take a long time. The loop worked so I'm going with that.

Thank you to all who replied!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.