Skip to content

Instantly share code, notes, and snippets.

@jnm2
Created January 29, 2025 16:47
Show Gist options
  • Save jnm2/db2efaec4822e9ee03ecf0b55140a243 to your computer and use it in GitHub Desktop.
Save jnm2/db2efaec4822e9ee03ecf0b55140a243 to your computer and use it in GitHub Desktop.
declare @databaseNameFilter sysname = ''
select
last_execution_time as LastExecutionTime,
last_elapsed_time * 0.000001 as ElapsedSeconds,
last_rows as LastRowCount,
total_rows as TotalRowCount,
execution_count as ExecutionCount,
objects.name as ObjectName,
SqlText.text as CommandText
from sys.dm_exec_query_stats as QueryStats
cross apply (
select
min(case when attribute = 'objectid' then cast(value as int) end) as objectid,
min(case when attribute = 'dbid' then cast(value as int) end) as dbid,
min(case when attribute = 'dbid_execute' then cast(value as int) end) as dbid_execute
from sys.dm_exec_plan_attributes(QueryStats.plan_handle)
) as PlanAttributes
left join sys.objects on objects.object_id = PlanAttributes.objectid
outer apply sys.dm_exec_sql_text(QueryStats.plan_handle) as SqlText
where len(@databaseNameFilter) = 0 or db_id(@databaseNameFilter) in (PlanAttributes.dbid, PlanAttributes.dbid_execute)
order by LastExecutionTime desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment