1

我这里有点奇怪。我在运行批处理的控制台应用程序中使用 Entity Framework Code First。代码每次循环执行存储过程的一系列日期。

目前它循环了大约 300 次,随着时间的推移,每次执行都会变得越来越慢,直到它爬行时接近尾声。

我试过内存分析,但不是这样。这是示例代码。

 _dbContext = new FooContext();
 _barService = new BarService(new GenericRepository<Bar>(), _dbContext);

 for (var date = lastCalculatedDate.AddDays(1); date <= yesterday; date = date.AddDays(1))
 {
     _barService.CalculateWeightings(date);
 }

所有 CalculateWeightings 所做的是(我也在使用 nlog)

public void CalculateWeightings(DateTime dateTime)
{
    _logger.Info("Calculating weightings for {1}", dateTime);
    Context.Database.ExecuteSqlCommand("EXEC CalculateWeightings @dateTime", new SqlParameter("@dateTime", dateTime);
}

存储过程只是用一些记录填充一个表。没什么复杂的,表格最终有几行 1000 行,所以问题不存在

有什么想法吗?

对于那些想要查看 sql 的人。它有点庞然大物,但我看不出有任何原因会随着时间的推移而减慢。处理的行数非常少。

CREATE PROCEDURE [dbo].[CalculateWeightings]
    @StartDate DateTime,
    @EndDate DateTime,
    @TradedMonthStart DateTime,
    @InstrumentGroupId int
AS
BEGIN
    ---- GET ALL THE END OF DAY PRICINGS FOR MONTHLYS ----
    SELECT 
    ROW_NUMBER() OVER 
                    (
                        PARTITION BY RawTrades.FirstSequenceItemName, 
                        CONVERT(VARCHAR, RawTrades.LastUpdate, 103) 
                        ORDER BY RawTrades.FirstSequenceItemName, RawTrades.LastUpdate DESC
                    ) AS [Row], 
    RawTrades.FirstSequenceItemID AS MonthId, 
    Sequences.ActualStartMonth,
    Sequences.ActualEndMonth,
    RawTrades.FirstSequenceItemName AS [MonthName], 
    CONVERT(VARCHAR, RawTrades.LastUpdate, 103) AS LastUpdate,
    RawTrades.Price
    INTO #monthly
    FROM RawTrades
    INNER JOIN Sequences ON RawTrades.FirstSequenceItemId = Sequences.SequenceItemId AND RawTrades.FirstSequenceId = Sequences.SequenceId
    WHERE RawTrades.FirstSequenceID IN (SELECT MonthlySequenceId FROM Instruments WHERE InstrumentGroupId = @InstrumentGroupId) 
    AND [Action] <> 'Remove'
    AND LastUpdate >= @StartDate
    AND LastUpdate < @EndDate
    AND ActualStartMonth >= @TradedMonthStart
    ORDER BY RawTrades.FirstSequenceItemID, RawTrades.LastUpdate DESC

    ---- GET ALL THE END OF DAY PRICINGS FOR QUARTERLYS ----
    SELECT 
    ROW_NUMBER() OVER 
                    (
                        PARTITION BY RawTrades.FirstSequenceItemName,  
                        CONVERT(VARCHAR, RawTrades.LastUpdate, 103) 
                        ORDER BY RawTrades.FirstSequenceItemName, RawTrades.LastUpdate DESC
                    ) AS [Row], 
    CONVERT(VARCHAR, RawTrades.LastUpdate, 103) AS LastUpdate, 
    Sequences.ActualStartMonth,
    Sequences.ActualEndMonth,
    RawTrades.Price
    INTO #quarterly
    FROM RawTrades
    INNER JOIN Sequences ON RawTrades.FirstSequenceItemId = Sequences.SequenceItemId AND RawTrades.FirstSequenceId = Sequences.SequenceId
    WHERE RawTrades.FirstSequenceID IN (SELECT QuarterlySequenceId FROM Instruments WHERE InstrumentGroupId = @InstrumentGroupId)  
    AND Action <> 'Remove' 
    AND LastUpdate >= @StartDate
    AND LastUpdate < @EndDate
    AND RawTrades.Price > 20
    ORDER BY RawTrades.FirstSequenceItemID, RawTrades.LastUpdate DESC

    ---- GET ALL THE END OF DAY PRICINGS FOR QUARTERLYS ----
    SELECT 
    ROW_NUMBER() OVER 
                    (
                        PARTITION BY RawTrades.FirstSequenceItemName,  
                        CONVERT(VARCHAR, RawTrades.LastUpdate, 103) 
                        ORDER BY RawTrades.FirstSequenceItemName, RawTrades.LastUpdate DESC
                    ) AS [Row], 
    CONVERT(VARCHAR, RawTrades.LastUpdate, 103) AS LastUpdate, 
    Sequences.ActualStartMonth,
    Sequences.ActualEndMonth,
    RawTrades.Price
    INTO #seasonal
    FROM RawTrades
    INNER JOIN Sequences ON RawTrades.FirstSequenceItemId = Sequences.SequenceItemId AND RawTrades.FirstSequenceId = Sequences.SequenceId
    WHERE RawTrades.FirstSequenceID IN (SELECT SeasonalSequenceId FROM Instruments WHERE InstrumentGroupId = @InstrumentGroupId) 
    AND Action <> 'Remove' 
    AND LastUpdate >= @StartDate
    AND LastUpdate < @EndDate
    AND RawTrades.Price > 20
    ORDER BY RawTrades.FirstSequenceItemID, RawTrades.LastUpdate DESC

    ---- BEFORE WE INSERT RECORDS MAKE SURE WE DON'T ADD DUPLICATES ----
    DELETE FROM LiveCurveWeightings
    WHERE InstrumentGroupId = @InstrumentGroupId
    AND CalculationDate = @EndDate

    ---- CALCULATE AND INSERT THE WEIGHTINGS ----
    INSERT INTO LiveCurveWeightings (InstrumentGroupId, CalculationDate, TradedMonth, QuarterlyWeighting, SeasonalWeighting)
    SELECT
    @InstrumentGroupId,
    @EndDate,
    #monthly.ActualStartMonth,
    AVG(COALESCE(#monthly.Price / #quarterly.Price,1)) AS QuarterlyWeighting,
    AVG(COALESCE(#monthly.Price / #seasonal.Price,1)) AS SeasonalWeighting
    FROM #monthly
    LEFT JOIN #quarterly 
        ON #monthly.ActualStartMonth >= #quarterly.ActualStartMonth 
        AND #monthly.ActualEndMonth <= #quarterly.ActualEndMonth 
        AND #quarterly.[Row] = 1
        AND #monthly.LastUpdate = #quarterly.LastUpdate
    LEFT JOIN #seasonal 
        ON #monthly.ActualStartMonth >= #seasonal.ActualStartMonth
        AND #monthly.ActualEndMonth <= #seasonal.ActualEndMonth 
        AND #seasonal.[Row] = 1
        AND #monthly.LastUpdate = #seasonal.LastUpdate
    WHERE #monthly.[Row] = 1
    GROUP BY #monthly.ActualStartMonth

    DROP TABLE #monthly
    DROP TABLE #quarterly
    DROP TABLE #seasonal

END
4

1 回答 1

0

我认为这个问题可能是由于您的 EF 跟踪图变得太大。如果您在每次执行操作时都在带有跟踪图的批处理操作中重新使用上下文,则它需要枚举该图。有了几百个项目,这不是问题,但是当您进入 000 年代时,它可能会成为一个大问题。在这里查看我关于此的文章,看看您是否认为它与问题相符。

如果您查看下面的插入操作图表,您可以看到大约 1000 个插入(当跟踪打开时)开始在执行时间中急剧上升。(还要注意轴上的对数刻度)

在此处输入图像描述

于 2012-07-20T23:13:46.237 回答