简单查询在SQL Server中运行缓慢?

简单查询在SQL Server中运行缓慢?,sql,sql-server,entity-framework,sql-server-2012,entity-framework-6,Sql,Sql Server,Entity Framework,Sql Server 2012,Entity Framework 6,我只有2张桌子、产品和图片。在产品中我只有50000行,在图像中我有10行。我已经写了一个非常简单的查询,将给我前10名的产品。实际上,这个查询是由EF生成的 SELECT TOP (10) [Join1].[Id1] AS [Id], [Join1].[Name] AS [Name], [Join1].[Price] AS [Price], [Join1].[NewPrice] AS [NewPrice], [Join1].[ShortDesc

我只有2张桌子、产品和图片。在产品中我只有50000行,在图像中我有10行。我已经写了一个非常简单的查询,将给我前10名的产品。实际上,这个查询是由EF生成的

SELECT TOP (10) 
    [Join1].[Id1] AS [Id], 
    [Join1].[Name] AS [Name], 
    [Join1].[Price] AS [Price], 
    [Join1].[NewPrice] AS [NewPrice], 
    [Join1].[ShortDescription] AS [ShortDescription], 
    [Join1].[SKU] AS [SKU], 
    [Join1].[ProductTypeID] AS [ProductTypeID], 
    [Join1].[ImageID] AS [ImageID], 
    [Join1].[Promotion] AS [Promotion], 
    [Join1].[ParentID] AS [ParentID], 
    [Join1].[Attributes] AS [Attributes], 
    [Join1].[Id2] AS [Id1], 
    [Join1].[Path] AS [Path]
FROM (
   SELECT [Extent1].[Id] AS [Id1]
        , [Extent1].[Name] AS [Name]
        , [Extent1].[Price] AS [Price]
        , [Extent1].[NewPrice] AS [NewPrice]
        , [Extent1].[ShortDescription] AS [ShortDescription]
        , [Extent1].[SKU] AS [SKU]
        , [Extent1].[ProductTypeID] AS [ProductTypeID]
        , [Extent1].[ImageID] AS [ImageID]
        , [Extent1].[Promotion] AS [Promotion]
        , [Extent1].[ParentID] AS [ParentID]
        , [Extent1].[Attributes] AS [Attributes]
        , [Extent2].[Id] AS [Id2]
        , [Extent2].[Path] AS [Path]
        , row_number() OVER (ORDER BY [Extent1].[Id] ASC) AS [row_number]
   FROM  [dbo].[Products] AS [Extent1]
   INNER JOIN [dbo].[Images] AS [Extent2] ON [Extent1].[ImageID] = [Extent2].[Id]
)  AS [Join1]
WHERE [Join1].[row_number] > 0
ORDER BY [Join1].[Id1] ASC

但是这个查询需要3秒钟。如何提高此查询性能。Id是主键和标识列。

好的,我添加了JOIN,然后它开始快速工作

SELECT TOP (10)
    [Join1].[Id1] AS [Id],
    [Join1].[Name] AS [Name],
    [Join1].[Price] AS [Price],
    [Join1].[NewPrice] AS [NewPrice],
    [Join1].[ShortDescription] AS [ShortDescription],
    [Join1].[SKU] AS [SKU],
    [Join1].[ProductTypeID] AS [ProductTypeID],
    [Join1].[ImageID] AS [ImageID],
    [Join1].[Promotion] AS [Promotion],
    [Join1].[ParentID] AS [ParentID],
    [Join1].[Attributes] AS [Attributes],
    [Join2].[Id] AS [Id2],
    [Join2].[Path] AS [Path]
FROM (SELECT
    [Extent1].[Id] AS [Id1],
    [Extent1].[Name] AS [Name],
    [Extent1].[Price] AS [Price],
    [Extent1].[NewPrice] AS [NewPrice],
    [Extent1].[ShortDescription] AS [ShortDescription],
    [Extent1].[SKU] AS [SKU],
    [Extent1].[ProductTypeID] AS [ProductTypeID],
    [Extent1].[ImageID] AS [ImageID],
    [Extent1].[Promotion] AS [Promotion],
    [Extent1].[ParentID] AS [ParentID],
    [Extent1].[Attributes] AS [Attributes],
    ROW_NUMBER() OVER (ORDER BY [Extent1].[Id] ASC) AS [row_number]
FROM [dbo].[Products] AS [Extent1]) AS [Join1]
INNER JOIN [dbo].[Images] AS [Join2] ON [Join1].[ImageID] = [Join2].[Id]
WHERE [Join1].[row_number] > 0
ORDER BY [Join1].[Id1] ASC

但我不明白EF为什么要做这些事情。

您正在编写的输出此查询的代码是什么?@biffbaffoff:从标记中可以看出,生成此beast的必须是Entity Framework 6。是的,但请显示生成此查询的您正在编写的代码。。Ie你的LINQ代码在你的method@BiffBaffBoff,代码是简单的EF
products.include(p=>p.Image)
那么您一次返回50000个产品?您正在从
产品
图像
中选择每一列?为什么?你确定吗?在我的sql管理研究中,同样的查询也在运行,这可能需要相当多的时间,因为行数以及应用于它的排序和筛选。行号是一个动态列,没有任何索引。因此,数据库必须进行大量处理(可能访问每一行),以确定需要哪些行。您可以通过查看访问的行数来检查执行计划中的这一点(如果这等于表中的行数,那么它将完成所有的繁重工作)。更快的方法是找到另一种没有行号的排序和过滤方法。对不起,我错了,我错过了结束括号。