Saturday 28 April 2012

Last Queries Executed Recently on Server.

If the Database users want to get the queries executed on the server before sometime, then he can get it by executing the below command. While it returns only those queries which are existing in the Cache.

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
ORDER BY    DEQS.last_execution_time DESC 

you will the below output screen : 


 












If you like to see execution plan as well then write the blow command:

SELECT      DEQS.last_execution_time AS [Time]
                ,DEST.TEXT AS [Query]
                ,DECP.QUERY_PLAN
FROM      sys.dm_exec_query_stats AS DEQS
CROSS APPLY sys.dm_exec_sql_text(DEQS.sql_handle) AS DEST
CROSS APPLY sys.dm_exec_query_plan(DEQS.plan_handle) AS DECP
ORDER BY DEQS.last_execution_time DESC












In the last column of above output is the XML formatted Execution Plan of the query.

No comments:

Post a Comment