I’ve an access database with more than 500,000 records and I want to delete records in bulk. I get a list of ids that I need to delete based on certain condition and this could be from 1 ~ 5000. Right now my code looks like this
for ( i = 0 to 5000 )
{
CDatabase.ExecuteSQL( Delete from table where id = ItemToDelete(i) )
}
It takes almost 1 sec for each sql call.
Is there an option to do bulk delete or Is there a better way to do this ?
I haven’t done Access in years, but I’ve had the same challenges. Instead of getting all the records that satisfy some condition, I would first try issuing a DELETE WHERE “some condition”. It may still be slow, but would reduce the # of SQL invocations from 5000 to 1.
With such large #’s, a more extreme solution might be just dumping the whole table to a text file and writing a little program to reload it with only the ones you want. Probably not a good solution for a 1-time thing, but if you do this every month….
But in general, minimizing the # Sql calls is the best way to improve performance. Depending on the server, the delete operation may be a lot faster if the WHERE clause only includes primary key fields.