优化实体框架中生成的sql查询?

Posted

技术标签:

【中文标题】优化实体框架中生成的sql查询?【英文标题】:Optimize generated sql query in entity framework? 【发布时间】:2013-11-11 10:18:39 【问题描述】:

我在 MVC4 应用程序中使用 SQL Server 2008 和 EntityFramework 6。

我正在使用以下代码从数据库中获取内容。

Content content = contentBusinessObject.SelectBy(contentPredicate.Expand(),
    new List<string>
    
        "Tags",
        "User",
        "Attachments",
        "Products",
        "ContentEditors",
        "ContentEditors.User"
    );

[DataObjectMethod(DataObjectMethodType.Select)]
public TEntity SelectBy(Expression<Func<TEntity, bool>> predicate, List<string> includeNavigationProperties = null)

    if (includeNavigationProperties == null || includeNavigationProperties.Count <= 0)
        return DBSet.FirstOrDefault(predicate);
    DbQuery<TEntity> query = includeNavigationProperties.Aggregate<string, DbQuery<TEntity>>(DBSet, (current, property) => current.Include(property));
    return query.FirstOrDefault(predicate);

以上代码生成以下t-sql

exec sp_executesql N'SELECT 
    [UnionAll3].[C2] AS [C1], 
    [UnionAll3].[C3] AS [C2], 
    [UnionAll3].[C4] AS [C3], 
    ...
    [UnionAll3].[C107] AS [C107], 
    [UnionAll3].[C108] AS [C108], 
    [UnionAll3].[C109] AS [C109], 
    [UnionAll3].[C110] AS [C110], 
    [UnionAll3].[C111] AS [C111], 
    [UnionAll3].[C112] AS [C112], 
    [UnionAll3].[C113] AS [C113], 
    [UnionAll3].[C114] AS [C114], 
    [UnionAll3].[C115] AS [C115]
    FROM  (SELECT 
        [UnionAll2].[C1] AS [C1], 
        [UnionAll2].[C2] AS [C2], 
        [UnionAll2].[C3] AS [C3], 
        [UnionAll2].[C4] AS [C4], 
        [UnionAll2].[C5] AS [C5], 
        ...
        [UnionAll2].[C109] AS [C109], 
        [UnionAll2].[C110] AS [C110], 
        [UnionAll2].[C111] AS [C111], 
        [UnionAll2].[C112] AS [C112], 
        [UnionAll2].[C113] AS [C113], 
        [UnionAll2].[C114] AS [C114], 
        [UnionAll2].[C115] AS [C115]
        FROM  (SELECT 
            [UnionAll1].[C1] AS [C1], 
            [UnionAll1].[Id] AS [C2], 
            [UnionAll1].[Id1] AS [C3], 
            ...
            [UnionAll1].[C49] AS [C102], 
            [UnionAll1].[C50] AS [C103], 
            [UnionAll1].[C51] AS [C104], 
            [UnionAll1].[C52] AS [C105], 
            [UnionAll1].[C53] AS [C106], 
            [UnionAll1].[C54] AS [C107], 
            [UnionAll1].[C55] AS [C108], 
            [UnionAll1].[C56] AS [C109], 
            [UnionAll1].[C57] AS [C110], 
            [UnionAll1].[C58] AS [C111], 
            [UnionAll1].[C59] AS [C112], 
            [UnionAll1].[C60] AS [C113], 
            [UnionAll1].[C61] AS [C114], 
            [UnionAll1].[C62] AS [C115]
            FROM  (SELECT 
                CASE WHEN ([Join2].[ContentId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1], 
                [Limit1].[Id] AS [Id], 
                [Limit1].[Id] AS [Id1], 
                [Limit1].[Title] AS [Title], 
                [Limit1].[Summary] AS [Summary], 
                [Limit1].[Context] AS [Context], 
                [Limit1].[ImagePath] AS [ImagePath], 
                [Limit1].[UserId] AS [UserId], 
                [Limit1].[VisitNumber] AS [VisitNumber], 
                  ...........
                [Join2].[Name] AS [Name], 
                [Join2].[Description] AS [Description], 
                [Join2].[ImagePath] AS [ImagePath1], 
                CAST(NULL AS int) AS [C2], 
                CAST(NULL AS varchar(1)) AS [C3], 
                CAST(NULL AS varchar(1)) AS [C4], 
                CAST(NULL AS varchar(1)) AS [C5], 
                CAST(NULL AS varchar(1)) AS [C6], 
                CAST(NULL AS varchar(1)) AS [C7], 
                CAST(NULL AS bigint) AS [C8], 
                CAST(NULL AS int) AS [C9], 
                CAST(NULL AS int) AS [C10], 
                CAST(NULL AS int) AS [C11], 
                CAST(NULL AS int) AS [C12], 
                CAST(NULL AS bit) AS [C13], 
                CAST(NULL AS datetime2) AS [C14], 
                CAST(NULL AS varchar(1)) AS [C15], 
                CAST(NULL AS int) AS [C16], 
                CAST(NULL AS varchar(1)) AS [C17], 
                CAST(NULL AS varchar(1)) AS [C18], 
                CAST(NULL AS varchar(1)) AS [C19], 
                CAST(NULL AS int) AS [C20], 
                CAST(NULL AS int) AS [C21], 
                CAST(NULL AS smallint) AS [C22], 
                CAST(NULL AS int) AS [C23], 
                CAST(NULL AS datetime2) AS [C24], 
                CAST(NULL AS bit) AS [C25], 
                CAST(NULL AS varchar(1)) AS [C26], 
                CAST(NULL AS int) AS [C27], 
                CAST(NULL AS int) AS [C28], 
                CAST(NULL AS int) AS [C29], 
                CAST(NULL AS int) AS [C30], 
                CAST(NULL AS bit) AS [C31], 
                CAST(NULL AS datetime2) AS [C32], 
                CAST(NULL AS varchar(1)) AS [C33], 
                CAST(NULL AS int) AS [C34], 
                CAST(NULL AS varchar(1)) AS [C35], 
                CAST(NULL AS varchar(1)) AS [C36], 
                CAST(NULL AS varchar(1)) AS [C37], 
                CAST(NULL AS varchar(1)) AS [C38], 
                CAST(NULL AS varchar(1)) AS [C39], 
                CAST(NULL AS int) AS [C40], 
                CAST(NULL AS bit) AS [C41], 
                CAST(NULL AS int) AS [C42], 
                CAST(NULL AS varchar(1)) AS [C43], 
                CAST(NULL AS varchar(1)) AS [C44], 
                CAST(NULL AS varchar(1)) AS [C45], 
                CAST(NULL AS varchar(1)) AS [C46], 
                CAST(NULL AS varchar(1)) AS [C47], 
                CAST(NULL AS datetime2) AS [C48], 
                CAST(NULL AS varchar(1)) AS [C49], 
                CAST(NULL AS datetime2) AS [C50], 
                CAST(NULL AS int) AS [C51], 
                CAST(NULL AS int) AS [C52], 
                CAST(NULL AS datetime2) AS [C53], 
                CAST(NULL AS int) AS [C54], 
                CAST(NULL AS varchar(1)) AS [C55], 
                CAST(NULL AS varchar(1)) AS [C56], 
                CAST(NULL AS varchar(1)) AS [C57], 
                CAST(NULL AS int) AS [C58], 
                CAST(NULL AS varchar(1)) AS [C59], 
                CAST(NULL AS varchar(1)) AS [C60], 
                CAST(NULL AS varchar(1)) AS [C61], 
                CAST(NULL AS varchar(1)) AS [C62]
                FROM   (SELECT TOP (1) 
                    [Extent1].[Id] AS [Id], 
                    [Extent1].[Title] AS [Title], 
                    [Extent1].[Summary] AS [Summary], 
                    [Extent1].[Context] AS [Context], 
                    [Extent1].[Keywords] AS [Keywords], 
                    [Extent1].[ImagePath] AS [ImagePath], 
                    [Extent1].[UserId] AS [UserId], 
                    [Extent1].[VisitNumber] AS [VisitNumber], 
                    [Extent1].[LanguageId] AS [LanguageId], 
                    [Extent1].[ReferenceName] AS [ReferenceName], 
                    .....
                    [Extent2].[TwitterPage] AS [TwitterPage], 
                    [Extent2].[FaceBookPage] AS [FaceBookPage], 
                    [Extent2].[PersianblogPage] AS [PersianblogPage], 
                    [Extent2].[ProfileVisitedNumber] AS [ProfileVisitedNumber], 
                    [Extent2].[MinSalary] AS [MinSalary], 
                    [Extent2].[Telephone] AS [Telephone], 
                    [Extent2].[Mobile] AS [Mobile], 
                    [Extent2].[ZipCode] AS [ZipCode], 
                    [Extent2].[ProvinceId] AS [ProvinceId], 
                    [Extent2].[City] AS [City], 
                    [Extent2].[Address] AS [Address], 
                    [Extent2].[LastRecoveryPasswordDate] AS [LastRecoveryPasswordDate], 
                    [Extent2].[LastIP] AS [LastIP], 
                    [Extent2].[RegistrationDate] AS [RegistrationDate1], 
                    [Extent2].[PersianRegistrationDate] AS [PersianRegistrationDate1]
                    FROM  [dbo].[Content] AS [Extent1]
                    INNER JOIN [dbo].[User] AS [Extent2] ON [Extent1].[UserId] = [Extent2].[Id]
                    WHERE ([Extent1].[Id] = @p__linq__0) AND (@p__linq__0 IS NOT NULL) AND ((([Extent1].[LanguageId] = @p__linq__1) AND ( NOT ([Extent1].[LanguageId] IS NULL OR @p__linq__1 IS NULL))) OR (([Extent1].[LanguageId] IS NULL) AND (@p__linq__1 IS NULL))) AND ((([Extent1].[StatusId] = @p__linq__2) AND ( NOT ([Extent1].[StatusId] IS NULL OR @p__linq__2 IS NULL))) OR (([Extent1].[StatusId] IS NULL) AND (@p__linq__2 IS NULL))) ) AS [Limit1]
                LEFT OUTER JOIN  (SELECT [Extent3].[ContentId] AS [ContentId], [Extent4].[Id] AS [Id], [Extent4].[Name] AS [Name], [Extent4].[Description] AS [Description], [Extent4].[ImagePath] AS [ImagePath]
                    FROM  [dbo].[TagContent] AS [Extent3]
                    INNER JOIN [dbo].[Tag] AS [Extent4] ON [Extent4].[Id] = [Extent3].[TagId] ) AS [Join2] ON [Limit1].[Id] = [Join2].[ContentId]
            UNION ALL
                SELECT 
                2 AS [C1], 
                [Limit2].[Id] AS [Id], 
                [Limit2].[Id] AS [Id1], 
                [Limit2].[Title] AS [Title], 
                [Limit2].[Summary] AS [Summary], 
                [Limit2].[Context] AS [Context], 
                [Limit2].[ImagePath] AS [ImagePath], 
                [Limit2].[UserId] AS [UserId], 
                [Limit2].[VisitNumber] AS [VisitNumber], 
                [Limit2].[LanguageId] AS [LanguageId], 
                [Limit2].[ReferenceName] AS [ReferenceName], 
                [Limit2].[ReferenceLink] AS [ReferenceLink], 
                .....
                [Extent7].[Path] AS [Path], 
                [Extent7].[Size] AS [Size], 
                [Extent7].[OrderNumber] AS [OrderNumber], 
                [Extent7].[GroupNumber] AS [GroupNumber], 
                [Extent7].[ContentId] AS [ContentId], 
                [Extent7].[MessageId] AS [MessageId], 
                [Extent7].[IsDownload] AS [IsDownload], 
                [Extent7].[RegistrationDate] AS [RegistrationDate2], 
                [Extent7].[PersianRegistrationDate] AS [PersianRegistrationDate2], 
                CAST(NULL AS int) AS [C6], 
                CAST(NULL AS varchar(1)) AS [C7], 
                CAST(NULL AS varchar(1)) AS [C8], 
                CAST(NULL AS varchar(1)) AS [C9], 
                CAST(NULL AS int) AS [C10], 
                CAST(NULL AS int) AS [C11], 
                CAST(NULL AS smallint) AS [C12], 
                CAST(NULL AS int) AS [C13], 
                CAST(NULL AS datetime2) AS [C14], 
                CAST(NULL AS bit) AS [C15], 
                CAST(NULL AS varchar(1)) AS [C16], 
                CAST(NULL AS int) AS [C17], 
                CAST(NULL AS int) AS [C18], 
                CAST(NULL AS int) AS [C19], 
                CAST(NULL AS int) AS [C20], 
                CAST(NULL AS bit) AS [C21], 
                CAST(NULL AS datetime2) AS [C22], 
                CAST(NULL AS varchar(1)) AS [C23], 
                CAST(NULL AS int) AS [C24], 
                CAST(NULL AS varchar(1)) AS [C25], 
                CAST(NULL AS varchar(1)) AS [C26], 
                CAST(NULL AS varchar(1)) AS [C27], 
                CAST(NULL AS varchar(1)) AS [C28], 
                CAST(NULL AS varchar(1)) AS [C29], 
                CAST(NULL AS int) AS [C30], 
                CAST(NULL AS bit) AS [C31], 
                CAST(NULL AS int) AS [C32], 
                CAST(NULL AS varchar(1)) AS [C33], 
                CAST(NULL AS varchar(1)) AS [C34], 
                CAST(NULL AS varchar(1)) AS [C35], 
                CAST(NULL AS varchar(1)) AS [C36], 
                CAST(NULL AS varchar(1)) AS [C37], 
                CAST(NULL AS datetime2) AS [C38], 
                CAST(NULL AS varchar(1)) AS [C39], 
                CAST(NULL AS datetime2) AS [C40], 
                CAST(NULL AS int) AS [C41], 
                CAST(NULL AS int) AS [C42], 
                CAST(NULL AS datetime2) AS [C43], 
                CAST(NULL AS int) AS [C44], 
                CAST(NULL AS varchar(1)) AS [C45], 
                CAST(NULL AS varchar(1)) AS [C46], 
                CAST(NULL AS varchar(1)) AS [C47], 
                CAST(NULL AS int) AS [C48], 
                CAST(NULL AS varchar(1)) AS [C49], 
                CAST(NULL AS varchar(1)) AS [C50], 
                CAST(NULL AS varchar(1)) AS [C51], 
                CAST(NULL AS varchar(1)) AS [C52]
                FROM   (SELECT TOP (1) 
                    [Extent5].[Id] AS [Id], 
                    [Extent5].[Title] AS [Title], 
                    [Extent5].[Summary] AS [Summary], 
                    [Extent5].[Context] AS [Context], 
                    [Extent5].[Keywords] AS [Keywords], 
                    [Extent5].[ImagePath] AS [ImagePath], 
                    [Extent5].[UserId] AS [UserId], 
                    [Extent5].[VisitNumber] AS [VisitNumber], 
                    [Extent5].[LanguageId] AS [LanguageId], 
                    [Extent5].[ReferenceName] AS [ReferenceName], 
                    [Extent5].[ReferenceLink] AS [ReferenceLink], 
                    [Extent5].[IsSpecial] AS [IsSpecial], 
                     ......
                    [Extent6].[ProfileVisitedNumber] AS [ProfileVisitedNumber], 
                    [Extent6].[MinSalary] AS [MinSalary], 
                    [Extent6].[Telephone] AS [Telephone], 
                    [Extent6].[Mobile] AS [Mobile], 
                    [Extent6].[ZipCode] AS [ZipCode], 
                    [Extent6].[ProvinceId] AS [ProvinceId], 
                    [Extent6].[City] AS [City], 
                    [Extent6].[Address] AS [Address], 
                    [Extent6].[LastRecoveryPasswordDate] AS [LastRecoveryPasswordDate], 
                    [Extent6].[LastIP] AS [LastIP], 
                    [Extent6].[RegistrationDate] AS [RegistrationDate1], 
                    [Extent6].[PersianRegistrationDate] AS [PersianRegistrationDate1]
                    FROM  [dbo].[Content] AS [Extent5]
                    INNER JOIN [dbo].[User] AS [Extent6] ON [Extent5].[UserId] = [Extent6].[Id]
                    WHERE ([Extent5].[Id] = @p__linq__0) AND (@p__linq__0 IS NOT NULL) AND ((([Extent5].[LanguageId] = @p__linq__1) AND ( NOT ([Extent5].[LanguageId] IS NULL OR @p__linq__1 IS NULL))) OR (([Extent5].[LanguageId] IS NULL) AND (@p__linq__1 IS NULL))) AND ((([Extent5].[StatusId] = @p__linq__2) AND ( NOT ([Extent5].[StatusId] IS NULL OR @p__linq__2 IS NULL))) OR (([Extent5].[StatusId] IS NULL) AND (@p__linq__2 IS NULL))) ) AS [Limit2]
                INNER JOIN [dbo].[Attachment] AS [Extent7] ON [Limit2].[Id] = [Extent7].[ContentId]) AS [UnionAll1]
        UNION ALL
            SELECT 
            3 AS [C1], 
            [Limit3].[Id] AS [Id], 
            [Limit3].[Id] AS [Id1], 
            [Limit3].[Title] AS [Title], 
            [Limit3].[Summary] AS [Summary], 
            [Limit3].[Context] AS [Context], 
            [Limit3].[ImagePath] AS [ImagePath], 
            [Limit3].[UserId] AS [UserId], 
            [Limit3].[VisitNumber] AS [VisitNumber], 
            [Limit3].[LanguageId] AS [LanguageId], 
            [Limit3].[ReferenceName] AS [ReferenceName], 
            [Limit3].[ReferenceLink] AS [ReferenceLink], 
            .....
            [Limit3].[ZipCode] AS [ZipCode], 
            [Limit3].[ProvinceId] AS [ProvinceId], 
            [Limit3].[City] AS [City], 
            [Limit3].[Address] AS [Address], 
            [Limit3].[LastIP] AS [LastIP], 
            [Limit3].[PersianRegistrationDate1] AS [PersianRegistrationDate1], 
            CAST(NULL AS int) AS [C2], 
            CAST(NULL AS varchar(1)) AS [C3], 
            CAST(NULL AS varchar(1)) AS [C4], 
            CAST(NULL AS varchar(1)) AS [C5], 
            CAST(NULL AS int) AS [C6], 
            CAST(NULL AS varchar(1)) AS [C7], 
            CAST(NULL AS varchar(1)) AS [C8], 
            CAST(NULL AS varchar(1)) AS [C9], 
            CAST(NULL AS varchar(1)) AS [C10], 
            CAST(NULL AS varchar(1)) AS [C11], 
            CAST(NULL AS bigint) AS [C12], 
            CAST(NULL AS int) AS [C13], 
            CAST(NULL AS int) AS [C14], 
            CAST(NULL AS int) AS [C15], 
            CAST(NULL AS int) AS [C16], 
            CAST(NULL AS bit) AS [C17], 
            CAST(NULL AS datetime2) AS [C18], 
            CAST(NULL AS varchar(1)) AS [C19], 
            [Extent10].[Id] AS [Id3], 
            [Extent10].[Name] AS [Name], 
            [Extent10].[Description] AS [Description], 
            [Extent10].[ImagePath] AS [ImagePath1], 
            [Extent10].[Price] AS [Price], 
            [Extent10].[ContentId] AS [ContentId], 
            [Extent10].[OrderNumber] AS [OrderNumber], 
            [Extent10].[StatusId] AS [StatusId2], 
            [Extent10].[RegistrationDate] AS [RegistrationDate2], 
            [Extent10].[IsDeleted] AS [IsDeleted], 
            [Extent10].[PersianRegistrationDate] AS [PersianRegistrationDate2], 
            CAST(NULL AS int) AS [C20], 
            CAST(NULL AS int) AS [C21], 
            CAST(NULL AS int) AS [C22], 
            CAST(NULL AS int) AS [C23], 
            CAST(NULL AS bit) AS [C24], 
            CAST(NULL AS datetime2) AS [C25], 
            CAST(NULL AS varchar(1)) AS [C26], 
            CAST(NULL AS int) AS [C27], 
            CAST(NULL AS varchar(1)) AS [C28], 
            CAST(NULL AS varchar(1)) AS [C29], 
            CAST(NULL AS varchar(1)) AS [C30], 
            CAST(NULL AS varchar(1)) AS [C31], 
            CAST(NULL AS varchar(1)) AS [C32], 
            CAST(NULL AS int) AS [C33], 
            CAST(NULL AS bit) AS [C34], 
            CAST(NULL AS int) AS [C35], 
            CAST(NULL AS varchar(1)) AS [C36], 
            CAST(NULL AS varchar(1)) AS [C37], 
            CAST(NULL AS varchar(1)) AS [C38], 
            CAST(NULL AS varchar(1)) AS [C39], 
            CAST(NULL AS varchar(1)) AS [C40], 
            CAST(NULL AS datetime2) AS [C41], 
            CAST(NULL AS varchar(1)) AS [C42], 
            CAST(NULL AS datetime2) AS [C43], 
            CAST(NULL AS int) AS [C44], 
            CAST(NULL AS int) AS [C45], 
            CAST(NULL AS datetime2) AS [C46], 
            CAST(NULL AS int) AS [C47], 
            CAST(NULL AS varchar(1)) AS [C48], 
            CAST(NULL AS varchar(1)) AS [C49], 
            CAST(NULL AS varchar(1)) AS [C50], 
            CAST(NULL AS int) AS [C51], 
            CAST(NULL AS varchar(1)) AS [C52], 
            CAST(NULL AS varchar(1)) AS [C53], 
            CAST(NULL AS varchar(1)) AS [C54], 
            CAST(NULL AS varchar(1)) AS [C55]
            FROM   (SELECT TOP (1) 
                [Extent8].[Id] AS [Id], 
                [Extent8].[Title] AS [Title], 
                [Extent8].[Summary] AS [Summary], 
                [Extent8].[Context] AS [Context], 
                [Extent8].[Keywords] AS [Keywords], 
                [Extent8].[ImagePath] AS [ImagePath], 
                [Extent8].[UserId] AS [UserId], 
                ....
                [Extent9].[ProfileVisitedNumber] AS [ProfileVisitedNumber], 
                [Extent9].[MinSalary] AS [MinSalary], 
                [Extent9].[Telephone] AS [Telephone], 
                [Extent9].[Mobile] AS [Mobile], 
                [Extent9].[ZipCode] AS [ZipCode], 
                [Extent9].[ProvinceId] AS [ProvinceId], 
                [Extent9].[City] AS [City], 
                [Extent9].[Address] AS [Address], 
                [Extent9].[LastRecoveryPasswordDate] AS [LastRecoveryPasswordDate], 
                [Extent9].[LastIP] AS [LastIP], 
                [Extent9].[RegistrationDate] AS [RegistrationDate1], 
                [Extent9].[PersianRegistrationDate] AS [PersianRegistrationDate1]
                FROM  [dbo].[Content] AS [Extent8]
                INNER JOIN [dbo].[User] AS [Extent9] ON [Extent8].[UserId] = [Extent9].[Id]
                WHERE ([Extent8].[Id] = @p__linq__0) AND (@p__linq__0 IS NOT NULL) AND ((([Extent8].[LanguageId] = @p__linq__1) AND ( NOT ([Extent8].[LanguageId] IS NULL OR @p__linq__1 IS NULL))) OR (([Extent8].[LanguageId] IS NULL) AND (@p__linq__1 IS NULL))) AND ((([Extent8].[StatusId] = @p__linq__2) AND ( NOT ([Extent8].[StatusId] IS NULL OR @p__linq__2 IS NULL))) OR (([Extent8].[StatusId] IS NULL) AND (@p__linq__2 IS NULL))) ) AS [Limit3]
            INNER JOIN [dbo].[Product] AS [Extent10] ON [Limit3].[Id] = [Extent10].[ContentId]) AS [UnionAll2]
    UNION ALL
        SELECT 
        4 AS [C1], 
        [Limit4].[Id] AS [Id], 
        [Limit4].[Id] AS [Id1], 
        [Limit4].[Title] AS [Title], 
        [Limit4].[Summary] AS [Summary], 
        [Limit4].[Context] AS [Context], 
        [Limit4].[ImagePath] AS [ImagePath], 
        [Limit4].[UserId] AS [UserId], 
        [Limit4].[VisitNumber] AS [VisitNumber], 
        .....
        [Limit4].[City] AS [City], 
        [Limit4].[Address] AS [Address], 
        [Limit4].[LastIP] AS [LastIP], 
        [Limit4].[PersianRegistrationDate1] AS [PersianRegistrationDate1], 
        CAST(NULL AS int) AS [C2], 
        CAST(NULL AS varchar(1)) AS [C3], 
        CAST(NULL AS varchar(1)) AS [C4], 
        CAST(NULL AS varchar(1)) AS [C5], 
        CAST(NULL AS int) AS [C6], 
        CAST(NULL AS varchar(1)) AS [C7], 
        CAST(NULL AS varchar(1)) AS [C8], 
        ....
        CAST(NULL AS varchar(1)) AS [C23], 
        CAST(NULL AS int) AS [C24], 
        CAST(NULL AS int) AS [C25], 
        CAST(NULL AS smallint) AS [C26], 
        CAST(NULL AS int) AS [C27], 
        CAST(NULL AS datetime2) AS [C28], 
        CAST(NULL AS bit) AS [C29], 
        CAST(NULL AS varchar(1)) AS [C30], 
        [Join9].[Id1] AS [Id3], 
        [Join9].[Id1] AS [Id4], 
        ...
        [Join9].[City] AS [City1], 
        [Join9].[Address] AS [Address1], 
        [Join9].[LastIP] AS [LastIP1], 
        [Join9].[PersianRegistrationDate] AS [PersianRegistrationDate2]
        FROM   (SELECT TOP (1) 
            [Extent11].[Id] AS [Id], 
            [Extent11].[Title] AS [Title], 
            [Extent11].[Summary] AS [Summary], 
            [Extent11].[Context] AS [Context], 
            [Extent11].[Keywords] AS [Keywords], 
            [Extent11].[ImagePath] AS [ImagePath], 
            [Extent11].[UserId] AS [UserId], 
            [Extent11].[VisitNumber] AS [VisitNumber], 
            [Extent11].[LanguageId] AS [LanguageId], 
            [Extent11].[ReferenceName] AS [ReferenceName], 
            [Extent11].[ReferenceLink] AS [ReferenceLink], 
            [Extent11].[IsSpecial] AS [IsSpecial], 
            [Extent11].[RegistrationDate] AS [RegistrationDate], 
            [Extent11].[StatusId] AS [StatusId], 
            [Extent11].[SubjectId] AS [SubjectId], 
            [Extent11].[LockCommenting] AS [LockCommenting], 
            [Extent11].[AllTagsString] AS [AllTagsString], 
            [Extent11].[OnlyVisibleToMembers] AS [OnlyVisibleToMembers], 
            [Extent11].[PersianRegistrationDate] AS [PersianRegistrationDate], 
            [Extent12].[Id] AS [Id1], 
            [Extent12].[FirstName] AS [FirstName], 
            [Extent12].[LastName] AS [LastName], 
            [Extent12].[FullName] AS [FullName], 
            [Extent12].[Username] AS [Username], 
            [Extent12].[Password] AS [Password], 
            [Extent12].[Email] AS [Email], 
            [Extent12].[RoleId] AS [RoleId], 
            [Extent12].[IsAdmin] AS [IsAdmin], 
            [Extent12].[LanguageId] AS [LanguageId1], 
            [Extent12].[PhotoPath] AS [PhotoPath], 
            [Extent12].[StatusId] AS [StatusId1], 
            [Extent12].[LastVisitDate] AS [LastVisitDate], 
            [Extent12].[AboutUser] AS [AboutUser], 
            [Extent12].[TwitterPage] AS [TwitterPage], 
            [Extent12].[FaceBookPage] AS [FaceBookPage], 
            [Extent12].[PersianblogPage] AS [PersianblogPage], 
            [Extent12].[ProfileVisitedNumber] AS [ProfileVisitedNumber], 
            [Extent12].[MinSalary] AS [MinSalary], 
            [Extent12].[Telephone] AS [Telephone], 
            [Extent12].[Mobile] AS [Mobile], 
            [Extent12].[ZipCode] AS [ZipCode], 
            [Extent12].[ProvinceId] AS [ProvinceId], 
            [Extent12].[City] AS [City], 
            [Extent12].[Address] AS [Address], 
            [Extent12].[LastRecoveryPasswordDate] AS [LastRecoveryPasswordDate], 
            [Extent12].[LastIP] AS [LastIP], 
            [Extent12].[RegistrationDate] AS [RegistrationDate1], 
            [Extent12].[PersianRegistrationDate] AS [PersianRegistrationDate1]
            FROM  [dbo].[Content] AS [Extent11]
            INNER JOIN [dbo].[User] AS [Extent12] ON [Extent11].[UserId] = [Extent12].[Id]
            WHERE ([Extent11].[Id] = @p__linq__0) AND (@p__linq__0 IS NOT NULL) AND ((([Extent11].[LanguageId] = @p__linq__1) AND ( NOT ([Extent11].[LanguageId] IS NULL OR @p__linq__1 IS NULL))) OR (([Extent11].[LanguageId] IS NULL) AND (@p__linq__1 IS NULL))) AND ((([Extent11].[StatusId] = @p__linq__2) AND ( NOT ([Extent11].[StatusId] IS NULL OR @p__linq__2 IS NULL))) OR (([Extent11].[StatusId] IS NULL) AND (@p__linq__2 IS NULL))) ) AS [Limit4]
        INNER JOIN  (SELECT [Extent13].[Id] AS [Id1], [Extent13].[ContentId] AS [ContentId], [Extent13].[UserId] AS [UserId], [Extent13].[IsReadyToPublish] AS [IsReadyToPublish], [Extent13].[EditDate] AS [EditDate], [Extent13].[PersianEditDate] AS [PersianEditDate], [Extent14].[Id] AS [Id2], [Extent14].[FirstName] AS [FirstName], [Extent14].[LastName] AS [LastName], [Extent14].[FullName] AS [FullName], [Extent14].[Username] AS [Username], [Extent14].[Password] AS [Password], [Extent14].[Email] AS [Email], [Extent14].[RoleId] AS [RoleId], [Extent14].[IsAdmin] AS [IsAdmin], [Extent14].[LanguageId] AS [LanguageId], [Extent14].[PhotoPath] AS [PhotoPath], [Extent14].[StatusId] AS [StatusId], [Extent14].[LastVisitDate] AS [LastVisitDate], [Extent14].[AboutUser] AS [AboutUser], [Extent14].[TwitterPage] AS [TwitterPage], [Extent14].[FaceBookPage] AS [FaceBookPage], [Extent14].[PersianblogPage] AS [PersianblogPage], [Extent14].[ProfileVisitedNumber] AS [ProfileVisitedNumber], [Extent14].[MinSalary] AS [MinSalary], [Extent14].[Telephone] AS [Telephone], [Extent14].[Mobile] AS [Mobile], [Extent14].[ZipCode] AS [ZipCode], [Extent14].[ProvinceId] AS [ProvinceId], [Extent14].[City] AS [City], [Extent14].[Address] AS [Address], [Extent14].[LastRecoveryPasswordDate] AS [LastRecoveryPasswordDate], [Extent14].[LastIP] AS [LastIP], [Extent14].[RegistrationDate] AS [RegistrationDate], [Extent14].[PersianRegistrationDate] AS [PersianRegistrationDate]
            FROM  [dbo].[ContentEditor] AS [Extent13]
            INNER JOIN [dbo].[User] AS [Extent14] ON [Extent13].[UserId] = [Extent14].[Id] ) AS [Join9] ON [Limit4].[Id] = [Join9].[ContentId]) AS [UnionAll3]
    ORDER BY [UnionAll3].[C3] ASC, [UnionAll3].[C22] ASC, [UnionAll3].[C1] ASC',N'@p__linq__0 int,@p__linq__1 int,@p__linq__2 int',@p__linq__0=2223,@p__linq__1=1,@p__linq__2=2

有没有办法优化它?如果没有办法,我们如何编写优化的存储过程而不是上面的EF代码?

【问题讨论】:

【参考方案1】:

无法修改从 EF 生成的 SQL。如果您需要更优化的 SQL,那么您最好的选择是编写自己的存储过程并调用它。

【讨论】:

能否指导我如何为上述查询编写优化的存储过程? @Mohammad 这不是问题所在,如果您有想要优化的 SQL 语句,我建议您创建一个单独的问题,详细说明为什么您当前的 SQL 语句对您来说不够高效。在你的问题中生成的 SQL 看起来非常复杂,有很多联合/连接 - 这可能是它不是非常快的原因。 可以使用dbContext.Database.SqlQuery(string sql);运行你自己的 sql @BappiDatta 是的,这实际上是 TSQL,具有通过网络发送的额外开销。考虑到 SP 是预编译的并且已经在服务器上,它们更加优化。

以上是关于优化实体框架中生成的sql查询?的主要内容,如果未能解决你的问题,请参考以下文章

内联 SQL 查询和实体框架触发的查询有啥区别?

优化实体框架生成的 SQL Server 执行计划

为啥实体框架会生成嵌套 SQL 查询?

如何查看查询转换器在 Oracle 中生成的查询

ORA-00904 Linq 在 Oracle 的 SQL 查询中生成错误

实体框架 SQL 查询