0

我将 ASP.NET 4.5、Entity Framework 5.0 和 LINQ 与 SQL Server 2012 一起使用。

这是一个基于网络的房地产网站应用程序。该查询从中提取数据的表大约有 200 列和大约 320 万行。

当我使用 EF 获取一些数据时,它会生成以下查询:

SELECT TOP (2) 
[Filter1].[ListingID] AS [ListingID], 
[Filter1].[Address] AS [Address], 
[Filter1].[Beds] AS [Beds], 
[Filter1].[BathsFull] AS [BathsFull], 
[Filter1].[ListPrice] AS [ListPrice], 
[Filter1].[RemarksPublic] AS [RemarksPublic], 
[Filter1].[MLSBoardID] AS [MLSBoardID], 
[Filter1].[PropertyTypeID] AS [PropertyTypeID], 
[Filter1].[sysid] AS [sysid], 
[Filter1].[ListAgentID] AS [ListAgentID], 
[Filter1].[LastUpdateDate] AS [LastUpdateDate], 
[Filter1].[ShowAddressOnlineYN] AS [ShowAddressOnlineYN], 
[Filter1].[City] AS [City], 
[Filter1].[State] AS [State], 
[Filter1].[ZipCode] AS [ZipCode], 
[Filter1].[MLSNumber] AS [MLSNumber], 
[Filter1].[County] AS [County], 
[Filter1].[LeasePrice] AS [LeasePrice]
FROM ( SELECT [Extent1].[ListingID] AS [ListingID], [Extent1].[MLSBoardID] AS [MLSBoardID], [Extent1].[PropertyTypeID] AS [PropertyTypeID], [Extent1].[sysid] AS [sysid], [Extent1].[Address] AS [Address], [Extent1].[BathsFull] AS [BathsFull], [Extent1].[Beds] AS [Beds], [Extent1].[City] AS [City], [Extent1].[County] AS [County], [Extent1].[LastUpdateDate] AS [LastUpdateDate], [Extent1].[LeasePrice] AS [LeasePrice], [Extent1].[ListAgentID] AS [ListAgentID], [Extent1].[ListPrice] AS [ListPrice], [Extent1].[MLSNumber] AS [MLSNumber], [Extent1].[RemarksPublic] AS [RemarksPublic], [Extent1].[ShowAddressOnlineYN] AS [ShowAddressOnlineYN], [Extent1].[State] AS [State], [Extent1].[ZipCode] AS [ZipCode], row_number() OVER (ORDER BY (Case When [Extent1].[ListAgentID] IN ('##########') Then 0 Else 1 End), ListPrice DESC, LeasePrice DESC) AS [row_number]
    FROM [dbo].[Listings] AS [Extent1]
    WHERE 1009 = [Extent1].[PropertyTypeID]
)  AS [Filter1]
WHERE [Filter1].[row_number] > 0
ORDER BY (Case When [Filter1].[ListAgentID] IN ('##########') Then 0 Else 1 End), ListPrice DESC, LeasePrice DESC

此查询估计需要 45-50 秒来执行并返回 2 行。我可以从执行计划中看到,大部分成本都在“排序”和“排序(前 N 个排序)”中。这些占整个查询成本的 79%。

我手动将上述查询重写为:

SELECT TOP (2) 
[ListingID], 
[Address], 
[Beds], 
[BathsFull], 
[ListPrice], 
[RemarksPublic], 
[MLSBoardID], 
[PropertyTypeID], 
[sysid], 
[ListAgentID], 
[LastUpdateDate], 
[ShowAddressOnlineYN], 
[City], 
[State], 
[ZipCode], 
[MLSNumber], 
[County], 
[LeasePrice]
FROM  dbo.Listings
WHERE 1009 = [PropertyTypeID]
ORDER BY (Case When [ListAgentID] IN ('##########') Then 0 Else 1 End), ListPrice DESC, LeasePrice DESC

我的查询返回与之前完全相同的结果,但运行时间不到 1 秒(“排序(前 N 个排序)”占成本的 91%)。两个查询在其执行计划的“索引查找”上使用相同的索引。

如何使第一个查询运行得更快?我已经尝试了各种带有索引的东西,但似乎无法让它以我编写的第二个查询的速度执行。

任何和所有的帮助表示赞赏。如果您需要更多详细信息,请告诉我。

4

1 回答 1

1

感谢大家的意见。

@Rowland - 很难显示 LINQ 查询,因为它是完全动态的并且集成到一个非常复杂的系统中,其中查询是由多个因素生成的。

@meda - 是的,有一个索引将字段“PropertyTypeID”作为其字段之一。有问题吗?

@Martin - 你是对的。

该问题与生成订单的方式有关。我调整了该方法,这是 LINQ to EF 生成的 SQL 查询以提取相同的结果:

SELECT TOP (2) 
[Project1].[ListingID] AS [ListingID], 
[Project1].[Address] AS [Address], 
[Project1].[Beds] AS [Beds], 
[Project1].[BathsFull] AS [BathsFull], 
[Project1].[ListPrice] AS [ListPrice], 
[Project1].[RemarksPublic] AS [RemarksPublic], 
[Project1].[MLSBoardID] AS [MLSBoardID], 
[Project1].[PropertyTypeID] AS [PropertyTypeID], 
[Project1].[sysid] AS [sysid], 
[Project1].[ListAgentID] AS [ListAgentID], 
[Project1].[LastUpdateDate] AS [LastUpdateDate], 
[Project1].[ShowAddressOnlineYN] AS [ShowAddressOnlineYN], 
[Project1].[City] AS [City], 
[Project1].[State] AS [State], 
[Project1].[ZipCode] AS [ZipCode], 
[Project1].[MLSNumber] AS [MLSNumber], 
[Project1].[County] AS [County], 
[Project1].[LeasePrice] AS [LeasePrice]
FROM ( SELECT 
    CASE WHEN ([Extent1].[ListAgentID] IN ('XXXXXXXXX')) THEN 0 ELSE 1 END AS [C1], 
    [Extent1].[ListingID] AS [ListingID], 
    [Extent1].[MLSBoardID] AS [MLSBoardID], 
    [Extent1].[PropertyTypeID] AS [PropertyTypeID], 
    [Extent1].[sysid] AS [sysid], 
    [Extent1].[Address] AS [Address], 
    [Extent1].[BathsFull] AS [BathsFull], 
    [Extent1].[Beds] AS [Beds], 
    [Extent1].[City] AS [City], 
    [Extent1].[County] AS [County], 
    [Extent1].[LastUpdateDate] AS [LastUpdateDate], 
    [Extent1].[LeasePrice] AS [LeasePrice], 
    [Extent1].[ListAgentID] AS [ListAgentID], 
    [Extent1].[ListPrice] AS [ListPrice], 
    [Extent1].[MLSNumber] AS [MLSNumber], 
    [Extent1].[RemarksPublic] AS [RemarksPublic], 
    [Extent1].[ShowAddressOnlineYN] AS [ShowAddressOnlineYN], 
    [Extent1].[State] AS [State], 
    [Extent1].[ZipCode] AS [ZipCode]
    FROM [dbo].[Listings] AS [Extent1]
    WHERE 1009 = [Extent1].[PropertyTypeID]
)  AS [Project1]
ORDER BY [Project1].[C1] ASC, [Project1].[ListPrice] DESC, [Project1].[LeasePrice] DESC

这需要不到一秒钟的时间来执行。谢谢各位的意见。

于 2013-07-30T12:27:35.980 回答