Find queries executed in since last 4 hours

  • Hi,

    I need to get all queries that executed from last 4 hours. How can I get that?

    Thanks!

  • Something like this?

    SELECT t.text, q.last_execution_time

    FROM sys.dm_exec_query_stats q

    CROSS APPLY sys.dm_exec_sql_text(sql_handle ) t

    WHERE last_execution_time >= DATEADD(HH, -4, GETDATE())

    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
  • Luis's suggestion is quite good, but please keep in mind that you won't get the exact sequence of the statements that way.

    For instance, you won't get parameteres for parameterized queries.

    If you need to extract the exact workload in the last four hours, a trace is the way to go (especially if you're planning to replay it).

    Another option is capturing the activity with an Extended Events session (less impact on the server, but more difficult to replay).

    -- Gianluca Sartori

  • spaghettidba (10/24/2013)


    Luis's suggestion is quite good, but please keep in mind that you won't get the exact sequence of the statements that way.

    For instance, you won't get parameteres for parameterized queries.

    If you need to extract the exact workload in the last four hours, a trace is the way to go (especially if you're planning to replay it).

    Another option is capturing the activity with an Extended Events session (less impact on the server, but more difficult to replay).

    +1

    I was getting out of the office and didn't advice on the downsides of my solution. A trace has certainly helped me in the past.

    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 4 posts - 1 through 3 (of 3 total)

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