简体   繁体   English

这两个 LINQ 查询有什么区别以及如何正确优化它们?

[英]What is the difference between these two LINQ Queries and how to correctly optimize them?

I have a Clients table which has the following columns: Id, FirstName, LastName, CurrencyId, Gender.我有一个客户表,其中包含以下列:Id、FirstName、LastName、CurrencyId、Gender。

I want to select the client's Currency that corresponded to Id 10 so I am doing something like this:我想 select 对应于 Id 10 的客户货币,所以我正在做这样的事情:

var currencyId = Db.Clients.FirstOrDefault(c=>c.Id == 10)?.CurrencyId;

Does this line bring all properties from Db and selects the currency in the code or it executes something like this in the database:此行是从 Db 中获取所有属性并在代码中选择货币还是在数据库中执行类似的操作:

SELECT currencyId FROM Client WHERE ID = 10

Or I should write the linq like this:或者我应该这样写 linq :

var currencyId = Db.Clients.Where(c=>c.Id == 10).Select(c=>c.CurrnecyId).FirstOrDefault();

What's the difference between the two queries?这两个查询有什么区别? And what is the correct way to translate the above SQL query into a linq query?将上述 SQL 查询转换为 linq 查询的正确方法是什么?

Looked into it myself cause I found the anwser from most people questionable.我自己调查了一下,因为我发现大多数人的答案都是有问题的。 I expect the FirstOrDefault to materialize the result (you also see from the type that you are not longer working with a query object), so that would mean it queries for all properties.我希望FirstOrDefault实现结果(您还可以从不再使用查询对象的类型中看到),这意味着它会查询所有属性。

Unlike the 2nd query where you are still working with a query when filtering the property you like, thus dependent on the implementation it could be used for filtering properties and selecting specific fields.与第二个查询不同,您在过滤您喜欢的属性时仍在使用查询,因此取决于实现,它可用于过滤属性和选择特定字段。

The following is an example of the queries generated using EF for two similar queries, where it shows both generating different queries: https://dotnetfiddle.net/5aFJAZ以下是使用 EF 为两个类似查询生成的查询示例,其中显示两者都生成不同的查询: https://dotnetfiddle.net/5aFJAZ

In your first example, var currencyId = Db.Clients.FirstOrDefault(c=>c.Id == 10)?.CurrencyId;在您的第一个示例中, var currencyId = Db.Clients.FirstOrDefault(c=>c.Id == 10)?.CurrencyId; the query selects the entire object to memory and then returns the Id property from that in memory object.查询选择整个 object 到 memory 然后从 memory ZA8CFDE6331149EB2ACZ96F8 中返回 Id 属性。 As a result it needs to do something like the following SQL: SELECT * FROM Clients WHERE Id = 10 .因此,它需要执行类似以下的操作 SQL: SELECT * FROM Clients WHERE Id = 10 I understand I'm not using a parameter here and EF does spell out every column.我知道我在这里没有使用参数,并且 EF 确实拼出了每一列。 However the key to understand here is that by returning more columns than you need, you potentially are setting up a performance concern because a covering index on Id and CurrencyId would not be used.然而,这里要理解的关键是,通过返回比您需要的更多的列,您可能会设置性能问题,因为不会使用 Id 和 CurrencyId 上的覆盖索引。

Your second LINQ query would use a SQL statement like SELECT CurrencyId FROM Clients Where Id = 10 which would take advantage of your indexes assuming you have an index covering these columns.您的第二个 LINQ 查询将使用 SQL 语句,例如SELECT CurrencyId FROM Clients Where Id = 10假设您拥有涵盖这些列的索引,它将利用您的索引。

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

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