SQL Server存储过程执行时间不好我想改善执行时间

时间:2017-09-14 09:52:00

标签: sql sql-server database-performance

这是我的存储过程 enter image description here

USE [Alarm_History]
GO
/****** Object:  StoredProcedure [dbo].[Alarm_Summary_R_PRM1]    Script Date: 09/14/2017 14:47:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*
 Alarm_Summary_R_PRM1 '2015-09-15 00:02:59','2015-10-15 15:35:40'
*/
ALTER procedure [dbo].[Alarm_Summary_R_PRM1]
(
@SDate datetime,
@EDate datetime
)



as
begin

Create table #Alarm_Summary_Report
(
Cid int  NOT NULL PRIMARY KEY ,
kpiparameter_label varchar(Max),
avgonemonthalarmkpi float,
standardkpiparameter_label varchar(Max)
)


create table #Alarm_Month
(
SourceName nvarchar(400),
EventTimeStamp DAteTime,
active int,
acked int,
AlarmClass nvarchar(400)
)

create table #Top_10
(
SourceName nvarchar(400),
Value int
)

create table #Alarm_Avtive1
(
SourceName nvarchar(400),
Count1 int
)

create table #Alarm_Avtive0
(
SourceName nvarchar(400),
Count2 int
)


create table #Alarm_Active_Final
(
SourceName nvarchar(400),
Count3 int
)




insert into #Alarm_Summary_Report values(1,'AlarmPerday',0,'150 AlarmPerDay')
insert into #Alarm_Summary_Report values(2,'AlarmPerHour',0,'5average')
insert into #Alarm_Summary_Report values(3,'Percentage contribution 10 frequent alarm load',0,'1%  to 5% max action plans to address deficiencies')
insert into #Alarm_Summary_Report values(4,'Quantity of chattering and fleeting alarms',0,'0x action plans to correct any that occur')
insert into #Alarm_Summary_Report values(5,'unauthorised alarm bypassed',0,'0x alarm attribute changes outsideapproval framework ')
insert into #Alarm_Summary_Report values(6,'unauthorised alarm suppression',0,'0x alarm suppress changes outsideapproval framework ')
insert into #Alarm_Summary_Report values(7,'UnAuthorised Alarm Attribute Changes ',0,'150 AlarmPerDay')
insert into #Alarm_Summary_Report values(8,'System PLC_DCS Alarm and UPS Alarm ',0,'')
insert into #Alarm_Summary_Report values(9,'No of Unacknowledged Alarm ',0,'')
insert into #Alarm_Summary_Report values(10,'Stale Alarm',0,'Less than 5% max on any day with action plans to address ')


Insert into #Alarm_Month
SELECT sourcename
,eventtimestamp 
,active
,acked
,AlarmClass
FROM ConditionEvent with (Nolock)
WHERE active in (1)  
--and acked in (0,1)
and ConditionEvent.EventTimeStamp between @SDate and @EDate
--CREATE UNIQUE CLUSTERED INDEX IX_1 on #Alarm_Month (EventTimeStamp)

Declare @Avg_Alarm_Month int
Declare  @Total_day int

--select @Avg_Alarm_Month = ISNULL(Count(sourcename),0)
--from #Alarm_Month
--where (active = 1 )  and AlarmClass='PROCESS'
--group by  Convert(varchar(10),eventtimestamp,103) 
--order by  Convert(varchar(10),eventtimestamp,103)


--select ISNULL(count(sourcename),0)
--from #Alarm_Month
--where (active = 1 )  and AlarmClass='PROCESS'
--group by  Convert(varchar(10),eventtimestamp,103) 
--order by  Convert(varchar(10),eventtimestamp,103)


SELECT @Avg_Alarm_Month=
count(SourceName)
FROM [Alarm_History].[dbo].[ConditionEvent]
where eventtimestamp between @SDate and @EDate and active = '1' and AlarmClass='PROCESS'
select @Total_day = DATEDIFF(Day, @sdate, @edate) 

Insert into #Top_10
select Top 10 SourceName ,Count (SourceName) as  Value 
from ConditionEvent 
where active in (1)
and ConditionEvent.EventTimeStamp between @SDate and @EDate
group by SourceName order by value desc 

Declare @summation int
select @summation= sum(Value) from #Top_10 

Declare @unauthorisebypass int
select @unauthorisebypass  = count( distinct SourceName)  
from ConditionEvent
where Disabled='1' and ConditionEvent.EventTimeStamp between @SDate and @EDate


Declare @supress int
select @supress  = count( distinct SourceName)  
from ConditionEvent 
where Suppressed='1' and ConditionEvent.EventTimeStamp between @SDate and @EDate



Declare @systemplc_dcs_upl int
select @systemplc_dcs_upl = ISNULL(Count(sourcename),0)
from #Alarm_Month 
where (active = 1 )  and AlarmClass='SYSTEM'
group by  Convert(varchar(10),eventtimestamp,103) 
order by  Convert(varchar(10),eventtimestamp,103)


Insert into #Alarm_Avtive1
select SourceName ,Count (SourceName) as  Count1 
from ConditionEvent  
where active in (1)
and acked in (0)
and ConditionEvent.EventTimeStamp between @SDate and @EDate
group by SourceName order by Count1  

Insert into #Alarm_Avtive0
select SourceName ,Count (SourceName) as  Count2 
from ConditionEvent  
where active in (0)
and acked in (0)
and ConditionEvent.EventTimeStamp between @SDate and @EDate
group by SourceName order by Count2  


insert into #Alarm_Active_Final
SELECT *FROM #Alarm_Avtive1 WHERE SourceName  NOT IN (SELECT SourceName  FROM #Alarm_Avtive0  )
union  SELECT *FROM #Alarm_Avtive0 WHERE SourceName  NOT IN (SELECT SourceName  FROM #Alarm_Avtive1  )


Declare @totalcount int
Declare @totalalarmname int
select @totalcount=Count(Count3)from #Alarm_Active_Final
select @totalalarmname=Count(SourceName)from #Alarm_Active_Final




update #Alarm_Summary_Report set avgonemonthalarmkpi = isnull((@Avg_Alarm_Month/@Total_day),0) where Cid = 1
update #Alarm_Summary_Report set avgonemonthalarmkpi = isnull((@Avg_Alarm_Month/@Total_day)/24,0) where Cid = 2
update #Alarm_Summary_Report set avgonemonthalarmkpi= round((((cast(@summation As float))/(cast(@Avg_Alarm_Month As float)))*100),2) where Cid =3
update #Alarm_Summary_Report set avgonemonthalarmkpi =isnull(@unauthorisebypass,0)  where Cid =5
update #Alarm_Summary_Report set avgonemonthalarmkpi =isnull(@supress,0)  where Cid =6
update #Alarm_Summary_Report set avgonemonthalarmkpi= isnull(@systemplc_dcs_upl,0) where Cid = 8
update #Alarm_Summary_Report set avgonemonthalarmkpi =@totalcount  where Cid =9
update #Alarm_Summary_Report set avgonemonthalarmkpi =@totalalarmname  where Cid =10

SELECT * FROM #Alarm_Summary_Report
--SELECT * FROM #Top_10

--drop table #Top_10 
drop table #Alarm_Month
drop table #Alarm_Avtive1
drop table #Alarm_Avtive0
drop table #Alarm_Active_Final


end

1 个答案:

答案 0 :(得分:0)

消除使用tempdb #tables而改为使用Table数据类型:

创建@MyTable TABLE (colname coldatatype)等

相关问题