什么是多where子句和与放大器之间的差异;放大器&;运营商LINQ到SQL? [英] What's the difference between multiple where clauses and && operator in LINQ-to-SQL?

查看:170
本文介绍了什么是多where子句和与放大器之间的差异;放大器&;运营商LINQ到SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来像我可以写一个,其中x.a == 1安培;&安培; XB == 1

It looks like I can write a where x.a==1 && x.b==1 as

where x.a==1
where x.b==1

据我了解后转成。凡(X => XA == 1)。凡(X =>预算外== 1),但这是如何转化为DB?这将在优化方面会更好?我总能看到从分析器执行的查询,但将难以有泛化而更像是一个经验观察,我不想依靠。

As I understand the latter turns into .Where(x => x.a == 1).Where(x => x.b ==1), but how does this translate to DB? Which would be better in terms of optimization? I can always look at the executed query from profiler but that would hardly be a generalization but more like a single empirical observation, which I don't want to rely on.

通过System.Linq的命名要与反射器是另一种选择,但接下来我们会错过许多人从保存在相同的事情花费时间的机会。我会做,如果我没有得到任何答案。

Going through System.Linq namespace with reflector is another option but then we would miss the chance to save many people from spending time on the same thing. I'll do it if I don't get any answers.

推荐答案

确定这里是我的发现,通过反射输出去为后一会儿。 LINQ到对象使用时,结合连续其中,谓词 WhereArrayIterator WhereListIterator 这导致几乎表现得像&放大器;&安培;运营商,但不是完全相同:

Ok here are my findings after going through Reflector output for a while. LINQ-to-Objects combine the consecutive where predicates when using WhereArrayIterator or WhereListIterator causing it to ALMOST behave like && operator, but not as exactly:

当您使用 x.a == 1安培;&安培; XB == 1 where子句转化为 Func键< TSource,布尔> 看起来像这样:

When you use x.a==1 && x.b==1 the where clause translates into a Func<TSource, bool> looking like this:

bool daspredicate(TSource x)
{
    return x.a==1 && x.b==1
}



然而,当你连续使用Where子句有轻微性能损失,在非即时编译IL-方面最少。下面是代码看起来像合并后的:

However, when you use consecutive Where clauses there is a slight performance penalty, at least from non-JITted IL-aspect. Here is how code looks like after combining:

bool predicate1(TSource x)
{
     return x.a==1;
}
bool predicate2(TSource x)
{
     return x.b==1;
}
bool daspredicate(TSource x)
{
    return predicate1(x) && predicate2(x);
}



正如你可以看到这涉及额外的函数调用的开销。除非JIT内联函数这可能是相当昂贵的。我敢肯定,它做了很好的工作,但现在我们知道,如果我们结合我们在哪里陈述自己,除非必要JIT的工作变得更加容易。

As you can see this involves additional function call overhead. This can be quite expensive unless JIT inlines the functions. I'm sure it does a good job at it but we now know JIT's job becomes much easier if we combine our Where statements ourselves, unless necessary.

在SQL-的的东西侧虽然,查询是相同的。即使在执行之前,调试器评估查询对象在同一个SQL语句。我不能去太远的LINQ的命名空间,因为事情似乎要复杂得多,但由于查询相同,应该有不同于上述LINQ到对象实例中没有点球。

On the SQL-side of things though, the queries are the same. Even before execution, debugger evaluates the query object into the same SQL statement. I couldn't go too far in Linq namespace because things seemed much more complex, but since queries are the same, there should be no penalty unlike LINQ-to-objects example above.

编辑:我见过的情况,其中多个,其中产生的嵌套子查询的SQL服务器上的语句。我认为这是更好地坚持单身,语句时,你可以是在安全方面。

I've seen instances where multiple where statements resulting in nested sub-queries on SQL server. I think it's better to stick to single where statements whenever you can to be on the safe side.

这篇关于什么是多where子句和与放大器之间的差异;放大器&;运营商LINQ到SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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