我有以下代码重复运行存储过程。当我从字面上运行 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
作为输出参数返回。我不能使用 DataContext 来执行带有输出参数的命令,所以我不得不将 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
并使用了 string EXEC Session_Aggregate ... WITH RECOMPILE
。但在这种情况下,我不断收到@session
未定义 out 参数的错误。无论如何,查询现在几乎没有运行,即使 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);
}