我只有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是主鍵和標識列。
什麼是你的代碼,你寫它輸出此查詢? – CallumVass
@BiffBaffBoff:如下標籤,它必須是生成這個野獸的實體框架6。 –
是的,但顯示你正在編寫生成此查詢的代碼..即你的方法 – CallumVass