实体框架使用include和order by生成的查询效率低下
本文关键字:查询 效率 by 框架 include order 实体 | 更新日期: 2023-09-27 18:18:11
1)我遇到的第一个问题是,如果你做一个包含,然后由SQL生成的订单生成一个内部连接和一个外部连接
var query = from l in Lead.Include("Contact")
orderby l.Contact.FirstName
select l;
在同一表
上生成以下内连接和外连接 INNER JOIN [dbo].[Contact] AS [Extent2]
ON [Extent1].[ContactId] = [Extent2].[ContactId]
LEFT OUTER JOIN [dbo].[Contact] AS [Extent3]
ON [Extent1].[ContactId] = [Extent3]. [ContactId]
ORDER BY [Extent2].[FirstName] ASC
这使得查询
效率略低。2)如果我执行多个include它总是将第二个作为外连接执行,例如
Lead.Include("OneToOne").Include("OtherOneToOne") <- in this scenario
OtherOneToOne is an outer
join and OneToOne is an inner
join
Lead.Include("OtherOneToOne").Include("OneToOne") <- in this scenario
OneToOne is an outer join
and OtherOneToOne is an
inner join
就是这样工作的吗?
我发现了另一个帖子,有人在那里谈论这个问题,他们说在6月的CTP版本中已经修复了http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing - -微软实体框架- 6 - 2011 - ctp.aspx
但是我安装并设置了要使用的,它仍然不工作…
它不让我回答我自己的问题
编辑:好了,我设置了一个独立的测试,发现http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx似乎已经解决了这些
但是由于我使用的是RIA,所以我运气不好,因为6月份的ctp不支持RIA:-/
一个解决方案是做include yourself:
var query = from l in Lead
select new { l, l.Contact } into row
orderby row.Contact.FirstName
select row;