简体   繁体   English

慢连接查询 - 实体框架

[英]Slow join query - Entity Framework

I'm trying to perform a simple query in Entity Framework. 我正在尝试在Entity Framework中执行一个简单的查询。

I have a list of funky things 我有一系列时髦的东西

List<FunkyThing> funkyThings;

Which has 1-5 unique FunkyThing 's in it. 其中有1-5个独特的FunkyThing

FunkyThing
{
   string FunkyThingUniqueCustomerCode{get;set}
   string UsefullInfoRegardFunkyThings{get;set}
}

I'm trying to do a join onto the funky things table in my database. 我正在尝试加入我数据库中的funky things表。

The table looks something along the lines of: 该表看起来像是:

FunkyThingsTable FunkyThingsTable

int ID
string UniqueCustomerCode 
string colour
string usefulInfoOfGreatValue
decimal cost

Now as it so happens there's approximately 300,000 funky things in this table. 现在正好如此,这张表中有大约300,000个时髦的东西。

What I was hoping to do is join my list on to my table to get the usefulInfoOfGreatValue element out. 我希望做的是将我的列表加入到我的表中以获取effectiveInfoOfGreatValue元素。 As follows: 如下:

var listOfFunkyThingsUsefulInfoQuery = from funkyThing in funkyThings
                                   join
                                      funkyThingDBEntity in unitOfWork.FunkyThingsRepository.Get()
                                      on funkyThing.FunkyThingUniqueCustomerCode equals funkyThingDBEntity .UniqueCustomerCode

                                  select new
                                  {
                                      uniqueCode= funkyThingDBEntity .UniqueCustomerCode,
                                      usefulInfoOfGreatValue= funkyThingDBEntity .usefulInfoOfGreatValue
                                  };

Unfortunately the query takes about 5 seconds to run -even with just one item in the list. 不幸的是,查询大约需要5秒才能运行 - 即使列表中只有一个项目。 What am I doing wrong? 我究竟做错了什么?

A few quick notes. 一些快速说明。 I'm using the unit of work pattern as described here: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application 我正在使用这里描述的工作单元模式: http//www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of -work图案功能于一个-ASP净MVC-应用

The customers for long technical reasons don't have the integer ID, only a string customer code - hence this comparison. 由于长期技术原因,客户没有整数ID,只有字符串客户代码 - 因此这种比较。

Additional Notes: As per the unit of work article the unitOfWork.FunkyThingsRepository.Get() returns an IEnumerable: 附加说明:根据工作单元文章,unitOfWork.FunkyThingsRepository.Get()返回一个IEnumerable:

 public virtual IEnumerable<TEntity> Get

I've just tried re-working so use lambda as follows: 我刚尝试重新工作,所以使用lambda如下:

var listOfFunkyThingsUsefulInfoQuery = unitOfWork.FunkyThingsRepository.Get().Join (funkyThings, funkyThingDBEntity=>funkyThingDBEntity.UniqueCustomerCode, funkyThings=>funkyThings.FunkyThingUniqueCustomerCode ,(funkyThings,funkyThingDBEntity)=>new {uniqueCode= funkyThingDBEntity .UniqueCustomerCode, usefulInfoOfGreatValue=funkyThingDBEntity .usefulInfoOfGreatValue}) ; 

However sadly this takes the same amount of time 但遗憾的是,这需要相同的时间

Your query will cause all 300,000 rows be retrieved from the database to memory and perform the join there. 您的查询将导致从数据库中检索所有300,000行到内存并在那里执行连接。 Looking at your query, all you're trying to do is extract UsefulInfoOfGreatValue associated with the items in funkyThings based on UniqueCustomerCode . 看你的查询,你正在试图做的一切都是提取UsefulInfoOfGreatValue与该项目相关联的funkyThings基于UniqueCustomerCode

In the article, the Get() method has a filter parameter that defaults to null. 在本文中, Get()方法有一个默认为null的filter参数。 You can set this filter to make your query much faster (ie only get the rows you need) 您可以设置此过滤器以使查询更快(即只获取您需要的行)

Try this 尝试这个

var funkyThingsCustomerCodes = funkyThings.Select(x => x.FunkyThingUniqueCustomerCode).ToList();

var listOfFunkyThingsUsefulInfoQuery =
      from funkyThing in unitOfWork.FunkyThingsRepository.Get(e => funkyThingsCustomerCodes.Contains(e.UniqueCustomerCode))
      select new {
        UniqueCode = funkyThing.UniqueCustomerCode,
        UsefulInfoOfGreatValue = funkyThing.UsefulInfoOfGreatValue
      };

Also if you create an index on UniqueCustomerCode column in your table the above query will be even much faster. 此外,如果您在表中的UniqueCustomerCode列上创建索引,则上述查询将更快。

try to use eager loading, and use include statement. 尝试使用预先加载,并使用include语句。 It will be little faster. 它会快一点。 Please go through below links, it might help you. 请通过以下链接,它可能会帮助您。

http://msdn.microsoft.com/en-us/data/jj574232.aspx http://blogs.msdn.com/b/adonet/archive/2008/10/07/migrating-from-linq-to-sql-to-entity-framework-eager-loading.aspx http://msdn.microsoft.com/en-us/data/jj574232.aspx http://blogs.msdn.com/b/adonet/archive/2008/10/07/migrating-from-linq-to-sql -to-实体框架急切,loading.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM