Sql statement troubleshooting for SqlServer performance testing

        Many times, when we use SQL statements to query data, it is inevitable that we will miss the consideration of SQL statement performance, so sometimes it will cause the problem of excessive SqlServer service usage. In order to roughly troubleshoot which SQL statements are causing the problem, we can pass The following SQL query shows all the most time-consuming SQL statements recently. The specific query SQL statements are as follows:

SELECT s2.dbid,
       s1.sql_handle,
       (
           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,
       last_worker_time '最后执行总耗时(毫秒)',
       last_execution_time '最后执行时间',
       total_worker_time '所有执行总耗时(毫秒)',
       min_worker_time '执行最小耗时(毫秒)',
       max_worker_time '执行最大耗时(毫秒)',
       execution_count,
       plan_generation_num,
       total_physical_reads,
       last_physical_reads,
       min_physical_reads,
       max_physical_reads,
       total_logical_writes,
       last_logical_writes,
       min_logical_writes,
       max_logical_writes
FROM sys.dm_exec_query_stats AS s1
    CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2
WHERE s2.objectid IS NULL
ORDER BY last_worker_time DESC,
         s1.sql_handle,
         s1.statement_start_offset,
         s1.statement_end_offset;

Execution effect:

Guess you like

Origin blog.csdn.net/qubernet/article/details/107376630