Retrieve the TOP 10 Elapsed time (query)

  • Hi ,

    Someone can give me a script that return the top 10 sql queries based on elapsed time for the past 7 days.

    Please ?

  • I'm not exactly sure what you are looking for. I suspect if it's for any specific database that you will required a tool. Check out the link.

    http://stackoverflow.com/questions/1201888/is-there-any-tool-to-see-the-queries-run-against-the-database

    What are you trying to achieve from this result? That may help with responses.

  • Hi , it's for sqlserver

    I want to see every friday the top 10 query of the week that take more time

  • Did you check out the link?

  • Unless you store all the queries that run on your server (or at least the more expensive), there's no way you can obtain them from a script.

    However, you can use something like this to start a query that will fit your needs. Remember that this will only return queries that are still on cache and it might not bring queries for several days depending on many things.

    SELECTt.text,

    s.*

    FROM (SELECT TOP 10 *

    FROM sys.dm_exec_query_stats

    ORDER BY max_elapsed_time DESC) s

    CROSS APPLY sys.dm_exec_sql_text(sql_handle ) t

    Remember that this is just a start.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • There is no quick answer to this one. Luis gave you a query, but it has nothing to do with the last seven days. It just gives you a dump of the plan cache, and returns the queries with the max elapsed time in the cache right now.

    Now, max elapsed time is not necessarily any of your top 10 queries. If there is an evil one-off query than ran for an hour, or a query that runs for two minutes every time, and has runs 10000 times, which query do you want to see?

    Since the query is a snapshot of the cache, this means that you may see queries that have been in the cache for months. Or you may not see all queries that have executed last week, because plans have been flushed or recompiled.

    If you want something like this, you need to snapshot the cache to table regularly, and then you can start analysing. This is not a trivial job, and you need to have good understanding of what you are looking for.

    Or you go out and buy a third-party too.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • Thank you for completing the information Erland. I just gave a starting point but as you've said, there's a long way to go from there (unless the OP takes the easy path and gets a third party tool).

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply