SQL alert when stored procedure takes too long

I would like to configure SQL Server 2008 notification to notify me when a procedure runs for 1 second or longer (just an example).

Any ideas?

EDIT:

Well, it seems like this is impossible. But, just to push you in a different direction, I know that in the main database there are statistical tables that contain compilation calculations, the number of calls and other statistics. Can I possibly request them periodically and then report it, somehow?

+5
source share
6 answers

, . .

+3

sqlservercentral (, SQL Server, 2008 )

http://www.sqlservercentral.com/scripts/Lock+and+Connection+Management/30144/

, .

, , , . , , , ; , . sysprocesses . script , , . sysprocesses ( , ), , , , " ".

CREATE proc sp_check_job_running
    @job_name       char(50),
    @minutes_allowed    int,
    @person_to_notify   varchar(50)
AS  

DECLARE @var1           char(1),
    @process_id     char(8),
    @job_id_char        char(8),
    @minutes_running    int,
    @message_text       varchar(255)

select @job_id_char = substring(CAST(job_id AS char(50)),1,8) 
from  msdb..sysjobs
where name = @job_name

select @process_id =    substring(@job_id_char,7,2) + 
            substring(@job_id_char,5,2) +
            substring(@job_id_char,3,2) +
            substring(@job_id_char,1,2)


select @minutes_running = DATEDIFF(minute,last_batch, getdate())
from master..sysprocesses
where program_name LIKE ('%0x' + @process_id +'%')

if @minutes_running > @minutes_allowed
  BEGIN
    select @message_text = ('Job ' 
    + UPPER(SUBSTRING(@job_name,1,LEN(@job_name)))
    + ' has been running for '
    + SUBSTRING(CAST(@minutes_running AS char(5)),1,LEN(CAST(@minutes_running AS char(5))))
    + ' minutes, which is over the allowed run time of '
    + SUBSTRING(CAST(@minutes_allowed AS char(5)),1,LEN(CAST(@minutes_allowed AS char(5)))) 
    + ' minutes.')
    EXEC master..xp_sendmail 
    @recipients = @person_to_notify, 
    @message = @message_text,
        @subject = 'Long-Running Job to Check'
  END

--  Typical job step syntax for job to do the checking

execute sp_check_job_running
      'JobThatSHouldBeDoneIn5Minutes', 
       5, 
       'DBAdmin@mycompany.com'
+2

Mladen Prajdic kevchadders SQL Server Central. DBMail SQLMail ( SQLServerCentral xp_sendmail). , SQLMail MAPI , DBMail SMTP . .

SQL Server Central SQL 2005, SQL 2005 - YMMV .

UDF, JOIN:

CREATE FUNCTION dbo.udf_SysJobs_GetProcessid(@job_id uniqueidentifier)
RETURNS VARCHAR(8)
AS
BEGIN
    RETURN (substring(left(@job_id,8),7,2) +
    substring(left(@job_id,8),5,2) +
    substring(left(@job_id,8),3,2) +
    substring(left(@job_id,8),1,2))
END

sproc:

CREATE PROC sp_check_job_running 
    @job_name char(50),
    @minutes_allowed int, 
    @person_to_notify varchar(50) 

AS 

DECLARE @minutes_running int, 
    @message_text varchar(255)

SELECT @minutes_running = isnull(DATEDIFF(mi, p.last_batch, getdate()), 0)
FROM master..sysprocesses p
JOIN msdb..sysjobs j ON dbo.udf_sysjobs_getprocessid(j.job_id) = substring(p.program_name,32,8)
WHERE j.name = @job_name

IF @minutes_running > @minutes_allowed 
    BEGIN
      SELECT @message_text = ('Job ' + UPPER(SUBSTRING(@job_name,1,LEN(@job_name))) + ' has been running for ' + SUBSTRING(CAST(@minutes_running AS char(5)),1,LEN(CAST(@minutes_running AS char(5)))) + ' minutes, which is over the allowed run time of ' + SUBSTRING(CAST(@minutes_allowed AS char(5)),1,LEN(CAST(@minutes_allowed AS char(5)))) + ' minutes.') 
      EXEC msdb.dbo.sp_send_dbmail
        @recipients = @person_to_notify,
        @body = @message_text,
        @subject = 'Long-Running Job to Check' 
    END

sproc SQL Server . , . , .

.

EXEC sp_check_job_running 'JobNameGoesHere', 5, 'admin@mycompany.com'

HT: http://www.sqlserverspecialists.co.uk/blog/_archives/2008/11/26/3996346.html

+2

, Nagios, .

PRTG. , 5 3 , 10 :

SELECT total_elapsed_time / execution_count / 1000000 avg_elapsed_time, execution_count, creation_time, last_execution_time, 
SUBSTRING(st.text, (qs.statement_start_offset/2) + 1, ((CASE statement_end_offset WHEN -1 THEN DATALENGTH(st.text) ELSE qs.statement_end_offset END - qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
WHERE total_elapsed_time / execution_count / 1000000 >= 3  -- filter queries that takes > 3 seconds
AND CHARINDEX('dm_exec_query_stats', st.text) = 0   -- In case this query is slow, we don't want it in the result set!
ORDER BY total_elapsed_time / execution_count DESC;

: fooobar.com/questions/115575/...

, , sys.dm_exec_procedure_stats sys.dm_exec_sql_text ( creation_time SUBSTRING st.text)

+2

, SQL Profiler, . ( , , - "" ).

SQL Profiler, :

SQL Profiler - , .

SQLProfilerTSQL_Duration

: : "" 1000 ()

, , .

Events SProcs ( SQL)

0

If you are not looking for a free solution, you can take a look at SQL Sentry . He does what you ask, and more.

0
source

All Articles