实体框架如何处理大量记录? [英] How entity framework works for large number of records?

查看:34
本文介绍了实体框架如何处理大量记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到一个未回答的问题这里.

I see already a un-answered question here on.

我的问题是 -

EF 真的为大型应用做好生产准备了吗?

Is EF really production ready for large application?

问题源于这些基本问题-

The question originated from these underlying questions -

  1. EF 将所有记录拉入内存然后执行查询手术.当表有大约 1000 条记录时,EF 会如何表现?
  2. 为了简单的编辑,我必须拉出记录编辑它然后使用 SaveChanges()
  3. 推送到数据库
  1. EF pulls all the records into memory then performs the query operation. How EF would behave when table has around ~1000 records?
  2. For simple edit I have to pull the record edit it and then push to db using SaveChanges()

推荐答案

我遇到了类似的情况,我们有一个包含许多表的大型数据库,每个表有 7-1000 万条记录.我们使用实体框架来显示数据.为了获得出色的表现,这是我学到的;实体框架的 10 条黄金法则:

I faced a similar situation where we had a large database with many tables 7- 10 million records each. we used Entity framework to display the data. To get nice performance here's what I learned; My 10 Golden rules for Entity Framework:

  1. 了解仅在需要实际记录时才调用数据库.所有操作仅用于进行查询(SQL),因此尝试仅获取一条数据,而不是请求大量记录.尽可能地修剪 fetch size

  1. Understand that call to database made only when the actual records are required. all the operations are just used to make the query (SQL) so try to fetch only a piece of data rather then requesting a large number of records. Trim the fetch size as much as possible

是的(在某些情况下,存储过程是更好的选择,它们并不像某些人认为的那么邪恶),您应该在必要时使用存储过程.将它们导入您的模型并为它们导入函数.您也可以直接调用它们 ExecuteStoreCommand()、ExecuteStoreQuery<>().函数和视图也是如此,但 EF 有一种非常奇怪的调用函数SELECT dbo.blah(@id)"的方式.

Yes, (In some cases stored procedures are a better choice, they are not that evil as some make you believe), you should use stored procedures where necessary. Import them into your model and have function imports for them. You can also call them directly ExecuteStoreCommand(), ExecuteStoreQuery<>(). Same goes for functions and views but EF has a really odd way of calling functions "SELECT dbo.blah(@id)".

EF 在必须填充具有深层层次结构的实体时执行速度较慢.对具有深层层次结构的实体要格外小心

EF performs slower when it has to populate an Entity with deep hierarchy. be extremely careful with entities with deep hierarchy

有时当您请求记录并且不需要修改它们时,您应该告诉 EF 不要监视属性更改 (AutoDetectChanges).这样记录检索速度要快得多

Sometimes when you are requesting records and you are not required to modify them you should tell EF not to watch the property changes (AutoDetectChanges). that way record retrieval is much faster

数据库的索引很好,但在 EF 的情况下它变得非常重要.用于检索和排序的列应正确编入索引.

Indexing of database is good but in case of EF it becomes very important. The columns you use for retrieval and sorting should be properly indexed.

当你的模型很大时,VS2010/VS2012 模型设计师会变得非常疯狂.所以把你的模型分解成中等大小的模型.存在一个限制,即来自不同模型的实体即使指向数据库中的同一个表也不能共享.

When you model is large, VS2010/VS2012 Model designer gets real crazy. so break your model into medium sized models. There is a limitation that the Entities from different models cannot be shared even though they may be pointing to the same table in the database.

当您必须在不同地方对同一实体进行更改时,请使用同一实体,进行更改并仅保存一次.重点是避免检索相同的记录,进行更改和更改.保存多次.(实际性能提升提示).

When you have to make changes in the same entity at different places, use the same entity, make changes and save it only once. The point is to AVOID retrieving the same record, make changes & save it multiple times. (Real performance gain tip).

当您只需要一两列中的信息时,尽量不要获取完整的实体.您可以直接执行您的 sql 或拥有一个迷你实体.您可能还需要在应用程序中缓存一些常用数据.

When you need the info in only one or two columns try not to fetch the full entity. you can either execute your sql directly or have a mini entity something. You may need to cache some frequently used data in your application also.

交易很慢.小心他们.

SQL Profiler 或任何查询分析器都是您的朋友.在开发应用程序时运行它以查看 EF 发送到数据库的内容.当您在应用程序中使用 LINQ 或 Lambda 表达式执行连接时,EF 通常会生成 Select-Where-In-Select 样式的查询,该查询可能并不总是表现良好.如果您发现任何此类情况,请卷起袖子,在 DB 上执行连接并让 EF 检索结果.(我忘了这个,最重要的!)

SQL Profiler or any query profiler is your friend. Run it when developing your application to see what does EF sends to database. When you perform a join using LINQ or Lambda expression in ur application, EF usually generates a Select-Where-In-Select style query which may not always perform well. If u find any such case, roll up ur sleeves, perform the join on DB and have EF retrieve results. (I forgot this one, the most important one!)

如果你牢记这些事情,EF 应该提供与普通 ADO.NET 几乎相似的性能,如果不一样的话.

if you keep these things in mind EF should give almost similar performance as plain ADO.NET if not the same.

这篇关于实体框架如何处理大量记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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