Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improving performance of Sql Delete

We have a query to remove some rows from the table based on an id field (primary key). It is a pretty straightforward query:

delete all from OUR_TABLE where ID in (123, 345, ...)

The problem is no.of ids can be huge (Eg. 70k), so the query takes a long time. Is there any way to optimize this? (We are using sybase - if that matters).

like image 212
amit Avatar asked Feb 23 '09 10:02

amit


People also ask

Why delete is slow in SQL?

In theory, we would expect the best delete performance for a table without any indexes—as it is for insert . If there is no index, however, the database must read the full table to find the rows to be deleted. That means deleting the row would be fast but finding would be very slow.


3 Answers

I'm wondering if parsing an IN clause with 70K items in it is a problem. Have you tried a temp table with a join instead?

like image 30
JohnOpincar Avatar answered Oct 16 '22 18:10

JohnOpincar


There are two ways to make statements like this one perform:

  1. Create a new table and copy all but the rows to delete. Swap the tables afterwards (alter table name ...) I suggest to give it a try even when it sounds stupid. Some databases are much faster at copying than at deleting.

  2. Partition your tables. Create N tables and use a view to join them into one. Sort the rows into different tables grouped by the delete criterion. The idea is to drop a whole table instead of deleting individual rows.

like image 97
Aaron Digulla Avatar answered Oct 16 '22 17:10

Aaron Digulla


Consider running this in batches. A loop running 1000 records at a time may be much faster than one query that does everything and in addition will not keep the table locked out to other users for as long at a stretch.

If you have cascade delete (and lots of foreign key tables affected) or triggers involved, you may need to run in even smaller batches. You'll have to experiement to see which is the best number for your situation. I've had tables where I had to delete in batches of 100 and others where 50000 worked (fortunate in that case as I was deleting a million records).

But in any even I would put my key values that I intend to delete into a temp table and delete from there.

like image 42
HLGEM Avatar answered Oct 16 '22 19:10

HLGEM