Skip to content

Instantly share code, notes, and snippets.

@ccjeng
Created March 16, 2013 07:11
Show Gist options
  • Save ccjeng/5175351 to your computer and use it in GitHub Desktop.
Save ccjeng/5175351 to your computer and use it in GitHub Desktop.
SQL Server: get top 50 slow sql statement
--get top 50 slow sql statement
select top 50 * from
(
SELECT SUBSTRING(st.text, (qs.statement_start_offset/2)+1,
((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset
END - qs.statement_start_offset)/2) + 1) AS StatementText,
last_execution_time as LastExecuted,
last_elapsed_time/1000000.0 as ElapsedTimeInSeconds,
execution_count as ExecCountFromLastCompile
, st.dbid
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
--where last_execution_time between '20080903' and '20080903'
) t
--where StatementText like '%JOB%'
where ElapsedTimeInSeconds > 1
order by --LastExecuted desc,
ElapsedTimeInSeconds desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment