EF插入/更新:非常缓慢 [英] EF Inserts/Updates: Ridiculously Slow

查看:447
本文介绍了EF插入/更新:非常缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我前几天破解了最新版本的EF。我有一个项目,我希望在一次点击中插入大量数据。我想插入的记录数量约为2,000,000。表中有大约8个十进制列。所以,我在
EF中编写了代码。我用"添加"编写了代码,然后编写了"SaveChanges"。用了一个半小时写入XML的内容突然跳到了48小时的估计时间。 (注意:大概的1 1/2小时是从互联网上下载数据)。

I cracked open the latest version of EF the other day. I had a project where I wanted to insert a lot of data in one hit. The number of records I wanted to insert was about 2,000,000. There are about 8 decimal columns in the table. So, I wrote the code in EF. I wrote the code with an "Add", and then a "SaveChanges". What was taking an hour and half to write to XML suddenly jumped to an estimated time of 48 hours. (Note: most of the 1 1/2 hours is downloading data from the internet).

所以我进行了时间跟踪,看看每个动作花费的时间。 "添加" (将我要插入的类型的记录放入集合中)大约需要300毫秒,而"SaveChanges"则需要大约300毫秒。花了大约500毫秒。
这简直荒谬。

So I put an time trace in to see where each action was spending its time. The "Add" (putting the record of the type I want to insert in to the collection) was taking about 300 milliseconds and the "SaveChanges" was taking about 500 milliseconds. This is simply absurd.

当我更改代码以避免使用EF时 - 即对SQL服务器数据库执行直接INSERT语句,时间又回到了大约1半小时。我发现插入调用只需要大约2-3毫秒。

When I changed the code to avoid using EF - i.e. executing a straight INSERT statement against the SQL server database, the time went back down to about 1 1/2 hours. I found that the insert call was only taking about 2-3 milliseconds.

我认为EF可能在数据库级别上做得太多了。所以,我追踪了SQL Server。令我惊讶的是,我发现EF只是以与执行insert语句相同的方式执行insert语句。那么,为什么这么慢? EF有什么问题,什么时候会修复
?是否可以使用EF?

I thought perhaps EF was doing too much at the database level. So, I traced SQL Server. To my surprise, I found that EF was only executing insert statements in the same way I was executing the insert statement. So, why so slow? What's wrong with EF and when will it be fixed? Is it even possible to use EF?

推荐答案

我前几天破解了最新版本的EF。我有一个项目,我希望在一次点击中插入大量数据。

我想要插入的记录数量约为2,000,000。表格中有大约8个十进制列

他们现在有2米。 :)

2m of them did you now. :)

ORM(s) 适用于Web解决方案,N层解决方案,SOA解决方案,客户端/服务器解决方案或任何解决方案,其中使用EF的解决方案的生命周期是短暂的,并且没有大量数据飞来飞去。  

ORM(s)  are suited for Web solutions, N-tier solutions, SOA solutions , client/server solutions or any solution where the lifetime of the solution using EF is short-lived and not a whole lot of data is flying around.  

ORM(s)不适用于长时间运行的作业,这只会延长完成时间。  并且它不适用于推送2m  对象在插页上。 

ORM(s) are not meant for long running jobs, which would just extend the completion time.  And it's not meant for pushing 2m  objects on a insert. 


这篇关于EF插入/更新:非常缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆