为什么使用连接语句实体框架如此缓慢? [英] Why is Entity Framework so slow with join statements?

查看:482
本文介绍了为什么使用连接语句实体框架如此缓慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EF 6查询,其中有大约6个linq连接语句。当我使用调试器浏览我的代码时,我可以看到查询语句花费大约6秒钟才能运行。使用SQL跟踪,我可以告诉我,实际的查询,看起来像我会期望的,需要0 ms并返回0行。我逐个删除了join语句。删除每个连接语句后,EF语句的执行时间减少了1秒。查询的选择部分从未更改,只有联接数。



我的问题是,发生了什么?什么是EF,需要很多时间来处理这个查询?奇怪的是,大部分时间实际上是在SQL执行已经完成之后花费的,​​所以时间不是生成查询,而是任何EF之后。

解决方案

您是否单独使用延迟加载和初始化每个连接?如果您在6个大型数据集上调用6个连接,这些数据集正在返回正确大小的对象,那么它将会很慢。这可能需要很长时间,因为您返回的数据集可能非常大,您正在做这6次。我有这个问题,巨大的数据集和表在网页上过滤ajax。我可以通过使用懒惰加载和一起构建我的所有功能,然后只初始化一次来提高性能。
我猜最终在6个连接之后,你想要一个数据集,我包括所有这些连接。您希望将这些连接建立在一个单一的查询中,并且只能对您的数据库进行一次触发,因此EF只需将对象构建到代码中即可。


I have an EF 6 query that has about 6 linq join statements in it. When I step through my code with the debugger, I can see that the query statement takes roughly 6 seconds to run. With a SQL trace, I can tell that the actual query, which looks like I would expect it to, takes 0 ms and returns 0 rows. I removed the join statements 1 by one. With each join statement removed, the EF statement took 1 second less to execute. The select portion of the query never changed, only the number of joins.

My question is, what is happening? What is EF doing that it requires so much time to process this query? Oddly, it looks like most of this time is actually spent after the SQL execution is already finished, so the time is not generating the query, but whatever EF is doing afterward.

解决方案

Are you utilizing lazy-loading and initializing each join individually? If you are calling 6 joins on 6 large data sets that are returning decently sized objects, it will be slow. It is probably taking a long time because the data-set you are returning might be very large and you are doing that 6 times. I've had this problem with huge data-sets and tables on web pages that filter with ajax. I was able to improve performance a lot by utilizing lazy-loading and building all my quires together and then only initializing it once. I'm guessing eventually after the 6 joins you want one dataset that i comprised of all those joins. You want to build those joins into one single query and have it only fire against your DB once so EF only has to build the objects into your code once.

这篇关于为什么使用连接语句实体框架如此缓慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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