简体   繁体   English

实体框架多对多关系包括极其缓慢

[英]Entity Framework many-to-many relationship include extremely slow

I have an Entity Framework 4 model, with 2 entities containing many-to-many relationship, so 3 tables, [Q], [P] and [Q2P]-cross table. 我有一个实体框架4模型,其中2个实体包含多对多关系,因此有3个表,[Q],[P]和[Q2P] - 交叉表。 Running code like: 运行代码如:

context.Q.Include("P");

Results in long time wait (I waited like 5 mins then aborted it). 长时间等待的结果(我等了5分钟然后中止了)。 Then I checked SQL generated and found this: 然后我检查了SQL生成并发现了这个:

SELECT *
FROM ( SELECT *
    FROM  [Q] AS [Extent1]
    LEFT OUTER JOIN  (SELECT *, CASE WHEN ([Join1].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2]
        FROM  [Q_P] AS [Extent2]
        INNER JOIN [P] AS [Extent3] ON [Extent3].[Id] = [Extent2].[Id] ) AS [Join1] ON [Extent1].[Id] = [Join1].[Id]
)  AS [Project1]
ORDER BY [Project1].[Id] ASC, [Project1].[C2] ASC

I can't hide my suprise, WTF is this? 我无法掩饰我的惊讶,WTF是这个吗? The usual many-to-many SQL query 通常的多对多SQL查询

select * from [q2p]
join [q] on qId=q.Id
join [p] on pId=p.Id

executes in less than 1ms, while EF query executes forever. 执行时间不到1毫秒,而EF查询永远执行。

Yep, that's not a secret that it takes long, please vote on the connection I opened about a year ago. 是的,这不是一个需要很长时间的秘密,请对我一年前开通的连接进行投票。

However, 5 minutes is something it's definitely not supposed to take. 然而,5分钟是绝对不应该采取的。

Try separating the execution from the query generation, and use ToTraceString to see how long does it take to determine query generation time. 尝试将执行与查询生成分开,并使用ToTraceString查看确定查询生成时间需要多长时间。

Eager-loading is not the great deal in current version, they said they're planning toreduce the performance cost in future. 渴望加载在当前版本中并不是很重要,他们表示他们计划在未来降低性能成本。

Anyway, what you could do is use Stored Procedures or create your own ObjectQueries. 无论如何,你可以做的是使用存储过程或创建自己的ObjectQueries。

See: 看到:
- http://msdn.microsoft.com/en-us/library/bb896241.aspx - http://msdn.microsoft.com/en-us/library/bb896241.aspx
- http://msdn.microsoft.com/en-us/library/bb896238.aspx - http://msdn.microsoft.com/en-us/library/bb896238.aspx

我已切换到Nhibernate。

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

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