SQL Server基線算法(同比和環比)

基線介紹

基線爲歷史數據統計而成的數據,具備參考價值,並利用基線數據與當前值的對比,經過必定的報警機制,造成實時監控架構。SQL Server計數器採用同比和環比兩種方式。算法

clip_image001

同比:能夠計算將來一週的基線數據,取近八週同一天同一時刻的均值。

  1 /****************************** 功能描述:<同比基線算法>
  2 *   建立者:<HuangCH〉
  3 *   建立日期:<2014-11-05>
  4 *   備註說明:<天天執行一次>
  5 ##########
  6 Change Log
  7 ##########
  8 Date                 Changer             Description
  9 --------------------------------------------------
 10 <2014-11-05>        <HuangCH>            <新建>
 11 說明:
 12 一、同比算法取近八週數據能夠統計一週後的數據
 13 二、考慮當天前一週0點以前的數據已經歸檔SQLPerfDataStat_Histroy.dbo.CounterData_OneMinute_Bak,所以要計算前一天以前的數據。
 14 
 15 二、計數器新建要超過一週才能開始計算
 16 
 17 三、開始時間當天0點與結束時間不能超過一週--CONVERT(VARCHAR(26),DATEADD(WEEK,1,@LastCBaseEnd),23)<=@LastOneMinute
 18 
 19 --------------------------------------------------
 20 ***************************/
 21 ALTER PROC [dbo].[spb_CBaseCounterData]
 22 AS
 23 SET NOCOUNT ON
 24 DECLARE @LastOneMinute DATETIME 
 25 DECLARE @LastCBaseEnd DATETIME --計算一週以後的數據
 26 DECLARE @RunCBaseEnd DATETIME 
 27 DECLARE @RunCBaseEtart DATETIME
 28 DECLARE @CounterID INT
 29 DECLARE @MachineName VARCHAR(128)
 30 
 31 --若時間未超過一週,則取當前時間
 32 UPDATE SQLPerfDataStat.dbo.CounterDetails_Dts
 33 SET LastCBaseEnd=DATEADD(D,-1,LastOneMinute)
 34 WHERE DATEADD(WEEK,-1,DATEADD(D,-1,LastOneMinute))<CreateTime
 35 
 36 DECLARE CBaseCur CURSOR FOR
 37 --計數器新建超過一週;
 38 --只能計算當天0點以前的數據:CONVERT(DATETIME,CONVERT(VARCHAR(16),LastOneMinute,23))
 39 --計算當前的時間的前天以前
 40 SELECT DISTINCT MachineName,DATEADD(D,-1,LastOneMinute) LastOneMinute,CONVERT(DATETIME,CONVERT(VARCHAR(16),LastCBaseEnd,120))  LastCBaseEnd
 41 FROM SQLPerfDataStat.dbo.CounterDetails_Dts WITH(NOLOCK)
 42 WHERE  DATEDIFF(MI,CONVERT(DATETIME,CONVERT(VARCHAR(16),LastCBaseEnd,120)),DATEADD(D,-1,LastOneMinute))>=1 and DATEADD(WEEK,-1,DATEADD(D,-1,LastOneMinute))>=CreateTime
 43 
 44 OPEN CBaseCur
 45 
 46 FETCH NEXT FROM CBaseCur INTO @MachineName,@LastOneMinute,@LastCBaseEnd
 47 
 48 WHILE @@FETCH_STATUS=0
 49 BEGIN
 50 
 51 IF CONVERT(VARCHAR(26),DATEADD(WEEK,1,@LastCBaseEnd),23)<=@LastOneMinute--超過一週未收集則時間爲上次收集時間推遲一週
 52 BEGIN
 53   SET  @RunCBaseEnd=CONVERT(VARCHAR(26),DATEADD(WEEK,1,@LastCBaseEnd),23)
 54   SET  @RunCBaseEtart=@LastCBaseEnd
 55 END
 56 ELSE 
 57 BEGIN
 58   SET  @RunCBaseEnd=@LastOneMinute
 59   SET  @RunCBaseEtart=@LastCBaseEnd
 60 END
 61 
 62 UPDATE SQLPerfDataStat.dbo.CounterDetails_Dts
 63 SET LastCBaseEnd=@RunCBaseEnd
 64 WHERE MachineName=@MachineName
 65 
 66 BEGIN TRY
 67 
 68 INSERT INTO dbo.CBaseCounterData_OneMinute(CounterID, CounterDateTime, CounterValue,MaxCounterValue)
 69 SELECT CounterID 
 70         ,DATEADD(MI,-DATEDIFF(MI,CounterDateTime,Dateadd(week,1,@RunCBaseEnd))%(7*24*60),Dateadd(week,1,@RunCBaseEnd)) CounterDateTime
 71         ,AVG(CounterValue) CounterValue,MAX(CounterValue) MaxCounterValue
 72 FROM (
 73 SELECT CounterID,CounterDateTime,CounterValue
 74 FROM SQLPerfDataStat_Histroy.dbo.CounterData_OneMinute_Bak a (NOLOCK)
 75 WHERE 
 76 (
 77 (CounterDateTime>=Dateadd(week,-7,@RunCBaseEtart) 
 78 AND CounterDateTime<Dateadd(week,-7,@RunCBaseEnd))
 79 OR 
 80 (CounterDateTime>=Dateadd(week,-6,@RunCBaseEtart) 
 81 AND CounterDateTime<Dateadd(week,-6,@RunCBaseEnd))
 82 OR 
 83 (CounterDateTime>=Dateadd(week,-5,@RunCBaseEtart) 
 84 AND CounterDateTime<Dateadd(week,-5,@RunCBaseEnd))
 85 OR         
 86 (CounterDateTime>=Dateadd(week,-4,@RunCBaseEtart) 
 87 AND CounterDateTime<Dateadd(week,-4,@RunCBaseEnd))
 88 OR 
 89 (CounterDateTime>=Dateadd(week,-3,@RunCBaseEtart) --取近四周
 90 AND CounterDateTime<Dateadd(week,-3,@RunCBaseEnd))
 91 OR 
 92 (CounterDateTime>=Dateadd(week,-2,@RunCBaseEtart) 
 93 AND CounterDateTime<Dateadd(week,-2,@RunCBaseEnd))
 94 OR 
 95 (CounterDateTime>=Dateadd(week,-1,@RunCBaseEtart) 
 96 AND CounterDateTime<Dateadd(week,-1,@RunCBaseEnd))        
 97 ) AND  EXISTS (SELECT TOP 1 1 FROM CounterDetails_Collect WITH(NOLOCK) WHERE CounterID = a.CounterID AND MachineName=@MachineName)
 98 UNION ALL
 99 SELECT CounterID,CounterDateTime,CounterValue 
100 FROM dbo.CounterData_OneMinute a (NOLOCK)
101 WHERE (CounterDateTime>=@RunCBaseEtart 
102         AND CounterDateTime<@RunCBaseEnd) 
103 AND  EXISTS (SELECT TOP 1 1 FROM CounterDetails_Collect WITH(NOLOCK) WHERE CounterID = a.CounterID AND MachineName=@MachineName)
104 ) AA
105 GROUP BY CounterID,DATEADD(MI,-DATEDIFF(MI,CounterDateTime,Dateadd(week,1,@RunCBaseEnd))%(7*24*60),Dateadd(week,1,@RunCBaseEnd))
106 
107 END TRY
108 BEGIN CATCH
109   IF @@TRANCOUNT >0
110   BEGIN
111     Rollback;
112   END;
113   THROW 
114 END CATCH
115 
116 FETCH NEXT FROM CBaseCur INTO @MachineName,@LastOneMinute,@RunCBaseEnd
117 END
118 CLOSE CBaseCur
119 DEALLOCATE  CBaseCur
spb_CBaseCounterData

同比能夠消除因爲業務變更帶來的波動,具備報警對比價值。下圖紅線同比數據,表示歷史上該時段存在異常升高。數據庫

clip_image003

環比:能夠計算將來一天的基線數據,取近兩週天天同一時刻的均值。

  1 /****************************** 功能描述:<環比基線算法>
  2 *   建立者:<HuangCH〉
  3 *   建立日期:<2014-11-05>
  4 *   備註說明:<每小時執行一次>
  5 ##########
  6 Change Log
  7 ##########
  8 Date                 Changer             Description
  9 --------------------------------------------------
 10 <2014-11-05>        <HuangCH>            <新建>
 11 說明:
 12 一、環比算法能夠統計一天後的數據
 13 
 14 二、若是開始時間與當前時間對比超過一天,則只取開始時間當天的數據進行計算--CONVERT(VARCHAR(26),DATEADD(D,1,@LastRBaseEnd),23)< = @LastOneMinute
 15 
 16 三、若是時間分別爲兩天的0點,則須要單獨處理當天0點的數據--DATEDIFF(MI,@RunRBaseStart,@RunRBaseEnd)=1440
 17 
 18 四、環比算法,分離工做日和週末,本別計算,統計兩週內的數據--DATEPART(WEEKDAY,@RunRBaseStart) IN(2,3,4,5,6)--非週末
 19 
 20 --------------------------------------------------
 21 ***************************/
 22 ALTER PROC [dbo].[spb_RBaseCounterData]
 23 AS
 24 SET NOCOUNT ON
 25 SET DATEFIRST 7--因爲須要考慮工做日與週末,所以須要確認默認設置是以週日開始
 26 DECLARE @LastOneMinute DATETIME 
 27 DECLARE @LastRBaseEnd DATETIME --計算一週以後的數據
 28 DECLARE @RunRBaseEnd DATETIME 
 29 DECLARE @RunRBaseStart DATETIME
 30 DECLARE @SqlCmd   VARCHAR(MAX)
 31 DECLARE @WHERE VARCHAR(2000) 
 32 DECLARE @GROUPBY VARCHAR(MAX)
 33 DECLARE @CYLE INT
 34 DECLARE @MachineName VARCHAR(128)
 35 DECLARE @ERRORMSG VARCHAR(MAX)
 36 SET @ERRORMSG=''
 37 
 38 --更新未超過一週的數據
 39 UPDATE SQLPerfDataStat.dbo.CounterDetails_Dts
 40 SET LastRBaseEnd=CONVERT(DATETIME,CONVERT(VARCHAR(16),GETDATE(),120))
 41 WHERE DATEADD(WEEK,-1,LastOneMinute)<CreateTime
 42 
 43 --取最後計算小於當前統計時間的數據
 44 DECLARE RBaseCur CURSOR FOR
 45 SELECT MachineName,LastOneMinute,CONVERT(DATETIME,CONVERT(VARCHAR(16),LastRBaseEnd,120))  LastRBaseEnd
 46 FROM SQLPerfDataStat.dbo.CounterDetails_Dts WITH(NOLOCK)
 47 WHERE  DATEDIFF(MI,LastRBaseEnd,LastOneMinute)>1  AND DATEADD(WEEK,-1,LastOneMinute)>=CreateTime--不超過新建時間一週,則不處理
 48 
 49 OPEN RBaseCur
 50 
 51 FETCH NEXT FROM RBaseCur INTO @MachineName,@LastOneMinute,@LastRBaseEnd
 52 
 53 WHILE @@FETCH_STATUS=0
 54 BEGIN
 55 
 56 IF CONVERT(VARCHAR(26),DATEADD(D,1,@LastRBaseEnd),23)< = @LastOneMinute--若是次日0點小於當前時間;即不能跨天處理
 57 BEGIN
 58   SET  @RunRBaseEnd=CONVERT(VARCHAR(26),DATEADD(D,1,@LastRBaseEnd),23)
 59   SET  @RunRBaseStart=@LastRBaseEnd
 60 END
 61 ELSE 
 62 BEGIN--在同一天
 63   SET  @RunRBaseEnd=@LastOneMinute
 64   SET  @RunRBaseStart=@LastRBaseEnd
 65 END
 66 
 67 IF DATEPART(WEEKDAY,@RunRBaseStart) IN(2,3,4,5,6)--非週末
 68 BEGIN  
 69   SET @WHERE='WHERE ((CounterDateTime>'+CASE WHEN CONVERT(VARCHAR(24),@RunRBaseStart,120)=CONVERT(VARCHAR(24),@RunRBaseStart,23)+' 00:00:00' THEN ''ELSE '=' END +''''+CONVERT(VARCHAR(24),@RunRBaseStart,120)+''' AND CounterDateTime<'''+CONVERT(VARCHAR(24),@RunRBaseEnd,120)+''')'
 70   SET @CYLE =1
 71   WHILE(@CYLE<14)
 72     BEGIN
 73       IF DATEPART(WEEKDAY,DATEADD(D,-@CYLE,@RunRBaseStart)) IN(2,3,4,5,6)
 74       BEGIN 
 75         SET @WHERE=@WHERE+CHAR(10)+'OR (CounterDateTime>'++CASE WHEN CONVERT(VARCHAR(24),@RunRBaseStart,120)=CONVERT(VARCHAR(24),@RunRBaseStart,23)+' 00:00:00' THEN ''ELSE '=' END ++''''+CONVERT(VARCHAR(24),DATEADD(D,-@CYLE,@RunRBaseStart),120)+''' AND CounterDateTime<'''+CONVERT(VARCHAR(24),DATEADD(D,-@CYLE,@RunRBaseEnd),120)+''')'        
 76       END
 77       SET @CYLE = @CYLE + 1
 78     END
 79   SET @GROUPBY='DATEADD(MI,-DATEDIFF(MI,CounterDateTime,'''+CONVERT(VARCHAR(24),@RunRBaseEnd,120)+''')%(24*60),'''+CASE WHEN DATEPART(WEEKDAY,@RunRBaseStart) =6 THEN CONVERT(VARCHAR(24),DATEADD(D,3,@RunRBaseEnd),120) ELSE CONVERT(VARCHAR(24),DATEADD(D,1,@RunRBaseEnd),120) END+''')'--週五要計算週一的數據
 80 END
 81 ELSE--週末數據,取近兩週週末數據庫
 82 BEGIN 
 83   SET @WHERE='WHERE ((CounterDateTime>'+CASE WHEN CONVERT(VARCHAR(24),@RunRBaseStart,120)=CONVERT(VARCHAR(24),@RunRBaseStart,23)+' 00:00:00' THEN ''ELSE '=' END +''''+CONVERT(VARCHAR(24),@RunRBaseStart,120)+''' AND CounterDateTime<'''+CONVERT(VARCHAR(24),@RunRBaseEnd,120) +''')'
 84               +CHAR(10)+'OR (CounterDateTime>'+CASE WHEN CONVERT(VARCHAR(24),@RunRBaseStart,120)=CONVERT(VARCHAR(24),@RunRBaseStart,23)+' 00:00:00' THEN ''ELSE '=' END +''''+CONVERT(VARCHAR(24),DATEADD(WEEK,-1,@RunRBaseStart),120)+''' AND CounterDateTime<'''+CONVERT(VARCHAR(24),DATEADD(WEEK,-1,@RunRBaseEnd),120)+''')'
 85   SET @GROUPBY='DATEADD(MI,-DATEDIFF(MI,CounterDateTime,'''+CONVERT(VARCHAR(24),@RunRBaseEnd,120)+''')%(24*60),'''+CONVERT(VARCHAR(24),DATEADD(WEEK,1,@RunRBaseEnd),120)+''')'
 86 END
 87 
 88 UPDATE SQLPerfDataStat.dbo.CounterDetails_Dts
 89 SET LastRBaseEnd=@RunRBaseEnd
 90 WHERE MachineName=@MachineName
 91 
 92 BEGIN TRY
 93 --插入數據
 94   SET @SqlCmd='INSERT INTO dbo.RBaseCounterData_OneMinute(CounterID, CounterDateTime, CounterValue,MaxCounterValue)'
 95     +CHAR(10)+'SELECT CounterID,'+@GROUPBY+',AVG(CounterValue),MAX(CounterValue) FROM'
 96     +CHAR(10)+'(SELECT CounterID,CounterDateTime,CounterValue'
 97     +CHAR(10)+'FROM CounterData_OneMinute a (NOLOCK)'
 98     +CHAR(10)+@WHERE+') '
 99     +CHAR(10)+'AND EXISTS (SELECT TOP 1 1 FROM CounterDetails_Collect WITH(NOLOCK) WHERE CounterID = a.CounterID AND MachineName='''+@MachineName+''')'    
100     +CHAR(10)+'Union all'
101     +CHAR(10)+'SELECT CounterID,CounterDateTime,CounterValue'
102     +CHAR(10)+'FROM SQLPerfDataStat_Histroy.dbo.CounterData_OneMinute_bak a (NOLOCK)'
103     +CHAR(10)+@WHERE+') '
104     +CHAR(10)+'AND EXISTS (SELECT TOP 1 1 FROM CounterDetails_Collect WITH(NOLOCK) WHERE CounterID = a.CounterID AND MachineName='''+@MachineName+''')'        
105     +CHAR(10)+')AA'
106     +CHAR(10)+ 'GROUP BY CounterID,'+@GROUPBY
107   exec (@SqlCmd)
108 
109   --0點數據處理
110   IF CONVERT(VARCHAR(24),@RunRBaseStart,120)=CONVERT(VARCHAR(24),@RunRBaseStart,23)+' 00:00:00'
111   BEGIN         
112     IF DATEPART(WEEKDAY,@RunRBaseStart) IN(2,3,4,5,6)--非週末
113     BEGIN  
114       SET @WHERE='WHERE ((CounterDateTime='''+CONVERT(VARCHAR(24),@RunRBaseStart,120)+''')'
115       SET @CYLE =1
116       WHILE(@CYLE<14)
117         BEGIN
118           IF DATEPART(WEEKDAY,DATEADD(D,-@CYLE,@RunRBaseStart)) IN(2,3,4,5,6)
119           BEGIN 
120             SET @WHERE=@WHERE+CHAR(10)+'OR (CounterDateTime='''+CONVERT(VARCHAR(24),DATEADD(D,-@CYLE,@RunRBaseStart),120)+''')'        
121           END
122           SET @CYLE = @CYLE + 1
123         END
124       SET @GROUPBY='DATEADD(MI,-DATEDIFF(MI,CounterDateTime,'''+CONVERT(VARCHAR(24),@RunRBaseEnd,120)+''')%(24*60),'''+CASE WHEN DATEPART(WEEKDAY,@RunRBaseStart) =6 THEN CONVERT(VARCHAR(24),DATEADD(D,3,@RunRBaseEnd),120) ELSE CONVERT(VARCHAR(24),DATEADD(D,1,@RunRBaseEnd),120) END+''')'
125     END
126     ELSE--週末數據,取近兩週週末數據庫
127     BEGIN 
128       SET @WHERE='WHERE ((CounterDateTime='''+CONVERT(VARCHAR(24),@RunRBaseStart,120)+''')'
129                   +CHAR(10)+'OR (CounterDateTime='''+CONVERT(VARCHAR(24),DATEADD(WEEK,-1,@RunRBaseStart),120)+''')'
130       SET @GROUPBY='DATEADD(MI,-DATEDIFF(MI,CounterDateTime,'''+CONVERT(VARCHAR(24),@RunRBaseEnd,120)+''')%(24*60),'''+CONVERT(VARCHAR(24),DATEADD(D,-1,DATEADD(WEEK,1,@RunRBaseEnd)),120)+''')'
131     END
132     SET @SqlCmd='INSERT INTO dbo.RBaseCounterData_OneMinute(CounterID, CounterDateTime, CounterValue,MaxCounterValue)'
133     +CHAR(10)+'SELECT CounterID,'+@GROUPBY+',AVG(CounterValue),MAX(CounterValue) FROM'
134     +CHAR(10)+'(SELECT CounterID,CounterDateTime,CounterValue'
135     +CHAR(10)+'FROM CounterData_OneMinute a (NOLOCK)'
136     +CHAR(10)+@WHERE+') '
137     +CHAR(10)+'AND EXISTS (SELECT TOP 1 1 FROM CounterDetails_Collect WITH(NOLOCK) WHERE CounterID = a.CounterID AND MachineName='''+@MachineName+''')'    
138     +CHAR(10)+'Union all'
139     +CHAR(10)+'SELECT CounterID,CounterDateTime,CounterValue'
140     +CHAR(10)+'FROM SQLPerfDataStat_Histroy.dbo.CounterData_OneMinute_bak a (NOLOCK)'
141     +CHAR(10)+@WHERE+') '
142     +CHAR(10)+'AND EXISTS (SELECT TOP 1 1 FROM CounterDetails_Collect WITH(NOLOCK) WHERE CounterID = a.CounterID AND MachineName='''+@MachineName+''')'        
143     +CHAR(10)+')AA'    
144     +CHAR(10)+ 'GROUP BY CounterID,'+@GROUPBY
145     exec (@SqlCmd)
146   END
147 
148 END TRY
149 BEGIN CATCH
150   SET @ERRORMSG=@ERRORMSG+ERROR_MESSAGE()
151   IF @@TRANCOUNT >0
152   BEGIN
153     ROLLBACK;
154   END 
155 END CATCH
156 
157 FETCH NEXT FROM RBaseCur INTO @MachineName,@LastOneMinute,@LastRBaseEnd
158 END
159 CLOSE RBaseCur
160 DEALLOCATE  RBaseCur
161 
162 
163 IF @ERRORMSG<>''
164 BEGIN
165 ;THROW 50000,@ERRORMSG,1
166 END
spb_RBaseCounterData

環比數據能夠說明數據近期走勢。具備優化對比價值。從上同比圖能夠看出117-27-139-238服務器cpu在歷史上存在異常升高,下圖紅線環比比數據,卻與藍線同步,說明此異常在近期已經正常。服務器

clip_image005

相關文章
相關標籤/搜索