SQL执行时间减慢,代码优先

时间:2012-05-29 14:13:21

标签: c# code-first dbcontext

我在这里有点奇怪。我在运行批处理的控制台应用程序中使用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

1 个答案:

答案 0 :(得分:0)

我认为此问题可能是由于您的EF跟踪图变得过大。如果在每次执行操作时在批处理操作中使用跟踪图重新使用上下文,则需要枚举图。有几个hundread项目这不是一个问题,但当你进入000s它可能会成为一个大问题。看看我在这篇here上的文章,看看你认为它是否符合这个问题。

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

enter image description here