@@ -321,10 +321,28 @@ BEGIN
321321 )
322322 FROM sys.dm_os_schedulers AS s
323323 WHERE s.status = '' VISIBLE ONLINE''
324- AND s.is_online = 1;
324+ AND s.is_online = 1
325+ OPTION(RECOMPILE);
325326 ' ;
326327
327328 END
329+ ELSE
330+ BEGIN
331+ SELECT
332+ @total_cpu_usage = 0 ,
333+ @get_thread_time_ms + =
334+ N'
335+ SELECT
336+ @thread_time_ms =
337+ CONVERT
338+ (
339+ FLOAT,
340+ SUM(s.total_worker_time / 1000.)
341+ )
342+ FROM sys.dm_exec_query_stats AS s
343+ OPTION(RECOMPILE);
344+ ' ;
345+ END
328346
329347 RAISERROR (' Now starting diagnostic analysis' ,10 ,1 ) WITH NOWAIT ;
330348
@@ -1335,7 +1353,7 @@ BEGIN
13351353 END ;
13361354
13371355
1338- IF @total_cpu_usage = 1
1356+ IF @total_cpu_usage IN ( 0 , 1 )
13391357 BEGIN
13401358 EXEC sys .sp_executesql
13411359 @get_thread_time_ms,
@@ -2531,7 +2549,7 @@ If one of them is a lead blocker, consider killing that query.'' AS HowToStopit,
25312549 WAITFOR TIME @FinishSampleTimeWaitFor;
25322550 END ;
25332551
2534- IF @total_cpu_usage = 1
2552+ IF @total_cpu_usage IN ( 0 , 1 )
25352553 BEGIN
25362554 EXEC sys .sp_executesql
25372555 @get_thread_time_ms,
@@ -3434,10 +3452,15 @@ If one of them is a lead blocker, consider killing that query.'' AS HowToStopit,
34343452 48 ,
34353453 254 ,
34363454 N ' Informational' ,
3437- N ' Thread Time will always be 0 in versions prior to SQL Server 2016' ,
3438- N ' sys.dm_os_schedulers doesn'' t have total_cpu_usage_ms' ,
3455+ N ' Thread Time comes from the plan cache in versions earlier than 2016, and is not as reliable' ,
3456+ N ' The oldest plan in your cache is from ' +
3457+ CONVERT (nvarchar (30 ), MIN (s .creation_time )) +
3458+ N ' and your server was last restarted on ' +
3459+ CONVERT (nvarchar (30 ), MAX (o .sqlserver_start_time )),
34393460 N ' https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-os-schedulers-transact-sql'
3440-
3461+ FROM sys .dm_exec_query_stats AS s
3462+ CROSS JOIN sys .dm_os_sys_info AS o
3463+ OPTION (RECOMPILE );
34413464 END /* Let people on <2016 know about the thread time column */
34423465
34433466 /* If we didn't find anything, apologize. */
0 commit comments