有多少包括我可以在上的EntityFramework使用对象集保留的表现? [英] How many Include I can use on ObjectSet in EntityFramework to retain performance?

查看:126
本文介绍了有多少包括我可以在上的EntityFramework使用对象集保留的表现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的LINQ查询我的个人资料页:

I am using the following LINQ query for my profile page:

var userData = from u in db.Users
                        .Include("UserSkills.Skill")
                        .Include("UserIdeas.IdeaThings")
                        .Include("UserInterests.Interest")
                        .Include("UserMessengers.Messenger")
                        .Include("UserFriends.User.UserSkills.Skill")
                        .Include("UserFriends1.User1.UserSkills.Skill")
                        .Include("UserFriends.User.UserIdeas")
                        .Include("UserFriends1.User1.UserIdeas")
                               where u.UserId == userId
                               select u;

它有一个长期的对象图,并使用了很多包括。它运行的是完美的权利,但如果网站有很多的用户,它会影响性能有多大?

It has a long object graph and uses many Includes. It is running perfect right now, but when the site has many users, will it impact performance much?

我应该做它在一些其他的方式?

Should I do it in some other way?

推荐答案

与查询包括返回一个结果集和数量,包括影响数据有多大集从数据库服务器到Web服务器转移。例如:

A query with includes returns a single result set and the number of includes affect how big data set is transfered from the database server to the web server. Example:

假设我们有一个实体客户(ID,姓名,地址)和实体订单(ID,客户编号,日期)。现在,我们要查询的客户与她的命令:

Suppose we have an entity Customer (Id, Name, Address) and an entity Order (Id, CustomerId, Date). Now we want to query a customer with her orders:

var customer = context.Customers
                      .Include("Orders")
                      .SingleOrDefault(c => c.Id == 1);

将所得的数据组将具有以下结构:

The resulting data set will have the following structure:

 Id | Name | Address | OrderId | CustomerId | Date 
---------------------------------------------------
  1 |  A   |   XYZ   |    1    |     1      | 1.1.
  1 |  A   |   XYZ   |    2    |     1      | 2.1.

这意味着通知客户数据重复每个订单。现在,让我们与另一个实体扩展的例子 - 订单行(ID,的OrderId,产品编号,数量)产品(编号,名称)`。现在,我们要查询的客户与她的订单,订单行和产品:

It means that Cutomers data are repeated for each Order. Now lets extend the example with another entities - 'OrderLine (Id, OrderId, ProductId, Quantity)andProduct (Id, Name)`. Now we want to query a customer with her orders, order lines and products:

var customer = context.Customers
                      .Include("Orders.OrderLines.Product")
                      .SingleOrDefault(c => c.Id == 1);

将所得的数据组将具有以下结构:

The resulting data set will have the following structure:

 Id | Name | Address | OrderId | CustomerId | Date | OrderLineId | LOrderId | LProductId | Quantity | ProductId | ProductName
------------------------------------------------------------------------------------------------------------------------------
  1 |  A   |   XYZ   |    1    |     1      | 1.1. |     1       |    1     |     1      |    5     |    1      |     AA
  1 |  A   |   XYZ   |    1    |     1      | 1.1. |     2       |    1     |     2      |    2     |    2      |     BB
  1 |  A   |   XYZ   |    2    |     1      | 2.1. |     3       |    2     |     1      |    4     |    1      |     AA
  1 |  A   |   XYZ   |    2    |     1      | 2.1. |     4       |    2     |     3      |    6     |    3      |     CC

正如你所看到的数据成为了不少重复。 Generaly均包括一个参考导航属性格式(产品中的例子)将增加新的栏目,每个包括一个集合导航属性(订单 OrderLines 中的例子)将包含集合中添加新列和复制已经创建行的每一行。

As you can see data become quite a lot duplicated. Generaly each include to a reference navigation propery (Product in the example) will add new columns and each include to a collection navigation property (Orders and OrderLines in the example) will add new columns and duplicate already created rows for each row in the included collection.

这意味着你的例子可以轻松拥有数百个行这是很多要传输的数据的列和数千人。正确的做法是创建性能测试,如果结果不能满足您的期望,您可以通过自己的查询或 LoadProperty 方法分别修改您的查询和装载的导航属性。

It means that your example can easily have hundreds of columns and thousands of rows which is a lot of data to transfer. The correct approach is creating performance tests and if the result will not satisfy your expectations, you can modify your query and load navigation properties separately by their own queries or by LoadProperty method.

单独的查询的例子:

var customer = context.Customers
                      .Include("Orders")
                      .SingleOrDefault(c => c.Id == 1);
var orderLines = context.OrderLines
                        .Include("Product")
                        .Where(l => l.Order.Customer.Id == 1)
                        .ToList();

示例 LoadProperty

var customer = context.Customers
                      .SingleOrDefault(c => c.Id == 1);
context.LoadProperty(customer, c => c.Orders);

此外,你应该始终只加载你真正需要的数据。

Also you should always load only data you really need.

编辑:我刚刚创建<一个href=\"http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/2770979-offer-eager-loading-strategies\">proposal在数据UserVoice的,以支持更多的预先加载策略,这样急于加载的数据将额外的结果集(在同一个数据库往返内单独的查询创建)进行传递。如果你发现这个有趣的改进,不要忘记投票支持该提案。

I just created proposal on Data UserVoice to support additional eager loading strategy where eager loaded data would be passed in additional result set (created by separate query within the same database roundtrip). If you find this improvement interesting don't forget to vote for the proposal.

这篇关于有多少包括我可以在上的EntityFramework使用对象集保留的表现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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