Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a large number of records takes a VERY long time

I have a database table (running on SQL Server 2012 Express) that contains ~ 60,000 rows.

I am using the following code to purge old rows:

//Deleting CPU measurements older than (oldestAllowedTime)
var allCpuMeasurementsQuery = from curr in msdc.CpuMeasurements where 
    curr.Timestamp < oldestAllowedTime select curr;
foreach (var cpuMeasurement in allCpuMeasurementsQuery)
{
  msdc.CpuMeasurements.Remove(cpuMeasurement);
}

When the number of deleted rows is large (~90% or more of the records in the tables are being deleted) the operation takes exceptionally long. It takes about 30 minutes to finish this operation on an relatively strong machine (Intel I5 desktop).

  1. does this seem like a normal behavior?

  2. any ideas about what I can do to reduce the operation's time?

Thanks,

like image 748
OSH Avatar asked May 08 '13 13:05

OSH


People also ask

Why delete is taking long time in Oracle?

Deleting lots of rows can be slow. And there's a chance it'll take even longer because another session has locked the data you want to remove.

Why does Excel take so long to delete filtered rows?

Well, deleting rows on a filtered range can be a very labor intensive process for Excel. If the data is not sorted then Excel has to go through each set of visible rows and delete the row sections one by one. This process takes longer with larger data sets that contain more rows, columns, and formulas.

How do you delete 10000 records in SQL?

If you need to remove 10 million rows and have 1 GB of log space available use Delete TOP(10000) From dbo. myTable (with your select clause) and keep running it till there are no more rows to delete.


Video Answer


1 Answers

Entity framework is not very good at handling bulk operations like this. You should use ExecuteStoreCommand to execute SQL directly against the data source in situations like this.

var deleteOld = "DELETE FROM CpuMeasurements WHERE curr.Timestamp < {0}";
msdc.ExecuteStoreCommand(deleteOld, oldestAllowedTime);

By doing so you don't need to load the entities into memory (just to delete them) and issue thousands of delete commands to the database.

like image 182
Magnus Avatar answered Sep 17 '22 04:09

Magnus