Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
SQL Server 2008 126
SQL Server 2005 109
TSQL Tips n Tricks 80
Performance Tuning 57
SQL Server 2012 48
SQL Server Basics 24
Sql Server Management 21
SSMS 21
Index 18
SQL Server Denali 13

Archive · View All
March 2011 24
December 2010 23
January 2011 22
May 2011 17
February 2011 16
April 2011 16
July 2012 12
August 2011 9
September 2012 8
August 2012 8

SQL Server: How to Figure out Peak/Off-Peak Hours of Production Databases

Mar 3 2011 12:22AM by aasim abdullah   

In every learning session of performance tuning we like to repeat one sentence “DON’T EXECUTE THIS IN PEAK HOURS” or you must wait for peak hours to execute a specific query. For example we should never execute REBUILD INDEX statement or FULL BACKUP DATABASE statement in peak hours of a production database.

But is there any way to find out these peak and off-peak hours for a production database. Performance counter SQL SERVER: SQL Statistics\Batch Request/Sec can be little helpful, but what if I want to create a graph report of work load for a specific production database.

Follow given steps to accomplish your goal.

1. Create a table to store work load data for next 24 hours or any other period of your choice

CREATE TABLE dbo.LoadCounter
    (
      cntr_time DATETIME,
      cntr_value BIGINT
    )

2. Create a job so LoadCounter table can be filled after every 10 minutes (or after interval of your own choice) and your are done.

Note:  Don’t forget to provide SERVER NAME, DATABASE NAME and LOGIN NAME for following statements at marked places

USE [msdb]
GO
--Add new job with name LoadCounter
DECLARE @jobId BINARY(16)
EXEC  msdb.dbo.sp_add_job @job_name=N'LoadCounter',
            @enabled=1,
            @notify_level_eventlog=0,
            @notify_level_email=2,
            @notify_level_netsend=2,
            @notify_level_page=2,
            @delete_level=0,
            @category_name=N'[Uncategorized (Local)]',
            @owner_login_name=N'YourLoginNameHere', --Provide your own login name here
            @job_id = @jobId OUTPUT
select @jobId
GO
EXEC msdb.dbo.sp_add_jobserver @job_name=N'LoadCounter', @server_name = N'YOURserverNAMEhere'--Provide your datbase server name here
GO
-- Create job setp to insert counter record from sys.dm_os_performance_counters
USE [msdb]
GO
EXEC msdb.dbo.sp_add_jobstep @job_name=N'LoadCounter', @step_name=N'LoadCounter',
            @step_id=1,
            @cmdexec_success_code=0,
            @on_success_action=1,
            @on_fail_action=2,
            @retry_attempts=0,
            @retry_interval=0,
            @os_run_priority=0, @subsystem=N'TSQL',
            @command=N'INSERT  INTO dbo.LoadCounter ( cntr_time, cntr_value )
        SELECT  GETDATE() AS cntr_time,
                cntr_value
        FROM    sys.dm_os_performance_counters
        WHERE   counter_name = ''Batch Requests/sec''',
            @database_name=N'YourDatabaseNameHere', --Provide Your Database Name here
            @flags=0
GO
-- Create Schedule
USE [msdb]
GO
DECLARE @schedule_id int
EXEC msdb.dbo.sp_add_jobschedule @job_name=N'LoadCounter', @name=N'LoadCounter',
            @enabled=1,
            @freq_type=4,
            @freq_interval=1,
            @freq_subday_type=4,
            @freq_subday_interval=10, -- exectue after every 10 Minutes
            @freq_relative_interval=0,
            @freq_recurrence_factor=1,
            @active_start_date=20110114,
            @active_end_date=99991231,
            @active_start_time=0,
            @active_end_time=235959, @schedule_id = @schedule_id OUTPUT
select @schedule_id
GO

3. Execute following query to see the results or you can create a report (or a graph) on this query. And easily find out that during which hours your database end users are working actively or just sleeping ;) .

SELECT  cntr_time,
        cntr_value - ( SELECT TOP ( 1 )
                                cntr_value
                       FROM     dbo.LoadCounter
                       WHERE    cntr_time < OuterTable.cntr_time
                       ORDER BY cntr_time DESC
                     ) AS BatchPerTenMin
FROM    dbo.LoadCounter OuterTable
ORDER BY cntr_time

Note: SQL SERVER Agent service must be running to execute your job after given intervals.

Tags: SQL Server 2008, Performance Tuning, #tsql, brh,


aasim abdullah
23 · 7% · 2372
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

5  Comments  

  • That is what i was looking for so loooooooooooong. Thanks for sharing

    commented on Jan 14 2011 10:43AM
    Anonymous
    248 · 1% · 178
  • Its really a nice script

    commented on Jan 15 2011 9:07AM
    Anonymous
    248 · 1% · 178
  • hello....... its good one for monitor Db peak hours.........

    i have one question is?

          if its showing negative value mean?
    

    i mean it showing like (-3456)?

    commented on Mar 10 2011 6:44AM
    peddi.surya
    1307 · 0% · 17
  • negative value is not correct, this can happen when ORDER BY clause in last output query is not working properly because of wrong cntr_time values.

    commented on Mar 10 2011 1:06PM
    aasim abdullah
    23 · 7% · 2372
  • ok thanks for ur reply.................

    one more thing I want to monitor in my local server to all my remote servers peak hours details ...

    how can i i configure this job in my local server ?

    I tried as ur script but getting error like ...... remote server name (i.e test server) is not found like getting error message

    Msg 14262, Level 16, State 1, Procedure spaddjobserver, Line 88 The specified @server_name ('testserver') does not exist.

    EXEC msdb.dbo.spaddjobserver @jobname=N'LoadCounter40', @servername = N'I given herer Remote server name'

    I add this server linked server also

    still its throws above error ..

    commented on Mar 10 2011 10:25PM
    peddi.surya
    1307 · 0% · 17

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]