为什么简单的LINQ查询比等效的SQL查询慢得多?

时间:2016-08-22 02:04:35

标签: sql linq

我必须优化一些使用LINQ的后端API调用。所以我安装了LINQPad并运行了一些简单的测试查询。这个SQL语句:

select * from aspnetusers

在LINQPad中运行需要0.45秒。这个LINQ:

from a in AspNetUsers select a

需要6.4秒才能运行。

你期望LINQ花更长的时间吗?如果没有,我该如何确定问题?

修改: 以下是AspNetUser实体的样子:

namespace myns.Data
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class AspNetUser
    {
        public AspNetUser()
        {
            //instantiate 5 hashsets here
            //that have to do with other tables
        }

        public string Id { get; set; }
        public string UserName { get; set; }
        //about 40 more similar members here
    }
}

以下是生成的SQL的样子:

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[UserName] AS [UserName], 
    ...50 similar lines here... 
    [Extent1].[TabletNumber] AS [TabletNumber], 
    [Extent1].[TabletId1] AS [TabletId1], 
    [Extent1].[TabletId2] AS [TabletId2], 
    [Extent1].[TabletId3] AS [TabletId3], 
    [Extent1].[TabletId4] AS [TabletId4], 
    [Extent1].[TabletId5] AS [TabletId5]
    FROM [dbo].[AspNetUsers] AS [Extent1]

此外,主键是具有以下格式的guid字符串:

019b9883-5c7d-441b-ad32-674f0bbf1bdd

0 个答案:

没有答案