Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the worst performing queries in SQL Server 2008?

How to find the worst performing queries in SQL Server 2008?

I found the following example but it does not seem to work:

SELECT TOP 5 obj.name, max_logical_reads, max_elapsed_time
FROM sys.dm_exec_query_stats a
CROSS APPLY sys.dm_exec_sql_text(sql_handle) hnd
INNER JOIN sys.sysobjects obj on hnd.objectid = obj.id
ORDER BY max_logical_reads DESC

Taken from:

http://www.sqlservercurry.com/2010/03/top-5-costly-stored-procedures-in-sql.html

like image 349
Thomas Bratt Avatar asked Mar 23 '10 12:03

Thomas Bratt


People also ask

Where can I find worst performing queries in SQL Server?

One of the most important DMVs regarding your worst performing SQL Server queries is sys. dm_exec_query_stats. For every cached execution plan SQL Server stores detailed information about how this execution plan performed at runtime.

Where can I find slow running queries?

Right click a database, Reports, Standard Reports, then Object Execution Statistics. It lists the currently cached execution plans, along with the amount of resources and the number of times they've been run.


1 Answers

top 10 worst queries based on...:

SELECT TOP 10
    total_worker_time/execution_count AS Avg_CPU_Time
        ,execution_count
        ,total_elapsed_time/execution_count as AVG_Run_Time
        ,(SELECT
              SUBSTRING(text,statement_start_offset/2,(CASE
                                                           WHEN statement_end_offset = -1 THEN LEN(CONVERT(nvarchar(max), text)) * 2 
                                                           ELSE statement_end_offset 
                                                       END -statement_start_offset)/2
                       ) FROM sys.dm_exec_sql_text(sql_handle)
         ) AS query_text 
FROM sys.dm_exec_query_stats 

--pick your criteria

ORDER BY Avg_CPU_Time DESC
--ORDER BY AVG_Run_Time DESC
--ORDER BY execution_count DESC
like image 94
KM. Avatar answered Oct 02 '22 13:10

KM.