SQLServer最耗資源時間的SQL語句

2022-02-23 16:44:28 字數 2658 閱讀 8975

作者kolachen

先拷過來了,等有空再研究一下

執行最慢的sql語句

select

(total_elapsed_time / execution_count)/1000 n'平均時間ms'

,total_elapsed_time/1000 n'總花費時間ms'

,total_worker_time/1000 n'所用的cpu總時間ms'

,total_physical_reads n'物理讀取總次數'

,total_logical_reads/execution_count n'每次邏輯讀次數'

,total_logical_reads n'邏輯讀取總次數'

,total_logical_writes n'邏輯寫入總次數'

,execution_count n'執行次數'

,substring(st.text, (qs.statement_start_offset/2) + 1,

((case statement_end_offset

when -1 then datalength(st.text)

else qs.statement_end_offset end

- qs.statement_start_offset)/2) + 1) n'執行語句'

,creation_time n'語句編譯時間'

,last_execution_time n'上次執行時間'

from

where

substring(st.text, (qs.statement_start_offset/2) + 1,

((case statement_end_offset

when -1 then datalength(st.text)

else qs.statement_end_offset end

- qs.statement_start_offset)/2) + 1) not like '�tch%'

order by

total_elapsed_time / execution_count desc;

--總耗cpu最多的前個sql:

select top 20

total_worker_time/1000 as [總消耗cpu 時間(ms)],execution_count [執行次數],

qs.total_worker_time/qs.execution_count/1000 as [平均消耗cpu 時間(ms)],

last_execution_time as [最後一次執行時間],max_worker_time /1000 as [最大執行時間(ms)],

substring(qt.text,qs.statement_start_offset/2+1,

(case when qs.statement_end_offset = -1

then datalength(qt.text)

else qs.statement_end_offset end -qs.statement_start_offset)/2 + 1)

as [使用cpu的語法], qt.text [完整語法],

qt.dbid, dbname=db_name(qt.dbid),

qt.objectid,object_name(qt.objectid,qt.dbid) objectname

from sys.dm_exec_query_stats qs with(nolock)

where execution_count>1

order by total_worker_time desc

--平均耗cpu最多的前個sql:

select top 20

total_worker_time/1000 as [總消耗cpu 時間(ms)],execution_count [執行次數],

qs.total_worker_time/qs.execution_count/1000 as [平均消耗cpu 時間(ms)],

last_execution_time as [最後一次執行時間],min_worker_time /1000 as [最小執行時間(ms)],

max_worker_time /1000 as [最大執行時間(ms)],

substring(qt.text,qs.statement_start_offset/2+1,

(case when qs.statement_end_offset = -1

then datalength(qt.text)

else qs.statement_end_offset end -qs.statement_start_offset)/2 + 1)

as [使用cpu的語法], qt.text [完整語法],

qt.dbid, dbname=db_name(qt.dbid),

qt.objectid,object_name(qt.objectid,qt.dbid) objectname

from sys.dm_exec_query_stats qs with(nolock)

where execution_count>1

order by (qs.total_worker_time/qs.execution_count/1000) desc

檢視SQLServer最耗資源時間的SQL語句

執行最慢的sql語句 select total elapsed time execution count 1000 n 平均時間ms total elapsed time 1000 n 總花費時間ms total worker time 1000 n 所用的cpu總時間ms total physic...

檢視SQLServer最耗資源時間的SQL語句

1.找出執行時間最長的10條sql 適用於sql server 2005及其以上版本 如果想對sql作篩選,可將 not like fetch 換成 like user 就可以找出sql語句中含有user關鍵字的sql 2 找出執行最慢的sql語句 適用於sql server 2005及其以上版本 ...

sqlserver定位消耗資源比較高的sql

sqlserver檢視消耗資源的sql 具體如下 可以使用sql 快速定位資料庫裡cpu,io 消耗比較高的sql 1.定位消耗cpu,io 最高的程序資訊 selectspid,dbid,cpu,physical io,login time,last batch,status,hostname,p...