期望邏輯:
select sql,max(花費時間),avg(花費時間),min(花費時間),count(*) 執行次數
from 查詢LOG表
where 執行日期 between '2020-01-01' and '2020-12-31'
group by sql
結果:
sql | 最大花費時間 | 最小花費時間 | 平均花費時間 | 執行次數 |
---|---|---|---|---|
select * from table1 where a=1 | 500 | 0.25 | 1.25 | 4500 |
select count(1) from table2 where b=5 | 200 | 0.35 | 2.25 | 500 |
我有這個可以查目前鎖定的SQL~
至於你要的~你可以在Google在找看看~
SELECT
r.scheduler_id as 排程器識別碼,
status as 要求的狀態,
r.session_id as SPID,
r.blocking_session_id as BlkBy,
substring(
ltrim(q.text),
r.statement_start_offset/2+1,
(CASE
WHEN r.statement_end_offset = -1
THEN LEN(CONVERT(nvarchar(MAX), q.text)) * 2
ELSE r.statement_end_offset
END - r.statement_start_offset)/2)
AS [正在執行的 T-SQL 命令],
r.cpu_time as [CPU Time(ms)],
r.start_time as [開始時間],
r.total_elapsed_time as [執行總時間],
r.reads as [讀取數],
r.writes as [寫入數],
r.logical_reads as [邏輯讀取數],
-- q.text, /* 完整的 T-SQL 指令碼 */
d.name as [資料庫名稱],
c.client_net_address as [用戶端IP地址],
c.client_tcp_port as [用戶端Port]
FROM
sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS q
LEFT JOIN sys.databases d ON (r.database_id=d.database_id)
LEFT JOIN sys.dm_exec_connections c ON ( r.connection_id = c.connection_id )
ORDER BY
r.total_elapsed_time desc
這是我找Google搜尋到的~
但不確定這樣是否Ok你要的@@
SELECT st.text as [Sql]
,max(datediff(s,qs.creation_time,qs.last_execution_time)) as [最大花費時間]
,min(datediff(s,qs.creation_time,qs.last_execution_time)) as [最小花費時間]
,avg(datediff(s,qs.creation_time,qs.last_execution_time)) as [平均花費時間]
,Count(0) Use_Count
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle) st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
where qs.creation_time between '2021/1/6 06:00:00' and '2021/1/6 06:10:00'
group by st.text
參考來源:https://www.cnblogs.com/seusoftware/p/4826958.html?fbclid=IwAR3sEISn_wfSZ-KrV7GIewuWpzWIqjcAAQhmIa5yYNekxLUzcPgiNtaS_N0
【五. 历史SQL语句记录】