LINQ-在哪里使用或在哪里加入-性能差异? [英] LINQ - Using where or join - Performance difference?

查看:73
本文介绍了LINQ-在哪里使用或在哪里加入-性能差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此问题: Linq和Join in linq有什么区别?

我的问题如下:

以下两个语句之间是否存在性能差异:

Is there a performance difference in the following two statements:

from order in myDB.OrdersSet
    from person in myDB.PersonSet
    from product in myDB.ProductSet
    where order.Persons_Id==person.Id && order.Products_Id==product.Id
    select new { order.Id, person.Name, person.SurName,  product.Model,UrunAdı=product.Name };

from order in myDB.OrdersSet
    join person in myDB.PersonSet on order.Persons_Id equals person.Id
    join product in myDB.ProductSet on order.Products_Id equals product.Id
    select new { order.Id, person.Name, person.SurName,  product.Model,UrunAdı=product.Name };

我总是使用第二个,只是因为它更清晰.

I would always use the second one just because it´s more clear.

我的问题是,第一个慢于第二个吗? 它会构建笛卡尔乘积并随后使用where子句对其进行过滤吗?

My question is now, is the first one slower than the second one? Does it build a cartesic product and filters it afterwards with the where clauses ?

谢谢.

推荐答案

这完全取决于您使用的提供程序.

It entirely depends on the provider you're using.

使用LINQ to Objects,它将绝对构建笛卡尔乘积并随后进行过滤.

With LINQ to Objects, it will absolutely build the Cartesian product and filter afterwards.

对于诸如LINQ to SQL之类的进程外查询提供程序,这取决于它是否足够聪明以意识到可以将其转换为SQL连接.即使不是LINQ to SQL,实际执行查询的查询引擎也可能会这样做-您必须使用数据库的相关查询计划工具进行检查,以了解实际情况.

For out-of-process query providers such as LINQ to SQL, it depends on whether it's smart enough to realise that it can translate it into a SQL join. Even if LINQ to SQL doesn't, it's likely that the query engine actually performing the query will do so - you'd have to check with the relevant query plan tool for your database to see what's actually going to happen.

旁注:多个"from"子句并不总会产生笛卡尔积-一个"from"的内容可能取决于较早版本的当前元素,例如

Side-note: multiple "from" clauses don't always result in a Cartesian product - the contents of one "from" can depend on the current element of earlier ones, e.g.

from file in files
from line in ReadLines(file)
...

这篇关于LINQ-在哪里使用或在哪里加入-性能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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