Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find SQL Server queries that took a lot of time?

Tags:

sql

sql-server

I have application that is up more than 3 days. I can see in logs that there was a moment when application executed some SQL query and this took a lot of time, probably because of some db locks.

I heard that there is a query for such situations. So I need to be able to ask all queries that took, for example, more than 30 minutes. Is it possible?

like image 481
Mikhail Avatar asked Feb 23 '10 13:02

Mikhail


People also ask

How do you find long running queries?

You can view any SQL statement that executes for more than 6 absolute seconds (the "long running" threshold) using the v$session_longops view. where rownum <=1; This query for long running SQL is especially useful when operations contain long running loops such as shown in the example below.

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

give this a try:

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 
ORDER BY AVG_Run_Time DESC
like image 89
KM. Avatar answered Oct 22 '22 09:10

KM.