We have created a custom alert in DPA for an SQL Query to send email if running for more than 30 seconds. I have also created the following in SQL Server Agent Job as follows (job fires every min):
DECLARE @counter INT;
SELECT
@counter = COUNT(*)
FROM
( SELECT
*
FROM
( SELECT
'TotalViewReportingODS' AS DBNAme
,sqltext.TEXT
,req.session_id
,req.status
,req.command
,req.cpu_time
,req.total_elapsed_time
FROM
sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
WHERE
DB_NAME(req.database_id) = 'TotalViewReportingODS'
) allRun
WHERE
total_elapsed_time > 30000
-- cpu_time > 30000
and text LIKE '%fnReturnCommonData%'
) counters
IF @counter > 0
BEGIN
--send mail to dbas
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'DBMailing',
@recipients = List of emails here,
@subject = 'Long runnung function fnReturnCommonData',
@body = 'Long running SQL in totalVIew'
END
I get emails every day from the DPA alert but never from this.
How is the DPA alert gathering data? Is it looking at average running time over a period of just on statement running longer that 30 seconds.