@@ -241,18 +241,288 @@ FROM sys.dm_exec_cached_plans
241
241
-- WHERE objtype = 'Adhoc' AND usecounts = 1
242
242
GROUP BY objtype, cacheobjtype;
243
243
244
+ -- ====================================================================================================
244
245
245
246
select top 1 collection_time as time
246
247
,available_physical_memory_gb* 1024 as decimal (20 ,0 )) as available_physical_memory
247
248
from DBA.[dbo].[dm_os_sys_memory]
248
249
order by collection_time desc
249
250
250
- select top 1 collection_time as time , (free_memory_kb* 1 .0 )/ 1024 as [Available Memory]
251
+ -- ====================================================================================================
252
+ DECLARE @server_name varchar (256 );
253
+ set @server_name = ' MSI' ;
254
+
255
+ select collection_time as time , (available_physical_memory_kb* 1 .0 )/ 1024 as [Available Memory]
251
256
from DBA.[dbo].[dm_os_sys_memory]
257
+ where 1 = 1
258
+ -- and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
259
+ -- and pc.server_name = @server_name
252
260
order by collection_time desc
253
261
262
+ -- ====================================================================================================
263
+
254
264
select *
255
265
from DBA..WhoIsActive_ResultSets as r
256
266
where r .collection_time >= ' 2020年08月23日 16:30:00'
257
267
258
- select getdate ()
268
+ -- ====================================================================================================
269
+
270
+ DECLARE @server_name varchar (256 );
271
+ set @server_name = ' MSI' ;
272
+
273
+ select master .dbo .local2utc (pc .collection_time ) as time , pc .cntr_value as [Available MBytes]
274
+ from DBA .dbo .dm_os_performance_counters_nonsql as pc
275
+ where 1 = 1
276
+ -- and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
277
+ and pc .server_name = @server_name
278
+ and pc.[object_name] = ' Memory'
279
+ and pc .counter_name = ' Available MBytes'
280
+
281
+ -- ====================================================================================================
282
+
283
+ DECLARE @server_name varchar (256 );
284
+ set @server_name = ' MSI' ;
285
+
286
+ select master .dbo .local2utc (pc .collection_time ) as time , pc .counter_name , CEILING (pc .cntr_value ) as cntr_value
287
+ from DBA .dbo .dm_os_performance_counters_nonsql as pc
288
+ where 1 = 1
289
+ -- and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
290
+ and pc .server_name = @server_name
291
+ and pc.[object_name] = ' Memory'
292
+ and pc .counter_name in (' Pages Input/Sec' ,' Pages/Sec' )
293
+
294
+ -- ====================================================================================================
295
+
296
+ DECLARE @server_name varchar (256 );
297
+ set @server_name = ' MSI' ;
298
+
299
+ select master .dbo .local2utc (pc .collection_time ) as time , pc .counter_name , CEILING (pc .cntr_value ) as cntr_value
300
+ from DBA .dbo .dm_os_performance_counters_nonsql as pc
301
+ where 1 = 1
302
+ -- and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
303
+ and pc .server_name = @server_name
304
+ and pc.[object_name] = ' Paging File'
305
+ and pc .counter_name in (' % Usage' ,' % Usage Peak' )
306
+
307
+
308
+ select top 1 collection_time as time , (available_physical_memory_kb* 1 .0 )/ 1024 as [Available Memory]
309
+ from DBA.[dbo].[dm_os_sys_memory]
310
+ order by collection_time desc
311
+
312
+ -- ====================================================================================================
313
+
314
+ DECLARE @server_name varchar (256 );
315
+ set @server_name = ' MSI' ;
316
+
317
+ select master .dbo .local2utc (pc .collection_time ) as time , pc .instance_name + ' \ --- ' + pc .counter_name as instance_name, CAST (pc .cntr_value AS FLOAT ) as cntr_value
318
+ from DBA .dbo .dm_os_performance_counters_nonsql as pc
319
+ where 1 = 1
320
+ -- and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
321
+ and collection_time >= DATEADD (hour,- 2 ,getdate ())
322
+ and pc .server_name = @server_name
323
+ and pc.[object_name] = ' LogicalDisk'
324
+ and counter_name in (' Avg. Disk sec/Read' ,' Avg. Disk sec/Write' )
325
+ and ( instance_name <> ' _Total' and instance_name not like ' HarddiskVolume%' )
326
+
327
+ -- ====================================================================================================
328
+
329
+ select * from [dbo].[CounterDetails]
330
+ where ObjectName = ' Network Interface'
331
+ and CounterName in (' Avg. Disk sec/Read' ,' Avg. Disk sec/Write' )
332
+
333
+ -- ====================================================================================================
334
+
335
+ DECLARE @server_name varchar (256 );
336
+ set @server_name = ' MSI' ;
337
+
338
+ select -- master.dbo.local2utc(pc.collection_time) as time, pc.instance_name + '\ --- ' + pc.counter_name as instance_name, CAST(pc.cntr_value AS FLOAT) as cntr_value
339
+ *
340
+ from DBA .dbo .dm_os_performance_counters_nonsql as pc
341
+ where 1 = 1
342
+ -- and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
343
+ and collection_time >= DATEADD (hour,- 2 ,getdate ())
344
+ and pc .server_name = @server_name
345
+ and pc.[object_name] = ' LogicalDisk'
346
+ and counter_name in (' Disk Bytes/sec' )
347
+ and ( instance_name <> ' _Total' and instance_name not like ' HarddiskVolume%' )
348
+
349
+
350
+ -- ====================================================================================================
351
+
352
+ DECLARE @server_name varchar (256 );
353
+ set @server_name = ' MSI' ;
354
+
355
+ select master .dbo .local2utc (pc .collection_time ) as time , pc .instance_name -- + '\ --- ' + pc.counter_name as instance_name
356
+ ,CAST (pc .cntr_value AS FLOAT ) as cntr_value
357
+ -- *
358
+ from DBA .dbo .dm_os_performance_counters_nonsql as pc
359
+ where 1 = 1
360
+ -- and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
361
+ and collection_time >= DATEADD (hour,- 2 ,getdate ())
362
+ and pc .server_name = @server_name
363
+ and pc.[object_name] = ' Network Interface'
364
+ and counter_name in (' Bytes Total/sec' )
365
+ -- and ( instance_name <> '_Total' and instance_name not like 'HarddiskVolume%' )
366
+
367
+ -- ====================================================================================================
368
+ GO
369
+
370
+
371
+ -- set nocount on;
372
+ DECLARE @server_name varchar (256 );
373
+ DECLARE @start_time datetime2 ;
374
+ DECLARE @end_time datetime2 ;
375
+ set @server_name = ' MSI' ;
376
+ -- set @start_time = master.dbo.utc2local($__timeFrom());
377
+ -- set @end_time = master.dbo.utc2local($__timeTo());
378
+ set @start_time = DATEADD (MINUTE,- 60 ,GETDATE ());
379
+ set @end_time = GETDATE ();
380
+
381
+ DECLARE @verbose bit = 1 ;
382
+
383
+ if OBJECT_ID (' tempdb..#wait_stats_range' ) is not null
384
+ drop table tempdb..#wait_stats_range;
385
+
386
+ select IDENTITY (int ,1 ,1 ) as id, point_in_time
387
+ into #wait_stats_range
388
+ from (
389
+ SELECT si .wait_stats_cleared_time as point_in_time
390
+ FROM [DBA].dbo .dm_os_sys_info AS si
391
+ WHERE 1 = 1
392
+ -- and pc.server_name = @server_name
393
+ and collection_time BETWEEN @start_time AND @end_time
394
+ --
395
+ union
396
+ --
397
+ select point_in_time
398
+ from (values (@start_time),(@end_time)) Times (point_in_time)
399
+ ) as ranges
400
+ order by point_in_time asc ;
401
+
402
+ if @verbose = 1
403
+ begin
404
+ select * from #wait_stats_range;
405
+ select count (distinct CollectionTime) as WaitStats_Sample_Counts from [DBA].[dbo].[WaitStats];
406
+ end
407
+
408
+ IF OBJECT_ID (' tempdb..#Wait_Stats_Delta' ) IS NOT NULL
409
+ DROP TABLE #Wait_Stats_Delta;
410
+ CREATE TABLE #Wait_Stats_Delta
411
+ (
412
+ -- [id_T1] [int] NULL,
413
+ -- [id_T2] [int] NULL,
414
+ -- [point_in_time_T1] [datetime2](7) NULL,
415
+ -- [point_in_time_T2] [datetime2](7) NULL,
416
+ -- [CollectionTime_T1] [datetime2](7) NULL,
417
+ -- [CollectionTime_T2] [datetime2](7) NULL,
418
+ [CollectionTime] [datetime2](7 ) NOT NULL ,
419
+ [CollectionTime_Duration_Seconds] [int] NOT NULL ,
420
+ [WaitType] [nvarchar](120 ) NOT NULL ,
421
+ [Wait_S] [decimal](15 , 2 ) NULL ,
422
+ [Resource_S] [decimal](15 , 2 ) NULL ,
423
+ [Signal_S] [decimal](15 , 2 ) NULL ,
424
+ [WaitCount] [bigint] NULL ,
425
+ [Percentage] [decimal](10 ,2 ) NULL ,
426
+ [AvgWait_S] [decimal](35 , 22 ) NULL ,
427
+ [AvgRes_S] [decimal](35 , 22 ) NULL ,
428
+ [AvgSig_S] [decimal](35 , 22 ) NULL
429
+ )
430
+
431
+ declare @l_id int
432
+ ,@l_point_in_time datetime2
433
+ ,@l_counter int = 1
434
+ ,@l_counter_max int ;
435
+
436
+ select @l_counter_max = max (id) from #wait_stats_range;
437
+
438
+ -- if @verbose = 1
439
+ -- select [@l_counter] = @l_counter, [@l_counter_max] = @l_counter_max;
440
+
441
+ while @l_counter < @l_counter_max -- execute for N-1 times
442
+ begin
443
+ select @l_point_in_time = point_in_time from #wait_stats_range as tr where tr .id = @l_counter;
444
+
445
+ if @verbose = 1
446
+ select [@l_counter] = @l_counter, [@l_counter_max] = @l_counter_max, [@l_point_in_time] = @l_point_in_time;
447
+
448
+ ;WITH T1 AS (
449
+ select id = tr .id , point_in_time = tr .point_in_time ,
450
+ ws .CollectionTime ,
451
+ ws .WaitType ,
452
+ ws .Wait_S , ws .Resource_S , ws .Signal_S , ws .WaitCount , ws .Percentage , ws .AvgWait_S , ws .AvgRes_S , ws .AvgSig_S
453
+ from [DBA].[dbo].[WaitStats] AS ws with (nolock ) full join #wait_stats_range as tr on tr .id = @l_counter
454
+ where ws .CollectionTime = ( case when @l_counter = 1
455
+ then (select MIN (wsi .CollectionTime ) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock ) where wsi .CollectionTime >= tr .point_in_time )
456
+ when @l_counter = @l_counter_max - 1
457
+ then ISNULL ( (select MIN (wsi .CollectionTime ) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock ) where wsi .CollectionTime >= tr .point_in_time ),
458
+ (select MAX (wsi .CollectionTime ) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock ) where wsi .CollectionTime <= tr .point_in_time )
459
+ )
460
+ else (select MIN (wsi .CollectionTime ) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock ) where wsi .CollectionTime > tr .point_in_time )
461
+ end
462
+ )
463
+ )
464
+ ,T2 AS (
465
+ select id = tr .id , point_in_time = tr .point_in_time ,
466
+ ws .CollectionTime ,
467
+ ws .WaitType ,
468
+ ws .Wait_S , ws .Resource_S , ws .Signal_S , ws .WaitCount , ws .Percentage , ws .AvgWait_S , ws .AvgRes_S , ws .AvgSig_S
469
+ from [DBA].[dbo].[WaitStats] AS ws with (nolock ) full join #wait_stats_range as tr on tr .id = @l_counter+ 1
470
+ where ws .CollectionTime = ( case when @l_counter = 1 AND (@l_counter <> (@l_counter_max - 1 ))
471
+ then (select MAX (wsi .CollectionTime ) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock ) where wsi .CollectionTime < tr .point_in_time )
472
+ when @l_counter = 1 AND (@l_counter = (@l_counter_max - 1 ))
473
+ then (select MAX (wsi .CollectionTime ) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock ) where wsi .CollectionTime <= tr .point_in_time )
474
+ when @l_counter = @l_counter_max - 1
475
+ then (select MAX (wsi .CollectionTime ) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock ) where wsi .CollectionTime <= tr .point_in_time )
476
+ else (select MAX (wsi .CollectionTime ) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock ) where wsi .CollectionTime < tr .point_in_time )
477
+ end
478
+ )
479
+ )
480
+ ,T_Waits_Delta AS (
481
+ SELECT CollectionTime, CollectionTime_Duration_Seconds, WaitType, Wait_S, Resource_S, Signal_S, WaitCount,
482
+ [Percentage],
483
+ AvgWait_S, AvgRes_S, AvgSig_S
484
+ -- INTO tempdb..Wait_Stats_Delta
485
+ FROM (
486
+ SELECT -- id_T1 = T1.id, id_T2 = T2.id,
487
+ -- point_in_time_T1 = T1.point_in_time, point_in_time_T2 = T2.point_in_time,
488
+ -- CollectionTime_T1 = T1.CollectionTime, CollectionTime_T2 = T2.CollectionTime,
489
+ CollectionTime = T2 .CollectionTime ,
490
+ CollectionTime_Duration_Seconds = DATEDIFF (second,T1 .CollectionTime ,T2 .CollectionTime ),
491
+ WaitType = COALESCE (T1 .WaitType ,T2 .WaitType ),
492
+ Wait_S = ISNULL (T2 .Wait_S ,0 .0 ) - ISNULL (T1 .Wait_S ,0 .0 ),
493
+ Resource_S = ISNULL (T2 .Resource_S ,0 .0 ) - ISNULL (T1 .Resource_S ,0 .0 ),
494
+ Signal_S = ISNULL (T2 .Signal_S ,0 .0 ) - ISNULL (T1 .Signal_S ,0 .0 ),
495
+ WaitCount = ISNULL (T2 .WaitCount ,0 .0 ) - ISNULL (T1 .WaitCount ,0 .0 ),
496
+ [Percentage] = NULL , -- ISNULL(T2.Wait_S,0.0) - ISNULL(T1.Wait_S,0.0),
497
+ AvgWait_S = CASE WHEN (ISNULL (T2 .WaitCount ,0 .0 ) - ISNULL (T1 .WaitCount ,0 .0 )) = 0 THEN (ISNULL (T2 .Wait_S ,0 .0 ) - ISNULL (T1 .Wait_S ,0 .0 ))
498
+ ELSE (ISNULL (T2 .Wait_S ,0 .0 ) - ISNULL (T1 .Wait_S ,0 .0 )) / (ISNULL (T2 .WaitCount ,0 .0 ) - ISNULL (T1 .WaitCount ,0 .0 ))
499
+ END ,
500
+ AvgRes_S = CASE WHEN (ISNULL (T2 .WaitCount ,0 .0 ) - ISNULL (T1 .WaitCount ,0 .0 )) = 0 THEN (ISNULL (T2 .Resource_S ,0 .0 ) - ISNULL (T1 .Resource_S ,0 .0 ))
501
+ ELSE (ISNULL (T2 .Resource_S ,0 .0 ) - ISNULL (T1 .Resource_S ,0 .0 )) / (ISNULL (T2 .WaitCount ,0 .0 ) - ISNULL (T1 .WaitCount ,0 .0 ))
502
+ END ,
503
+ AvgSig_S = CASE WHEN (ISNULL (T2 .WaitCount ,0 .0 ) - ISNULL (T1 .WaitCount ,0 .0 )) = 0 THEN (ISNULL (T2 .Signal_S ,0 .0 ) - ISNULL (T1 .Signal_S ,0 .0 ))
504
+ ELSE (ISNULL (T2 .Signal_S ,0 .0 ) - ISNULL (T1 .Signal_S ,0 .0 )) / (ISNULL (T2 .WaitCount ,0 .0 ) - ISNULL (T1 .WaitCount ,0 .0 ))
505
+ END
506
+ FROM T1 full outer join T2 on T2 .WaitType = T1 .WaitType
507
+ ) as waits
508
+ WHERE 1 = 1
509
+ AND CollectionTime_Duration_Seconds > 0 .0
510
+ -- AND Wait_S >= 0.0
511
+ )
512
+ INSERT #Wait_Stats_Delta
513
+ SELECT CollectionTime, CollectionTime_Duration_Seconds, WaitType, Wait_S, Resource_S, Signal_S, WaitCount,
514
+ [Percentage] = (Wait_S* 100 .0 )/ Total_Wait_S,
515
+ AvgWait_S, AvgRes_S, AvgSig_S
516
+ FROM T_Waits_Delta as d
517
+ JOIN (select sum (i .Wait_S ) as Total_Wait_S from T_Waits_Delta as i) as t ON 1 = 1
518
+ ORDER BY Wait_S DESC ;
519
+
520
+ set @l_counter + = 1 ;
521
+ end
522
+
523
+ select CollectionTime as time , CollectionTime_Duration_Seconds as Duration_S, WaitType, Wait_S, Resource_S, Signal_S, WaitCount, [Percentage], AvgWait_S, AvgRes_S, AvgSig_S
524
+ from #Wait_Stats_Delta;
525
+
526
+
527
+ GO
528
+
0 commit comments