我有以下查询,查找我们的房地产数据库中特定商家信息的降价。我想知道这个查询是否可以更有效地完成。最终目标是在最近的价格变化小于之前的价格变化的列表中获得最后2个价格变化。这是我的疑问:
var PriceDrops = idxContext.ListingPriceChanges
.Where(a => a.DateAdded >= LastRunTime && ListingIDsChunk.Contains(a.ListingID))
.GroupBy(g => g.ListingID)
.Where(g => g.Count() > 1 && g.OrderByDescending(a => a.DateAdded).FirstOrDefault().ListPrice < g.OrderByDescending(a => a.DateAdded).Skip(1).FirstOrDefault().ListPrice)
.SelectMany(g => g.OrderByDescending(a => a.DateAdded).Take(2)).ToList();
此查询有效,我只是想知道它是否可以更有效地完成。
以下是根据要求提供的更多信息:
每次列出IDID大小为2000
id
执行时间:20.4172256 seconds
(基于秒表)
它生成的SQL是:
SELECT
[Limit3].[ListingPriceChangeID] AS [ListingPriceChangeID],
[Limit3].[ListingID] AS [ListingID],
[Limit3].[ListPrice] AS [ListPrice],
[Limit3].[PriceChangeDate] AS [PriceChangeDate],
[Limit3].[DateAdded] AS [DateAdded]
FROM (SELECT [Project7].[ListingID] AS [ListingID]
FROM ( SELECT
[Project6].[ListingID] AS [ListingID],
[Project6].[ListPrice] AS [ListPrice],
[Project6].[ListPrice1] AS [ListPrice1],
(SELECT
COUNT(1) AS [A1]
FROM [dbo].[ListingPriceChanges] AS [Extent4]
WHERE ([Extent4].[DateAdded] >= @p__linq__0) AND ([Extent4].[ListingID] IN (REMOVED-2000-IDs)) AND ([Project6].[ListingID] = [Extent4].[ListingID])) AS [C1]
FROM ( SELECT
[Project4].[ListingID] AS [ListingID],
[Project4].[ListPrice] AS [ListPrice],
[Limit2].[ListPrice] AS [ListPrice1]
FROM (SELECT
[Project2].[ListingID] AS [ListingID],
[Limit1].[ListPrice] AS [ListPrice]
FROM (SELECT
@p__linq__0 AS [p__linq__0],
[Distinct1].[ListingID] AS [ListingID]
FROM ( SELECT DISTINCT
[Extent1].[ListingID] AS [ListingID]
FROM [dbo].[ListingPriceChanges] AS [Extent1]
WHERE ([Extent1].[DateAdded] >= @p__linq__0) AND ([Extent1].[ListingID] IN (REMOVED-2000-IDs))
) AS [Distinct1] ) AS [Project2]
OUTER APPLY (SELECT TOP (1) [Project3].[ListPrice] AS [ListPrice]
FROM ( SELECT
[Extent2].[ListPrice] AS [ListPrice],
[Extent2].[DateAdded] AS [DateAdded]
FROM [dbo].[ListingPriceChanges] AS [Extent2]
WHERE ([Extent2].[DateAdded] >= @p__linq__0) AND ([Extent2].[ListingID] IN (REMOVED-2000-IDs)) AND ([Project2].[ListingID] = [Extent2].[ListingID])
) AS [Project3]
ORDER BY [Project3].[DateAdded] DESC ) AS [Limit1] ) AS [Project4]
OUTER APPLY (SELECT TOP (1) [Project5].[ListPrice] AS [ListPrice], [Project5].[DateAdded] AS [DateAdded]
FROM ( SELECT [Project5].[ListPrice] AS [ListPrice], [Project5].[DateAdded] AS [DateAdded], row_number() OVER (ORDER BY [Project5].[DateAdded] DESC) AS [row_number]
FROM ( SELECT
[Extent3].[ListPrice] AS [ListPrice],
[Extent3].[DateAdded] AS [DateAdded]
FROM [dbo].[ListingPriceChanges] AS [Extent3]
WHERE ([Extent3].[DateAdded] >= @p__linq__0) AND ([Extent3].[ListingID] IN (REMOVED-2000-IDs)) AND ([Project4].[ListingID] = [Extent3].[ListingID])
) AS [Project5]
) AS [Project5]
WHERE [Project5].[row_number] > 1
ORDER BY [Project5].[DateAdded] DESC ) AS [Limit2]
) AS [Project6]
) AS [Project7]
WHERE ([Project7].[C1] > 1) AND ([Project7].[ListPrice] < [Project7].[ListPrice1]) ) AS [Filter5]
CROSS APPLY (SELECT TOP (2) [Project8].[ListingPriceChangeID] AS [ListingPriceChangeID], [Project8].[ListingID] AS [ListingID], [Project8].[ListPrice] AS [ListPrice], [Project8].[PriceChangeDate] AS [PriceChangeDate], [Project8].[DateAdded] AS [DateAdded]
FROM ( SELECT
[Extent5].[ListingPriceChangeID] AS [ListingPriceChangeID],
[Extent5].[ListingID] AS [ListingID],
[Extent5].[ListPrice] AS [ListPrice],
[Extent5].[PriceChangeDate] AS [PriceChangeDate],
[Extent5].[DateAdded] AS [DateAdded]
FROM [dbo].[ListingPriceChanges] AS [Extent5]
WHERE ([Extent5].[DateAdded] >= @p__linq__0) AND ([Extent5].[ListingID] IN (REMOVED-2000-IDs)) AND ([Filter5].[ListingID] = [Extent5].[ListingID])
) AS [Project8]
ORDER BY [Project8].[DateAdded] DESC ) AS [Limit3]
ListingPriceChanges表中目前有3,239,193
个记录。
如果您需要更多信息,请告诉我们。在上面的查询中,我用文本REMOVED-2000-IDs
我正在使用EF 5.0和.NET 4.5
ListingPriceChanges
表定义为:
[ListingPriceChangeID] [int] IDENTITY(1,1) NOT NULL,
[ListingID] [int] NOT NULL,
[ListPrice] [money] NOT NULL,
[PriceChangeDate] [datetime2](7) NULL,
[DateAdded] [datetime2](7) NOT NULL
字段ListingID
是对“清单”表的FK引用。 ListingID
字段上的表格还有一个索引,其中包含PriceChangeDate
。
答案 0 :(得分:3)
我目前无法测试,但我相信你可以做到以下几点:
var PriceDrops = idxContext.ListingPriceChanges
.Where(a => a.DateAdded >= LastRunTime && ListingIDsChunk.Contains(a.ListingID))
.GroupBy(g => g.ListingID)
.Where(g => g.Count() > 1)
.Select(g => g.OrderByDescending(a => a.DateAdded).Take(2))
.Where(g => g.First().ListPrice < g.Skip(1).First().ListPrice)
.SelectMany(g => g)
.ToList();
这应评估更少的子查询,我相信这可能有助于您的整体表现。它也更容易理解。
答案 1 :(得分:1)
使用对Reed答案的修改,我能够在2000上市批次中将平均执行时间从20秒减少到13秒。我使用下面的解决方案并将平均执行时间降低到大约3秒。我在选择初始集合之后但在ListPrice比较之前需要调用.ToList()。
var PriceDrops = idxContext.ListingPriceChanges
.Where(a => a.DateAdded >= LastRunTime && ListingIDsChunk.Contains(a.ListingID))
.GroupBy(g => g.ListingID)
.Where(g => g.Count() > 1)
.Select(g => g.OrderByDescending(a => a.DateAdded).Take(2))
.ToList()
.Where(g => g.First().ListPrice < g.Last().ListPrice)
.SelectMany(g => g)
.ToList();