替代LINQ to SQL中的Any()以获得更好的性能 [英] Alternative to Any() in LINQ to SQL for better performance

查看:68
本文介绍了替代LINQ to SQL中的Any()以获得更好的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用嵌套.Where()的动态构建的大型查询.我正在使用Any(),但是在性能不佳和一些SQL性能分析之后,我发现Any()实际上会导致许多往返,并选择相关表的每个记录进行评估,而不是使用JOIN,例如,这样会更好.

I have a large query built dynamically with the usage of nested .Where(). I am using Any() but after poor performance and some SQL profiling, I found Any() actually causes many round trips and selects each record of a related table for evaluation instead of using a JOIN, for example, which would be much better.

这里是表的表示形式,并假设表A,B和C:

Here's a representation of the tables and what's being done, assuming tables A, B and C:

A<-B-> C

A <-- B --> C

假设我正在查询A并且有类似.Where(a => a.B.C.Any(c => c.IsActive))

Suppose I'm querying A and have something like .Where(a => a.B.C.Any(c => c.IsActive))

尽管使用Any(),还有更好的选择吗?

Is there a better option in spite of using Any()?

推荐答案

显然,您并不是真正在使用LINQ将查询远程传输到服务器.看来您正在使用IEnumerable查询,而不是IQueryable查询.

Apparently, you are not really using LINQ to remote your queries to the server. It appears you are using IEnumerable queries, not IQueryable queries.

通常,将显示的查询翻译为SQL批发.找出原因,为什么对对象使用LINQ.该错误与您有关,而不与L2S有关. (这些错误很容易发生,因为您不能仅仅通过查看代码就可以发现它们.)

Normally, the query as shown qould be translated to SQL wholesale. Find out, why you are using LINQ to objects. The bug is with you, not with L2S. (These bugs can easily happen because you cannot trivially spot them just by looking at the code).

尽管使用Any(),还有更好的选择吗?

Is there a better option in spite of using Any()?

不确定为什么您认为Any是问题.这是一个随机的旁观者.

Not sure why you think Any is the problem. It is a random bystander.

这篇关于替代LINQ to SQL中的Any()以获得更好的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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