请选择 进入手机版 | 继续访问电脑版

技术控

    今日:316| 主题:53845
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Handling Special Dates For SQL Server Metrics

[复制链接]
扯不掉的面具 发表于 2016-12-1 14:41:03
184 1

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Problem

  We've noticed that holidays and weekends impact our collection of metrics across our systems. Should we track these, or are there easy ways we filter these out?
  Solution

  Holidays, weekends and seasonal cycles might have an impact on metric evaluation and filtering them or separating them may assist in establishing stronger baselines for analysis. This applies for architecture that has down time or is used in cycles, such as the common Monday through Friday cycle. In this tip, we'll look at some approaches to handling these values that we can combine in our metric analysis, or that we can use for other application purposes where the time cycle will impact us.
  One approach to tracking weekends, holidays and possible business downtime days (if applicable) is to use a date table where we can exclude dates through a join to the table. In the below code example, I create this join table with only two columns that will be used, one for the join (JoinDateValue) and one of the filter (WeekendHoliday). In some cases, we might find value by adding more columns, like knowing whether it's a weekend, holiday, or other business cyclical date.
  1. /*

  2. ---- Table for JOIN
  3. CREATE TABLE tblDateJoin(
  4. DateID SMALLINT IDENTITY(1,1),
  5. JoinDateValue DATE,
  6. WeekendHoliday BIT
  7. )

  8. */

  9. DECLARE @stYear DATE = '2016-01-01'
  10. DECLARE @enYear DATE = '2016-12-31'

  11. WHILE @stYear <= @enYear
  12. BEGIN

  13. ---- Sometimes a business may have a different cycle, so this can be changed to fit the cycle
  14. IF (DATEPART(DW,@stYear) IN (1,7))
  15. BEGIN
  16.   INSERT INTO tblDateJoin (JoinDateValue,WeekendHoliday) VALUES (@stYear,1)
  17. END
  18. ELSE
  19. BEGIN
  20.   INSERT INTO tblDateJoin (JoinDateValue,WeekendHoliday) VALUES (@stYear,0)
  21. END

  22. SET @stYear = DATEADD(DD,1,@stYear)
  23. END

  24. SELECT *
  25. FROM tblDateJoin

  26. ---- For holidays, we would execute the below - example with New Years' Day:
  27. UPDATE tblDateJoin
  28. SET WeekendHoliday = 1
  29. WHERE tblDateJoin = '2016-01-01'
复制代码
A contrived join example with this table:
  1. SELECT t.*
  2. FROM tblMetricData t
  3. ---- In this example, tblMetricData's column MetricDate is a DATE field
  4. INNER JOIN tblDateJoin tt ON t.MetricDate = tt.JoinDateValue
  5. WHERE tt.WeekendHoliday = 0
复制代码
We should note here that not all business (or even states and provinces) honor the same holidays, business cycles, and sometimes even weekends are different - in fact, in the case of holidays and business cycles, I've seen these significantly vary. The key is that when we perform the JOIN to this table, we avoid using statements    NOT IN ('2016-01-01'), which might create significant costs if we have multiple years. Another benefit to using a join table like this is that we can replicate or bulk copy it on other servers for the same level of analysis for our needs - essentially, one table is the source of truth and all other servers use the source of truth table. Because businesses can differ from each other, it might be just as convenient to exclude the dates within the query in situations where the query must be manually constructed each time and only a few dates are excluded, like a once-a-year report running in December where only December 25th is supposed to be excluded.  
  If you consider excluding dates, be careful about excluding these dates in every report for certain environments, as these dates can still provide useful information if we determine that usage has increased during these periods, or if we're using these periods to identify and maintain administrative task schedules.
  If we're in an environment where we know that we don't need these metrics, or space is a heavy restriction so we're limited, we can create the filter on the data retrieval level. For an example, if we're using PowerShell to retrieve metrics, we can create the filter on the PowerShell level. In the below example, I exclude Sundays and Mondays within an if statement, where we could apply further logic depending on needs:
  1. <# PS Version 4.0 #>
  2. ### We can filter on the script/application level
  3. if ((Get-Date).DayOfWeek | Where-Object {$_ -notin ("Sunday","Monday")})
  4. {
  5.     Write-Host "Do something"
  6. }

  7. ### Let's test this with other dates:
  8. $testOne = ((Get-Date).AddDays(-4).DayOfWeek)
  9. $testTwo = ((Get-Date).AddDays(1).DayOfWeek)

  10. if ($testOne | Where-Object {$_ -notin ("Sunday","Monday")})
  11. {
  12.     Write-Host "($testOne) Do something"
  13. }

  14. if ($testTwo | Where-Object {$_ -notin ("Sunday","Monday")})
  15. {
  16.     Write-Host "($testTwo) Do something"
  17. }
复制代码
From here, we could skip retaining the information, or log it elsewhere, or even log a different set of information, relative to the cycle we're trying to filter. If we're completely excluding data and we're using a tool like Task Scheduler, SQL Server Agent, or the .NET Quartz library, we could just have the schedule exclude Sundays and Mondays on run times during the week.
  I've seen rare cases where the business is    offduring periods and this is the intended behavior - like a site being turned off for a daily period on the cloud. In these rare cases, the monitoring matches the on and off cycle of the business, so no metric collection occurs when the time is off.  
  Next Steps

  
       
  • I recommend collecting a baseline of everything for at least three to four months before determining what to exclude. This does vary by environment, but it's a good starting practice (see last example as an exception).   
  • Consider the costs and benefits of filtering while storing everything versus storing only what's needed.   
  • Check out these additional resources:   
            
    •         Creating a date dimension or calendar table in SQL Server      
    •         SQL Server Function to return a range of dates      
    •         TSQL Function to Determine Holidays in SQL Server      
    •         SQL Server Date Time Calculation Examples      
    •         Built in Time Dimension and Time Intelligence in SQL Server Analysis      
    •         All Dates Tips   
      
Last Update:  12/1/2016



上一篇:Configure One Way Merge Replication for SQL Server
下一篇:Image zoom with pure JavaScript
你穿过的墙 发表于 2016-12-1 17:13:23
试金可以用火,试女人可以用金,试男人可以用女人。
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )

© 2001-2017 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表