0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SQL Serverで時間のかかっているクエリを算出するクエリ

Posted at

備忘録
コンパイルされたクエリ毎に実行される

DECLARE @DB nvarchar(16) = N'sample-database'
DECLARE @何分前 int = 30

SELECT 
    qs.last_execution_time AS 最後に実行された時間
    , qs.execution_count AS クエリ実行回数
    , qs.total_elapsed_time / 1000000 AS 合計実行秒
    , SUBSTRING(qt.text,qs.statement_start_offset/2 +1,   
                 (CASE WHEN qs.statement_end_offset = -1   
                       THEN LEN(CONVERT(nvarchar(max), qt.text)) * 2   
                       ELSE qs.statement_end_offset end -  
                            qs.statement_start_offset  
                 )/2  
             ) AS query_text
    , dbname = DB_NAME(qt.dbid) 
FROM sys.dm_exec_query_stats AS qs   
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt
WHERE
    DB_NAME(qt.dbid) = @DB
    and last_execution_time >= dateadd(minute, @何分前 * -1, getdate())
ORDER BY
    qs.total_elapsed_time desc
;  
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?