Question

We have a tonne of linked servers on our database server that we would like to clean up.

Is it possible to determine the last date a linked server was used? e.g. the last time a query was performed through it.

Was it helpful?

Solution

You'll have to setup SQL Profiler to monitor for queries which use the linked server. There's no DMV that you can easily monitor for usage.

OTHER TIPS

You might be able to look at the dmv sys.dm_exec_sql_text to look at where the linked server name was last used in a sql statement.

set transaction isolation level read uncommitted

SELECT
    (SELECT TOP 1 SUBSTRING(s2.text,statement_start_offset / 2+1 , 
      ( (CASE WHEN statement_end_offset = -1 
         THEN (LEN(CONVERT(nvarchar(max),s2.text)) * 2) 
         ELSE statement_end_offset END)  - statement_start_offset) / 2+1)) 
        AS sql_statement,
    last_execution_time
FROM sys.dm_exec_query_stats AS s1 
    CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2  
WHERE 
    s2.text like '%LinkedServerName%' 
    and last_execution_time > convert(datetime, '2011-01-01 00:00:00.000', 121)
ORDER BY 
    s1.sql_handle, s1.statement_start_offset, s1.statement_end_offset

Lots of caveats for this:

  • if linked server is used via view/function it may not appear in your result set
  • it will only include any sql that is in the plan cache
    • the plan cache is cleared on restart
    • SQL Server will clear out old plans from the cache once it's size limits are reached

Note: If you do tweak it and get it working, please change the above sql

The only way you can be sure one is not being used is to drop it. ;) I have monitored linked servers for weeks and then had someone complain within hours of dropping it.

You should script it out first. Right click in Object Explorer and chose the appropriate option.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top