sqlserver查詢執行過的SQL記錄

2021-10-24 17:56:52 字數 1516 閱讀 1443

說明:僅支援sql server2008及以上版本

declare @starttime datetime,@endtime datetime;

set @starttime='2020-10-14 00:00:00';

set @endtime='2020-10-15 00:00:00';

select top 1000 qs.creation_time,substring(st.text,(qs.statement_start_offset/2)+1,((case qs.statement_end_offset when -1 then datalength(st.text) else qs.statement_end_offset end - qs.statement_start_offset)/2) + 1) as statement_text,st.text,qs.total_worker_time,qs.last_worker_time,qs.max_worker_time,qs.min_worker_time

--and st.text like '%%'

order by qs.creation_time desc

select top 1000 qs.creation_time,st.text from

and st.text not like 'select%'

order by qs.creation_time desc

該檢視sys.dm_exec_query_stats存放的就是當前所有執行計畫的詳細資訊,比如某條執行計畫共佔cpu多少等等。因為該檢視對編譯次數、占用cpu資源總量、執行次數等都進行了詳細的記錄,所以,可以說是優化db伺服器cpu的一大利器。

關於cpu時間的單位,說明如下:

欄位名資料型別描述備註

total_worker_timebigint此計畫自編譯以來執行所用的 cpu 時間總量(以微秒為單位報告,但僅精確到毫秒)。

對於本機編譯的儲存過程,如果許多執行所用的時間都不到 1 毫秒,則 total_worker_time 可能不精確。

對於啟用統計資訊收集時的本機編譯的儲存過程, 以毫秒為單位收集工作執行緒時間。

如果查詢執行的時間不到1毫秒, 則該值將為0。

last_worker_timebigint上次執行計畫所用的 cpu 時間(以微秒為單位報告,但僅精確到毫秒)。 

min_worker_timebigint此計畫在單次執行期間所用的最小 cpu 時間(以微秒為單位報告,但僅精確到毫秒)。

max_worker_timebigint此計畫在單次執行期間所用的最大 cpu 時間(以微秒為單位報告,但僅精確到毫秒)。

SQLSERVER 執行過的語句查詢

select top30000total worker time 1000as 總消耗cpu 時間 ms execution count 執行次數 qs.total worker time qs.execution count 1000as 平均消耗cpu 時間 ms last execution ...

SQLSERVER 執行過的語句查詢

select top 30000 total worker time 1000 as 總消耗cpu 時間 ms execution count 執行次數 qs.total worker time qs.execution count 1000 as 平均消耗cpu 時間 ms last execut...

SQLSERVER 執行過的語句查詢

select top30000 total worker time 1000as 總消耗cpu 時間 ms execution count 執行次數 qs.total worker time qs.execution count 1000as 平均消耗cpu 時間 ms last execution...