我只有2个表,产品和图片。在产品中我只有50,000行,在图像中我有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是主键和标识列。
答案 0 :(得分:0)
好的,我稍后添加了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会这样做。