Deleting a large number of records takes a VERY long time

做~自己de王妃 提交于 2019-12-10 12:32:05

问题


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,


回答1:


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.




回答2:


You should look at EntityFramework.Extended it was created to help with both bulk deletions and updates.

Using it, you could simply do:

msdc.CpuMeasurements.Delete(curr => curr.Timestamp < oldestAllowedTime);



回答3:


The reason for this is that you execute a DB update for every single record. You need to do a bulk update.

EntityFramework.extended can handle this scenario.




回答4:


Deleting huge amounts of data can take a long time.

You might have to move the sql out of your application and run it as a single sql script via SQL Server Agent. It could be run, for example, once a day during the quietest period.



来源:https://stackoverflow.com/questions/16442286/deleting-a-large-number-of-records-takes-a-very-long-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!