Sql server how to speed up row delete

Posted on 2015-01-20
Medium Priority
Last Modified: 2015-01-20
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?
Question by:esak2000
  • 3
  • 2
  • 2
  • +1
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40559279
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/

Author Comment

ID: 40559283
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
LVL 12

Expert Comment

ID: 40559299
use where exists instead it will be faster
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

LVL 12

Expert Comment

ID: 40559306
  FROM list a
                 FROM transactions b
                WHERE  b.sono = a.sono)
since this will not use temp tables for results
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40559362
Delete from list a
 WHERE a.sono in (select aa.sono From list aa
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.
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40559376
re:> I have a non-clustered index on the sono field for each table

You may need to rebuild these indexes.
LVL 54

Accepted Solution

Vitor Montalvão earned 2000 total points
ID: 40559386
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 
                  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.

Author Closing Comment

ID: 40559502
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!

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question