Retention Period for Queries Executed

  • Hi Guys,

    On Fri. 10/19 I had a process that was in a suspended status because it was blocked by another SPID. Is there any way to determine whether or not that process was executed successfully?

    I queried a list of the current process and filtered it by adding a range for dates I wanted in the output - but the history didn't go as far back as I wanted. Is there a way to see what the retention period is for executed queries?

    I used the following query:

    SELECT deqs.last_execution_time AS [Time], dest.TEXT AS [Query]

    FROM sys.dm_exec_query_stats AS deqs CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest

    WHERE deqs.last_execution_time

    Between '2012-10-17' and '2012-10-22'

    ORDER BY deqs.last_execution_time DESC

    Can you guys please help me - is this query the correct way to pull a history on executed queries? Also, I have the exact SQL Statement that I'm looking for - is there a way to perhaps search by the query?

  • sys.dm_exec_query_stats

    Returns aggregate performance statistics for cached query plans in SQL Server 2012. The view contains one row per query statement within the cached plan, and the lifetime of the rows are tied to the plan itself. When a plan is removed from the cache, the corresponding rows are eliminated from this view.

    There's no set retention. It depends on memory pressure and how many other queries are executed as to when it will get pushed out of the plan cache.

  • What you're querying there is not a history of queries executed, it's the plan cache which contains some info on execution history. Plans that aren't cached don't appear in there, it's cleared any time SQL restarts or the database restarts, plans can be removed or replaced for a variety of reasons.

    If you need a history of queries executed, set up an extended events or SQL Trace session (warning, it'll be huge volumes of data)

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thank you, HowardW and Gail - you've been very helpful.

Viewing 4 posts - 1 through 3 (of 3 total)

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