sql server发生的等待统计信息

Posted 菡萏No.1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql server发生的等待统计信息相关的知识,希望对你有一定的参考价值。

SELECT * FROM sys.dm_os_wait_stats
WHERE wait_time_ms>0
ORDER BY wait_time_ms DESC

只有sql server重启的时候才会自动清除等待统计信息,可以通过 DBCC SQLPERF("sys.dm_os_wait_stats", CLEAR)命令清除统计信息

通过一个临时表和waitfor delay 语句跟踪一段时间内发生的变化,从而判断当前正在发生的等待。

IF OBJECT_ID(tempdb..#wait_stats) IS NOT NULL
DROP TABLE #wait_stats

SELECT * INTO #wait_stats
FROM sys.dm_os_wait_stats
WAITFOR DELAY 00:00:05

SELECT ws1.wait_type,
    ws2.waiting_tasks_count - ws1.waiting_tasks_count
    AS waiting_tasks_count,
    ws2.wait_time_ms - ws1.wait_time_ms AS wait_time_ms,
CASE WHEN ws2.max_wait_time_ms > ws1.max_wait_time_ms 
THEN ws2.max_wait_time_ms
ELSE ws1.max_wait_time_ms
END AS max_wait_time_ms,
    ws2.signal_wait_time_ms - ws1.signal_wait_time_ms
    AS signal_wait_time_ms,
    (ws2.wait_time_ms - ws1.wait_time_ms)-(ws2.signal_wait_time_ms - ws1.signal_wait_time_ms)
    AS resource_wait_time_ms
FROM sys.dm_os_wait_stats AS ws2
JOIN #wait_stats AS ws1 ON ws1.wait_type=ws2.wait_type
WHERE ws2.wait_time_ms - ws1.wait_time_ms > 0
ORDER BY ws2.wait_time_ms - ws1.wait_time_ms DESC

在查看资源等待时间和信号等待时间的差别的时候,最好观察两者在总时间内所占的比例。

SELECT SUM(signal_wait_time_ms) AS total_signal_wait_time_ms,
    SUM(wait_time_ms - signal_wait_time_ms) AS resource_wait_time_ms,
    SUM(signal_wait_time_ms) * 1.0 / SUM(wait_time_ms) * 100 AS signal_wait_percent,
   SUM(wait_time_ms - signal_wait_time_ms) * 1.0 / SUM(wait_time_ms) * 100 AS resource_wait_percent
FROM sys.dm_os_wait_stats

 

以上是关于sql server发生的等待统计信息的主要内容,如果未能解决你的问题,请参考以下文章

不正确的 SQL Server 统计信息

SQL Server 2016新特性:Live Query Statistics

SQL Server 统计信息

sql server 统计信息

SQL SERVER的统计信息

Microsoft SQL Server 代码片段收集