C#存储过程调用,参数嗅探/优化问题的大幅减速?

时间:2012-08-07 22:02:37

标签: c# sql stored-procedures parameter-sniffing

我有以下代码重复运行存储过程。当我按字面意思运行SQL语句时,它工作得很好,所以我创建了一个封装我正在做的事情的存储过程。

foreach (string worker in workers)
{
    _gzClasses.ExecuteCommand("EXEC dbo.Session_Aggregate @workerId = {0}, @timeThresh = {1}", worker, SecondThreshold);
    Console.WriteLine("Inserted sessions for {0}", worker);
}

然后,我想知道每个调用生成了多少行,所以我稍微更改了SP以返回@@rowcount作为输出参数。我can't use the DataContext to execute commands with output parameters,所以我不得不将for循环中的上述代码更改为以下内容:

using (var cn = new SqlConnection(CnStr))
{
    cn.Open();
    using (var cmd = new SqlCommand("Session_Aggregate", 
        cn) {CommandTimeout = 300})
    {                        
        cmd.CommandType = CommandType.StoredProcedure;                        

        cmd.Parameters.AddWithValue("@workerId", worker);                        
        cmd.Parameters.AddWithValue("@timeThresh", SecondThreshold);                        

        SqlParameter sessions = cmd.Parameters.Add("@sessions", SqlDbType.Int);
        sessions.Direction = ParameterDirection.Output;

        cmd.ExecuteNonQuery();

        Console.WriteLine("Inserted {1} sessions for {0}", worker, sessions.Value);
    }
}

这样可行,但它运行速度比其他查询慢很多。我认为可能是参数嗅探的情况,因此我将其更改为CommandType.Text并使用字符串EXEC Session_Aggregate ... WITH RECOMPILE。但在这种情况下,我不断收到out参数@session未定义的错误。在任何情况下,即使SQL命令在<中运行,查询也几乎不会立即运行。在SSMS中1秒。

这是存储过程,以防任何人可以帮助弄清楚发生了什么,或者可以找到加快速度的方法。我还会指出如何正确分析这里发生的事情。使用CommandType.StoredProcedure我甚至看不到VS发送给SQL的实际命令。

PROCEDURE [dbo].[Session_Aggregate] 
    -- Add the parameters for the stored procedure here
    @workerId varchar(64) = 0, 
    @timeThresh dateTime = '13 July 2007 11:27:46'
    @sessions INT OUTPUT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO e_activeSessions
    SELECT *
    FROM (
        SELECT workerId, startTime, COUNT(*) as totalTasks, MAX(timeInSession) as totalTime, 
        MIN(dwellTime) as minDwell, MAX(dwellTime) as maxDwell, AVG(dwellTime) as avgDwell, STDEV(dwellTime) as stdevDwell, 
        SUM(CAST(wrong80 as INT)) + SUM(CAST(correct80 as INT)) as total80, SUM(CAST(correct80 as INT)) as correct80, 
        SUM(CAST(correct80 as FLOAT)) / NULLIF(SUM(CAST(wrong80 as INT)) + SUM(CAST(correct80 as INT)), 0 ) as percent80 
        FROM (
            SELECT *, (SELECT MAX(timeStamp)
                FROM workerLog w where dwellTime is null AND timeInSession = 0 AND workerId = @workerId AND w.timeStamp <= workerLog.timeStamp
                    AND w.timeStamp >= @timeThresh) as startTime
            FROM workerLog where workerId = @workerId) t 
    GROUP BY startTime, workerId) f 
    WHERE startTime is NOT NULL AND f.totalTasks > 1 AND totalTime > 0;

    SET @sessions = @@ROWCOUNT;
END

编辑:无论原始查询的执行计划如何,都通过创建临时表来大大加快。我认为SQL会通过分析查询来完成此操作,但我可能是错误。 此外,我发现了OPTIMIZE FOR UNKNOWN提示,在新版本的SQL Server中,减少了参数嗅探对于执行计划适用于大小不同的数据大小的影响。

PROCEDURE [dbo].[Session_Aggregate] 
    -- Add the parameters for the stored procedure here
    @workerId varchar(64) = 0, 
    @timeThresh dateTime = '13 July 2007 11:27:46',
    @sessions INT OUTPUT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here

    CREATE TABLE #startTimes
    (
        startTime DATETIME
    );

    CREATE INDEX Idx_startTime ON #startTimes(startTime);

    INSERT INTO #startTimes
    SELECT timeStamp FROM workerLog 
    WHERE dwellTime is null AND timeInSession = 0 
    AND workerId = @workerId AND timeStamp >= @timeThresh;

    INSERT INTO e_activeSessions
    SELECT *
    FROM (
        SELECT workerId, startTime, COUNT(*) as totalTasks, MAX(timeInSession) as totalTime, 
        MIN(dwellTime) as minDwell, MAX(dwellTime) as maxDwell, AVG(dwellTime) as avgDwell, STDEV(dwellTime) as stdevDwell, 
        SUM(CAST(wrong80 as INT)) + SUM(CAST(correct80 as INT)) as total80, SUM(CAST(correct80 as INT)) as correct80, 
        SUM(CAST(correct80 as FLOAT)) / NULLIF(SUM(CAST(wrong80 as INT)) + SUM(CAST(correct80 as INT)), 0 ) as percent80 
        FROM (
            SELECT *, (SELECT MAX(startTime) FROM #startTimes where startTime <= workerLog.timeStamp) as startTime
            FROM workerLog where workerId = @workerId) t 
    GROUP BY startTime, workerId) f 
    WHERE startTime is NOT NULL AND f.totalTasks > 1 AND totalTime > 0
    OPTION (OPTIMIZE FOR UNKNOWN);

    SET @sessions = @@ROWCOUNT;     
END;

其他简化:将SP拖到您的DBML文件中,您可以执行以下操作:

foreach (string worker in workers)
{
    int? rows = 0;
    _gzClasses.Session_Aggregate(worker, SecondThreshold, ref rows);

    Console.WriteLine("Inserted {1} sessions for {0}", worker, rows);
}

1 个答案:

答案 0 :(得分:1)

启动SQLServerProfiler,这可以为您提供单个查询与现在运行方式之间的区别。

http://www.techrepublic.com/article/step-by-step-an-introduction-to-sql-server-profiler/5054787

但更重要的是,您应该查看可以通过查询磁贴在SSMS中打开的查询执行计划,并选择show execution plan。

http://www.mssqltips.com/sqlservertip/1856/sql-server-query-execution-plans-in-sql-server-management-studio/

如果你是SSMS的新手,我可能会在我提供的内容之上阅读几篇文章,但查询执行计划将真正向您展示查询滞后的地方。 (基本的经验法则是你不希望发生全表扫描,你希望它进行搜索,这意味着你希望它在索引和/或主键上搜索)我不是dba但是这是调试查询时可能需要的路径。

我不太确定这是你查询后的查询,因为它看起来非常简单。它可能与您调用它的次数有关。您可能想要找到一种方法将所有工作人员数据传递到查询中,这样您只需运行查询一次,而不是运行它.surms.count次...... ...... HTH