简体   繁体   English

实体框架性能连接与导航属性

[英]Entity framework performance join vs navigation property

I am trying to understand why is join in my case faster than statement which use navigation property. 我试图理解为什么加入我的情况比使用导航属性的语句更快。 I have two queries. 我有两个问题。

First with navigation property : 首先是导航属性:

          var result = (from users in context.MetricBloodPreasure
                orderby users.User.LastName, users.User.FirstName
                select new
                {
                    UserName = users.User.LastName + ", " + users.User.FirstName,
                    Date = users.DateOfValue,
                }).ToList();

Generatet sql : Generatet sql:

SELECT 
    [Project1].[C1] AS [C1], 
    [Project1].[C2] AS [C2], 
    [Project1].[DateOfValue] AS [DateOfValue]
    FROM ( SELECT 
        [Extent1].[DateOfValue] AS [DateOfValue], 
        [Extent2].[FirstName] AS [FirstName], 
        [Extent2].[LastName] AS [LastName], 
        1 AS [C1], 
        CASE WHEN ([Extent2].[LastName] IS NULL) THEN N'' ELSE [Extent2].[LastName] END + N', ' + CASE WHEN ([Extent2].[FirstName] IS NULL) THEN N'' ELSE [Extent2].[FirstName] END AS [C2]
        FROM  [dbo].[MetricBloodPreasure] AS [Extent1]
        INNER JOIN [dbo].[User] AS [Extent2] ON [Extent1].[UserId] = [Extent2].[Id]
    )  AS [Project1]
    ORDER BY [Project1].[LastName] ASC, [Project1].[FirstName] ASC

Second with join: 第二次加入:

var result1 = (from u in context.User
                orderby u.LastName, u.FirstName
                join us in context.MetricBloodPreasure
                    on u.Id equals us.UserId into users
                from s in users
                select new
                {
                    UserName = s.User.LastName + ", " + s.User.FirstName,
                    Date = s.DateOfValue,
                }).ToList();

Generated sql: 生成的sql:

SELECT 
    1 AS [C1], 
    CASE WHEN ([Extent1].[LastName] IS NULL) THEN N'' ELSE [Extent1].[LastName] END + N', ' + CASE WHEN ([Extent1].[FirstName] IS NULL) THEN N'' ELSE [Extent1].[FirstName] END AS [C2], 
    [Extent2].[DateOfValue] AS [DateOfValue]
    FROM  [dbo].[User] AS [Extent1]
    INNER JOIN [dbo].[MetricBloodPreasure] AS [Extent2] ON ([Extent1].[Id] = [Extent2].[UserId]) AND ([Extent2].[UserId] = [Extent1].[Id])

Before running first query, call var user = context.User.FirstOrDefault(); 在运行第一个查询之前,请调用var user = context.User.FirstOrDefault(); because I think open connection to database take some time. 因为我认为与数据库的开放连接需要一些时间。

Results : Navigation property query : 00:00:00.6719646 Join query : 00:00:00.4941169 结果:导航属性查询:00:00:00.6719646加入查询:00:00:00.4941169

Looking at results it seems that Linq queries that use joins instead of navigation properties are faster. 看看结果似乎Linq查询使用连接而不是导航属性更快。 Is that true or I'm doing something wrong ? 是真的还是我做错了什么?

To get a better insight into what it is doing you should get the raw SQL and you can check out the execution plan yourself. 为了更好地了解它正在做什么,您应该获得原始SQL,并且您可以自己检查执行计划。

To do this, you can either use SQL Profiler to see what query is being run, or you can log the SQL query itself by doing something like this before you run your query: 为此,您可以使用SQL事件探查器查看正在运行的查询,也可以在运行查询之前通过执行以下操作来记录SQL查询:

context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);

Also doing a simple benchmark like you did by running each once isn't going to necessarily be reliable. 同样做一个简单的基准测试,就像你每次运行一样,并不一定是可靠的。 You'll want to run it multiple times and average things out. 你需要多次运行它并将其平均化。 You should also run them in the opposite order to see if that changes things as well. 您还应该以相反的顺序运行它们,看看它是否也会改变。

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

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