SQL中SQL SQL速度慢C#

时间:2016-06-14 18:46:33

标签: c# sql

我有以下SQL SP:

ALTER PROCEDURE mysp

@HCPID varchar(10),
@From datetime,
@To Datetime,
@LoadType int
with recompile
AS
BEGIN
set arithabort on;
set nocount on;

begin
Select appt.*,
t1.StoreID,
t2.AppointmentDate,
t2.DoctorID,
t2.LoggedInUserID,
t2.Serial,
t2.TimeIn,
t2.TimeOut,
t3.FName,
t3.LName,
t3.AR_FName,
t3.AR_LName,
t3.MName,
t3.MotherName,
t3.AR_MName as Ar_MName,
t3.AR_MotherName as Ar_MotherName,
t4.ID as VisitID,
t5.Flag,
t5.FlagDate,
convert(date,'1900-1-1') as AdmissionDate,

convert(date,'1900-1-1')as DischargeDate

from cms.Appointments as appt
left join CMs.t1 on appt.ID = t1 .AppointmentID
left join CMs.t2 on appt.ID = t2.AppointmentID
left join CMs.t4 on t4.AppointmentID = appt.ID
left join CMs.t5 on t5.


AppointmentID = appt.ID

    inner join CMs.t3 on t3.ID = appt.PatientID



    where  appt.UserID = @HCPID
     AND  ( CONVERT(varchar(10), StartTime, 112) >=  CONVERT(varchar(10), @From, 112) OR (IsNull(@From, 0) = 0))
  AND  ( CONVERT(varchar(10), StartTime, 112)  <=  CONVERT(varchar(10), @To, 112) OR (IsNull(@To, 0) = 0))

    and ShowInSchedule = 1
end

如果我从SQL运行SP需要1-2秒,如果我使用相同的参数值从C#调用它,它需要大约10秒,有时我得到以下消息之一。 First Error Second Error

我从C#调用它如下:

string commande = "mysp";
CommandType commandType = CommandType.StoredProcedure;
DbHelper.CommandTimeOut = 0;
DbParameter[] dbParameter = new DbParameter[4];
dbParameter[0] = DbHelper.CreateParameter(Settings, "@HCPID", ent.UserID);
dbParameter[1] = DbHelper.CreateParameter(Settings, "@From", ent.StartTime);
dbParameter[2] = DbHelper.CreateParameter(Settings, "@To", ent.EndTime);
dbParameter[3] = DbHelper.CreateParameter(Settings, "@LoadType", LoadType);

list = DbHelper.ExecuteList32<AppointmentsViewEnt_1>(Settings, commandType, commande, dbParameter);

更新

请注意,如果我在另一个客户端上使用相同代码的相同应用程序,它运行正常,我从未遇到过相同的错误。此错误仅在一个客户端发生。

更新

SQL配置文件数据:

  • CPU 2479读取31791写入0持续时间3578 StartTime
    14/06/2016 22:33:30 EndTime 14/06/2016 22:33:34

2 个答案:

答案 0 :(得分:0)

谢谢大家的帮助。 经过几个小时的搜索和故障排除,这是SQL 2008R2损坏了!我安装了SQL 2014,一切顺利,内存从10 GB减少到5 GB!并且大多数时候CPU负载小于20%(虽然在某些情况下它加载到70%但很少见,我会再次检查我的代码)

答案 1 :(得分:0)

要为将来的搜索者添加更多详细信息,这个问题可能会从所谓的参数嗅探中发生,并且可以通过将with recompile添加到存储过程来解决,例如:

create procedure mysp

@par varchar(100
with recompile
as 

-- def of SP goes here