2

My manager is requesting to know when was the last time a number of reports were last ran to see if they are getting used. He requested info from our vendor and the vendor if coming to me for SA level permission to dig through the relative SQL Server.

Obviously, I won't be doing that but I would like to know is there a way to track the last time a specific query has been executed?

This database resides on an old SQL Server 2008 R2 install so I am sure my options are limited if not non-existent especially since he wants to look back as far as 18 months.

Just figured I would ask in case there might be at least a partial way to go back in history and track this information.

2 Answers 2

4

When were your servers last booted? Unless you have some form of auditing running, then that is a far back as you will be able to query the sys.dm_exec_query_stats view and sys.dm_exec_sql_text() table-valued function.

You can then use the following statement to see when a statement was last executed:

SELECT sdest.TEXT , sdeqs.last_execution_time, *
FROM   sys.dm_exec_query_stats AS sdeqs
   CROSS APPLY sys.dm_exec_sql_text(sdeqs.sql_handle) AS sdest

However, there are some restrictions with this approach:

  • Size of Memory reserved for SQL Server instance
  • Execution of DBCC FREEPROCCACHE
  • Execution of DBCC FREESESSIONCACHE
  • Execution of DBCC FREESYSTEMCACHE('ALL')
  • other possible reasons for SQL Server to free up cache including, but not limited to:
    • Server reboots
    • Instance restarts
2
  • Just looking over these answers it just answers my question in general. Is this possible over 18 months without already having something in place? The answer is no, having a way to look through the cache might be of some help but best case in my situation that covers about two weeks.
    – Shawn
    Jul 31, 2018 at 14:08
  • @Shawn No, SQL Server doesn't maintain stats like that. You need a monitoring and/or audit tool. Even Query Store has limits on how much data it keeps (and you want those limits). Jul 31, 2018 at 14:46
1

Go ahead and run this:

SELECT sqlserver_start_time FROM sys.dm_os_sys_info

This code snippet will tell you wen SQL Server was last restarted. If you are not running something that captures code to analyze later from the dynamic management views, you will only have historical data up until either the last restart or when that data has aged out of cache.

You can run this while changing the database in the code to match yours:

SELECT deqs.last_execution_time AS [Time], dest.text AS [Query], dest.*
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
WHERE dest.dbid = DB_ID('msdb')
ORDER BY deqs.last_execution_time DESC

Written by Tommy_o on Stack Exchange.

I would recommend to start capturing and aggregating data like this regularly if you can and have the administrative / spec capacity to do so. Sp_Whoisactive is a great start to doing this. When you do upgrade to 2016, you can use the Query Store to capture similar data when you enable that feature.

3
  • Thanks for the heads up on Query Store. While looking for an answer to this question I had seen it mentioned somewhere else and we are moving that server to a SQL Server 2016 install next month so I will keep that in mind going forward.
    – Shawn
    Jul 31, 2018 at 14:09
  • @Shawn when you upgrade I would recommend to set the compatibility level to 2008 R2 and leave it for a month. Simultaneously you will want to create a template for query store in model db or if you upgrade to 2017, a lot of the defaults will be alright. The idea is that the optimizer has changed drastically, so you will capture your query plans in 2008 R2 mode, then you can switch compatibility after you have monitored everything. Then when queries go awry, you can force the plan to the old while you begin troubleshooting and tuning. Jul 31, 2018 at 14:44
  • thank you this is all great feedback to keep in mind when we make the migration to a SQL 2016 server.
    – Shawn
    Jul 31, 2018 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.