Solved

Sql server how to speed up row delete

Posted on 2015-01-20
8
216 Views
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?
0
Comment
Question by:esak2000
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 33

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/
0
 

Author Comment

by:esak2000
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
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40559299
use where exists instead it will be faster
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40559306
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40559362
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
 
LVL 33

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.
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 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 
  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
 

Author Closing Comment

by:esak2000
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!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now