This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

Custom Alert - Long Running Queries

FormerMember
FormerMember

Description

This alert is for SQL Server instances only and will tell you when SQL statements have executed more than a defined amount of time. This example uses a 300 second threshold, meaning any SQL statements that have been executing for longer than 300 seconds (5 minutes) will be alerted. The alert query executes against the monitored database directly so caution should be taken on how often the alert is executed, especially if less than the default of every 300 seconds.

Definition

To create the alert, click on Alerts > Manage Alerts tab and create a Custom Alert of type Custom SQL Alert - Multiple Numeric Return. Configure the alert with values similar to these:

  • Alert Name: SQL Server - Long Running Queries
  • Execution Interval: 5 Minutes
  • Notification Text: The following list of SQL statements have been running for over 5 minutes.
  • Execute Against: Monitored Database
  • Units: Seconds
  • High Threshold: Minimum 300 (This value must match value found in query. Default: 300 ), MAX empty

SQL Statement

SELECT 'SPID=' + CONVERT(VARCHAR, a.session_id) + ' has been running the following for ' + CONVERT(VARCHAR, DATEDIFF(SS, a.start_time, CURRENT_TIMESTAMP)) + ' seconds: ' + convert(VARCHAR, b.TEXT)
     , CONVERT(VARCHAR, DATEDIFF(SS, a.start_time, CURRENT_TIMESTAMP))
FROM sys.dm_exec_requests a
CROSS APPLY sys.dm_exec_sql_text(a.sql_handle) b
WHERE a.STATUS <> 'background'
     AND DATEDIFF(SS, a.start_time, CURRENT_TIMESTAMP) > 300 --This value of 300 must match the Execution Interval set in the alert

  • Hello,

    this alert - is what I'm looking for, but having "non-standart" sql_hash makes it useless...

    Isn't there any way to find the hash value like in a web presentation (I mean 10-digit format)?
    Maybe, by making a 'join' (based, for instance, on session_id) with some other table? Or execute completely anoter query, on another tables?
    Thanks!

  • It's on our roadmap to persist/display the SQL handle which would allow the bridge back to SQL Server or from SQL Server to DPA in this case.  However, that hasn't been implemented yet.  I respectfully disagree about the "uselessness" of this alert though. I'd assume you are running this to notify you that a SPID is running a query for longer than 5 minutes (as defined above).  When you get this alert, you can hop into DPA and filter down to the interval or timeframe this occurred, flip over to sessions tab, look for the appropriate SPID, click on it and get all the detail you want.  This is just letting you know there's something you need to take a look at...

  • Thanks for your answer!

    It's good news that you are working on displaying SQL handle.

    And your proposition about using sessioin to determine appropriate slow SQL looks great, I think this way problems can be determined fast enough.
    By the way, can you tell please, for what period of time session_id is unique? For hour or day, etc.

    Looking into the problem from the other side - can't we use query for alert like Ignite use for create daily report (to show "Top waits", for instance)?

  • SPIDs will get reused.  Remember, we're displaying what SQL Server is telling us.  A SPID could be used by app A between 2:01 and 2:03, then release it and then that same SPID will be used by app B between 2:03:01 and 2:03:05.  If applications are using connection pooling (best practice), the app hopping should not be significant.  So the answer to this one is it depends on your apps.  8 )

    Can you clarify the next question.  Are you wanting to create a daily report to show top queries?  Or use the data from the alerts to build a report?

  • Expected behavior of SPID lifecycle)
    About next question:
    I'd like to have an alert with report-alike information. Or, in other words, I'd like to use report's query (with more stringent time range - not for all day, but for last 15 minutes, for instance) for alert.

  • Very possible.  Whatever TSQL you can come up with will work as long as the output is acceptable.

    For the single numeric, that one's self-explanatory.  For the multi numeric, that one can have one alpha/numeric and one numeric value (the numeric always second).

    In fact, we've had lots of folks grab some of our custom content and tweak for other uses.

  • That would be avesome.

    I need to converse with my teammates to make a final decision what alert to use. Nevertheless, I'd like to work with report's SQL. Could you tell me, where can find it?
    Thank you so much for your help!

  • Go here:  https://thwack.solarwinds.com/community/database-management/database-performance-analyzer and scroll down.  On the left hand side you will see a link to custom SQL content including reports, alerts and even metrics.  You can use them as is and plug them into DPA or you can tweak them per your own requirements.  You're most welcome!

  • That's very interesting, I just found a custom report Custom Alert - SQL Statements with Abnormal Execution Counts which outputs exactly required hash from Ignite to e-mail, it's maybe possible to use this modified report for our required alerts?

    Thanks

  • For sure.  See constraints in my previous post though about outputs.