0

我只有2個表,產品和圖像。在產品中,我只有5萬行,圖像中有10行。我寫了一個非常簡單的查詢,可以給我十大產品。其實這個查詢是由EF生成的。簡單查詢在SQL Server中緩慢運行?

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

什麼是你的代碼,你寫它輸出此查詢? – CallumVass

+1

@BiffBaffBoff:如下標籤,它必須是生成這個野獸的實體框架6。 –

+0

是的,但顯示你正在編寫生成此查詢的代碼..即你的方法 – CallumVass

回答

0

OK我已經加入隨後加入,然後開始工作快。

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做這些東西。

+0

這不是有效的SQL –

+0

@ElectricLlama,你確定嗎?同樣的查詢在我的SQL Management Studio中 – user960567

+0

它的題幹,因爲這是適用於它的行號,排序和過濾的相當多的時間運行。行號是一個動態列,它沒有任何索引。因此數據庫必須做大量的處理(可能訪問每一行)來找出你想要的行。你可以通過查看訪問的行數來檢查執行計劃(如果這等於表中所做的所有辛苦工作的行數)。使其更快的方法是找到另一種排序和篩選方式,而不需要row_number。 – sarin