c# - Stored Procedure Run from SMSS is Lightning Fast, ADO.NET takes 20x slower
问题描述
Hopefully someone could shine a little bit of light on this for me. I've been trying to use Slow in the Application, Fast in the SMSS to try and troubleshoot my problem for the last day or so with no avail.
I have a stored procedure that's attempting to retrieve values out of a custom built view :
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ARITHABORT ON
GO
CREATE PROCEDURE [dbo].[GetProcesses]
@SessionId INT,
@LastUpdate DATETIME2(7)
AS
DECLARE @MySessionId INT
SET @MySessionId = @SessionId
DECLARE @MyLastUpdate DATETIME2(7)
SET @MyLastUpdate = @LastUpdate
BEGIN
SELECT *
FROM [GetProcessesView]
WHERE SessionId = @MySessionId
AND (StartTime >= @MyLastUpdate OR
LastModified >= @MyLastUpdate OR
ProcessStepViewLastModified >= @MyLastUpdate)
END
GO
I can run this from SQL Server Management Studio using the same parameters my application provides and this completes in 1-2 seconds. However, when I attempt to run it from my Data Access layer, it takes over 40. The dataset is relatively small (maybe 5 mbs or so).
using (var connection = new SqlConnection(GetConnectionString()))
{
var dataSet = new DataSet("ProcessUpdates");
SqlCommand command = new SqlCommand("GetProcesses", connection);
command.Parameters.AddWithValue("@SessionId", sessionId);
command.Parameters.AddWithValue("@LastUpdate", lastUpdate);
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dataAdapter = new SqlDataAdapter()
{
SelectCommand = command
};
dataAdapter.Fill(dataSet);
}
Here is a list of my view: Queries in this view in SQL Management Studio are incredibly fast.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[GetProcessesView]
as
select P.Id as ProcessViewId, P.[Name], P.StartTime, P.CompletedTime, P.IsFailed, P.UnitId, P.ProcedureId, P.SessionId, P.LastModified, P.AutomationId,
PS.Id as ProcessStepViewId, PS.[Status], PS.ProcessId, PS.StepNum, PS.Description, PS.LastModified as ProcessStepViewLastModified,
[U].Id as UnitViewId, [U].Number, [U].[Grouping], [U].EnumValue,
PR.Id as ProcedureViewId, PR.Title as ProcedureViewTitle, PR.Steps, PR.SystemId as ProcedureViewSystemId, PR.VisionId, PR.[Grouping] as ProcedureViewGrouping,
A.Id as AutomationViewId, A.Title as AutomationViewTitle, A.UserControl, A.InProgressUserControl, A.SystemId as AutomationViewSystemId,
SY.Id as SystemViewId, SY.FullName as SystemViewFullName, SY.Acronym as SystemViewAcronym,
S.Id as SystemTwoViewId, S.FullName as SystemTwoViewFullName, S.Acronym as SystemTwoViewAcronym,
P.[UID]
from [Process] P
left join [ProcessSteps] PS on P.Id = PS.ProcessId
left join [Unit] U on P.UnitId = U.Id
left join [Procedure] PR on P.ProcedureId = PR.Id
left join [Automation] A on P.AutomationId = A.Id
left join [PlantSystem] S on PR.SystemId = S.Id
left join [PlantSystem] SY on A.SystemId = SY.Id
GO
解决方案
推荐阅读
- wordpress - 使用 AWS SES、Cloudflare 和 Wordpress 设置电子邮件的困难
- javascript - D3 强制布局颜色和缩放
- mongodb - 是否可以在 MongoDB 中平均嵌套值?
- arrays - VBA:将多行字符串拆分为带有字典的列表
- android - Cordova InAppBrowser - 打开系统浏览器后未调用事件回调(“_system”)
- perl - 500 内部服务器错误 Perl CGI
- apache-spark - Spark查询Geode数据
- c# - 从 HttpResponseMessage 读取字符串
- julia - 在 Julia 1.0 中,如何设置一个只有一个键值对的命名元组?
- python - 如何根据特定位置的类似项目排列列表