-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAzSqlDbPerformanceTest.sql
797 lines (688 loc) · 31.1 KB
/
AzSqlDbPerformanceTest.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/*
Performance test setup.
WARNING: DO NOT RUN THIS IN PRODUCTION!
1. create database.
a. local database with defaults. Large enough that the file doesn't have to grow. 1 GB should suffice.
b. Azure database, every SKU possible. From S0 to business critical. To keep costs under control, for the non-serverless versions use 4, 8, 16 and 32 core DB's
2. Run the scripts below with SqlStress or SSMS. Every script has a pointer where to run it.
The scripts you need to run with SQLStress: for the local machine on the local machine.
For the Azure DB on a 4 Core 16 GB Windows VM in the same subnet as the database with private endpoints enabled.
It's up to you to choose between a B or D series VM. If you choose a B-series, make sure to run the workload more than once to get comparable results.
Don't forget to empty the cache before you start! This means, DO NOT RUN THIS IN PRODUCTION!
Check the connection to make sure you're using the local subnet and not the internet route.
3. Check the wait statistics to see where bottlenecks may occur
4. Check the IO stats to see where bottlenecks may occur
5. Check the portal for information
6. Check the Query Store for information
7. Log the data for future reference
8. DO NOT RUN THIS IN PRODUCTION
This script contains everything you need to do a stupid load test. Adapt it to whatever you see fit in your situation.
Maybe 10 tables to test join performance? Or 250 columns to test wide tables?
The datatypes are all over the place, this is by design to create a messy table.
REMINDER: DO NOT RUN THIS IN PRODUCTION
*/
/* system description */
SELECT physical_memory_kb/1024/1024 AS [GB memory available],
committed_target_kb/1024/1024 AS [GB sql memory],
cpu_count AS [cpu cores],
socket_count,
numa_node_count,
cores_per_socket
max_workers_count,
scheduler_count,
scheduler_total_count,
virtual_machine_type_desc,
@@VERSION AS [sql server version]
FROM sys.dm_os_sys_info
/* Check the resource governance settings */
select *
from sys.dm_user_db_resource_governance
/* Check the workers */
select * from sys.dm_os_workers
/* Check the schedulers */
select * from sys.dm_os_schedulers
/* Check any running requests */
select *
from sys.dm_exec_requests
SELECT * FROM sys.sysprocesses
/* Check your connection (you want a local IP, no internet based IP) */
SELECT client_net_address, local_net_address
FROM sys.dm_exec_connections
/* Create the testing table */
SET NOCOUNT ON
DROP TABLE IF EXISTS dbo.PerfTest;
CREATE TABLE dbo.PerfTest (
ID int IDENTITY(1,1) PRIMARY KEY NOT NULL,
val1 INT NULL,
val2 VARCHAR(100) NULL,
val3 NVARCHAR(100) NULL,
val4 CHAR(50) NULL,
val5 NCHAR(50) NULL,
val6 DECIMAL(38,15) NULL,
val7 REAL NULL,
val8 MONEY NULL,
val9 BIGINT NULL,
val10 VARCHAR(MAX) NULL,
val11 NVARCHAR(MAX) NULL,
val12 CHAR(50) NULL,
val13 NCHAR(50) NULL,
val14 DECIMAL(38,15) NULL,
val15 REAL NULL,
val16 MONEY NULL
);
/* Add ridiculous indexes, just because we can */
CREATE NONCLUSTERED INDEX ncix_report1 ON dbo.PerfTest
(val1,val2,val3,val4)
INCLUDE
(val5, val6, val7, val8);
CREATE NONCLUSTERED INDEX ncix_report2 ON dbo.PerfTest
(val16, val15, val14, val13)
INCLUDE
(val12, val11, val10, val9);
/* Wait Stats Monitoring start */
/* Run this script only once, just before your start the insert data test! */
CREATE TABLE ws_Capture
(
wst_WaitType VARCHAR(150),
wst_WaitTime BIGINT,
wst_WaitingTasks BIGINT,
wst_SignalWaitTime BIGINT
)
INSERT INTO ws_Capture
SELECT
wait_type,
wait_time_ms,
waiting_tasks_count,
signal_wait_time_ms
FROM sys.dm_os_wait_stats
/* File latency monitoring start */
CREATE TABLE FileLatency
(
CaptureDateTime DATETIME,
[ReadLatency] BIGINT,
[WriteLatency] BIGINT,
[Latency] BIGINT,
[AvgBPerRead] BIGINT,
[AvgBPerWrite] BIGINT,
[AvgBPerTransfer] BIGINT,
[Drive] VARCHAR(10),
[DB] VARCHAR(150),
[sample_ms] BIGINT,
[num_of_reads] BIGINT,
[num_of_bytes_read] BIGINT,
[io_stall_read_ms] BIGINT,
[num_of_writes] BIGINT,
[num_of_bytes_written] BIGINT,
[io_stall_write_ms] BIGINT,
[io_stall] BIGINT,
[size_on_disk_MB]DECIMAL(12,6),
[physical_name] VARCHAR(250)
)
INSERT INTO FileLatency
(
CaptureDateTime,
ReadLatency,
WriteLatency,
Latency,
AvgBPerRead,
AvgBPerWrite,
AvgBPerTransfer,
Drive,
DB,
sample_ms,
num_of_reads,
num_of_bytes_read,
io_stall_read_ms,
num_of_writes,
num_of_bytes_written,
io_stall_write_ms,
io_stall,
size_on_disk_MB,
physical_name
)
SELECT
--virtual file latency
GETDATE() AS CaptureDateTime,
CASE WHEN [num_of_reads] = 0 THEN 0 ELSE ([io_stall_read_ms] / [num_of_reads]) END [ReadLatency],
CASE WHEN [io_stall_write_ms] = 0 THEN 0 ELSE ([io_stall_write_ms] / [num_of_writes]) END [WriteLatency],
CASE WHEN ([num_of_reads] = 0 AND [num_of_writes] = 0) THEN 0 ELSE ([io_stall] / ([num_of_reads] + [num_of_writes])) END [Latency],
--avg bytes per IOP
CASE WHEN [num_of_reads] = 0 THEN 0 ELSE ([num_of_bytes_read] / [num_of_reads]) END [AvgBPerRead],
CASE WHEN [io_stall_write_ms] = 0 THEN 0 ELSE ([num_of_bytes_written] / [num_of_writes]) END [AvgBPerWrite],
CASE WHEN ([num_of_reads] = 0 AND [num_of_writes] = 0) THEN 0 ELSE (([num_of_bytes_read] + [num_of_bytes_written]) / ([num_of_reads] + [num_of_writes])) END [AvgBPerTransfer],
LEFT([mf].[physical_name], 2) [Drive],
DB_NAME([vfs].[database_id]) [DB],
[vfs].[sample_ms],
[vfs].[num_of_reads],
[vfs].[num_of_bytes_read],
[vfs].[io_stall_read_ms],
[vfs].[num_of_writes],
[vfs].[num_of_bytes_written],
[vfs].[io_stall_write_ms],
[vfs].[io_stall],
[vfs].[size_on_disk_bytes] / 1024 / 1024. [size_on_disk_MB],
[mf].[physical_name]
FROM [sys].[dm_io_virtual_file_stats](NULL, NULL) AS vfs
JOIN [sys].database_files [mf]
ON [vfs].[database_id] = DB_ID()
AND [vfs].[file_id] = [mf].file_id
ORDER BY [Latency] DESC;
/* Insert data test */
/* 20000 iterations, 50 threads 25 on Basic to prevent monitoring threads to get killed*/
INSERT INTO dbo.PerfTest
(val1, val2,val3,val4,val5,val6,val7,val8,val9,val10,val11,val12,val13,val14,val15,val16)
SELECT RAND(), RAND(), RAND(), RAND(), RAND(), RAND(), RAND(), RAND(), RAND(), RAND(), RAND(), RAND(), RAND(), RAND(), RAND(), RAND();
/* Check the wait stats! */
SELECT
GETDATE() AS [DATETIME],
dm.wait_type AS WaitType,
dm.wait_time_ms - ws.wst_WaitTime AS WaitTime,
dm.waiting_tasks_count - ws.wst_WaitingTasks AS WaitingTasks,
dm.signal_wait_time_ms - ws.wst_SignalWaitTime AS SignalWaitTime
FROM sys.dm_os_wait_stats dm
INNER JOIN ws_Capture ws ON dm.wait_type = ws.wst_WaitType
where dm.wait_type NOT IN (
N'BROKER_EVENTHANDLER' ,N'BROKER_RECEIVE_WAITFOR',N'BROKER_TASK_STOP',N'BROKER_TO_FLUSH',N'BROKER_TRANSMITTER',N'CHECKPOINT_QUEUE',N'CHKPT',N'CLR_AUTO_EVENT',N'CLR_MANUAL_EVENT',N'CLR_SEMAPHORE'
,-- Maybe uncomment these four if you have mirroring issues
N'DBMIRROR_DBM_EVENT',N'DBMIRROR_EVENTS_QUEUE',N'DBMIRROR_WORKER_QUEUE',N'DBMIRRORING_CMD',N'DIRTY_PAGE_POLL',N'DISPATCHER_QUEUE_SEMAPHORE',N'EXECSYNC',N'FSAGENT',N'FT_IFTS_SCHEDULER_IDLE_WAIT',N'FT_IFTSHC_MUTEX',
-- Maybe uncomment these six if you have AG issues
N'HADR_CLUSAPI_CALL',N'HADR_FILESTREAM_IOMGR_IOCOMPLETION',N'HADR_LOGCAPTURE_WAIT',N'HADR_NOTIFICATION_DEQUEUE' ,N'HADR_TIMER_TASK',N'HADR_WORK_QUEUE',N'KSOURCE_WAKEUP',N'LAZYWRITER_SLEEP',N'LOGMGR_QUEUE',N'MEMORY_ALLOCATION_EXT',N'ONDEMAND_TASK_QUEUE',N'PREEMPTIVE_XE_GETTARGETSTATE',N'PWAIT_ALL_COMPONENTS_INITIALIZED',N'PWAIT_DIRECTLOGCONSUMER_GETNEXT',N'QDS_PERSIST_TASK_MAIN_LOOP_SLEEP'
,N'QDS_ASYNC_QUEUE',N'QDS_CLEANUP_STALE_QUERIES_TASK_MAIN_LOOP_SLEEP',N'QDS_SHUTDOWN_QUEUE',N'REDO_THREAD_PENDING_WORK',N'REQUEST_FOR_DEADLOCK_SEARCH',N'RESOURCE_QUEUE',N'SERVER_IDLE_CHECK',N'SLEEP_BPOOL_FLUSH',N'SLEEP_DBSTARTUP',N'SLEEP_DCOMSTARTUP',N'SLEEP_MASTERDBREADY',N'SLEEP_MASTERMDREADY',N'SLEEP_MASTERUPGRADED',N'SLEEP_MSDBSTARTUP',N'SLEEP_SYSTEMTASK',N'SLEEP_TASK',N'SLEEP_TEMPDBSTARTUP'
,N'SNI_HTTP_ACCEPT',N'SP_SERVER_DIAGNOSTICS_SLEEP',N'SQLTRACE_BUFFER_FLUSH',N'SQLTRACE_INCREMENTAL_FLUSH_SLEEP',N'SQLTRACE_WAIT_ENTRIES',N'WAIT_FOR_RESULTS',N'WAITFOR',N'WAITFOR_TASKSHUTDOWN',N'WAIT_XTP_RECOVERY',N'WAIT_XTP_HOST_WAIT',N'WAIT_XTP_OFFLINE_CKPT_NEW_LOG',N'WAIT_XTP_CKPT_CLOSE',N'XE_DISPATCHER_JOIN',N'XE_DISPATCHER_WAIT',N'XE_TIMER_EVENT', N'SOS_WORK_DISPATCHER')
and dm.wait_time_ms - ws.wst_WaitTime > 0
order by WaitTime DESC;
DROP TABLE ws_Capture;
/* Check the disk latency */
SELECT
--virtual file latency
GETDATE() AS CaptureDateTime,
F.CaptureDateTime as PrevCaptureDateTime,
CASE WHEN vfs.[num_of_reads] = 0 THEN 0 ELSE (vfs.[io_stall_read_ms] / vfs.[num_of_reads]) END - F.ReadLatency [ReadLatency],
CASE WHEN vfs.[io_stall_write_ms] = 0 THEN 0 ELSE (vfs.[io_stall_write_ms] / vfs.[num_of_writes]) END - F.WriteLatency [WriteLatency],
CASE WHEN (vfs.[num_of_reads] = 0 AND vfs.[num_of_writes] = 0) THEN 0 ELSE (vfs.[io_stall] / (vfs.[num_of_reads] + vfs.[num_of_writes])) END - F.Latency [Latency],
--avg bytes per IOP
CASE WHEN vfs.[num_of_reads] = 0 THEN 0 ELSE (vfs.[num_of_bytes_read] / vfs.[num_of_reads]) END [AvgBPerRead],
CASE WHEN vfs.[io_stall_write_ms] = 0 THEN 0 ELSE (vfs.[num_of_bytes_written] / vfs.[num_of_writes]) END [AvgBPerWrite],
CASE WHEN (vfs.[num_of_reads] = 0 AND vfs.[num_of_writes] = 0) THEN 0 ELSE ((vfs.[num_of_bytes_read] + vfs.[num_of_bytes_written]) / (vfs.[num_of_reads] + vfs.[num_of_writes])) END [AvgBPerTransfer],
LEFT([mf].[physical_name], 2) [Drive],
DB_NAME([vfs].[database_id]) [DB],
[vfs].[sample_ms],
[vfs].[num_of_reads],
[vfs].[num_of_bytes_read],
[vfs].[io_stall_read_ms],
[vfs].[num_of_writes],
[vfs].[num_of_bytes_written],
[vfs].[io_stall_write_ms],
[vfs].[io_stall],
[vfs].[size_on_disk_bytes] / 1024 / 1024. [size_on_disk_MB],
[mf].[physical_name]
FROM [sys].[dm_io_virtual_file_stats](NULL, NULL) AS vfs
JOIN [sys].database_files [mf]
ON [vfs].[database_id] = DB_ID()
AND [vfs].[file_id] = [mf].file_id
JOIN FileLatency F on DB_NAME([vfs].[database_id]) = F.DB
ORDER BY [Latency] DESC;
DROP TABLE FileLatency
/*
Next step
*/
/* Wait Stats Monitoring start */
/* Run this script only once, just before your start the insert data test! */
CREATE TABLE ws_Capture
(
wst_WaitType VARCHAR(150),
wst_WaitTime BIGINT,
wst_WaitingTasks BIGINT,
wst_SignalWaitTime BIGINT
)
INSERT INTO ws_Capture
SELECT
wait_type,
wait_time_ms,
waiting_tasks_count,
signal_wait_time_ms
FROM sys.dm_os_wait_stats
/* File latency monitoring start */
CREATE TABLE FileLatency
(
CaptureDateTime DATETIME,
[ReadLatency] BIGINT,
[WriteLatency] BIGINT,
[Latency] BIGINT,
[AvgBPerRead] BIGINT,
[AvgBPerWrite] BIGINT,
[AvgBPerTransfer] BIGINT,
[Drive] VARCHAR(10),
[DB] VARCHAR(150),
[sample_ms] BIGINT,
[num_of_reads] BIGINT,
[num_of_bytes_read] BIGINT,
[io_stall_read_ms] BIGINT,
[num_of_writes] BIGINT,
[num_of_bytes_written] BIGINT,
[io_stall_write_ms] BIGINT,
[io_stall] BIGINT,
[size_on_disk_MB]DECIMAL(12,6),
[physical_name] VARCHAR(250)
)
INSERT INTO FileLatency
(
CaptureDateTime,
ReadLatency,
WriteLatency,
Latency,
AvgBPerRead,
AvgBPerWrite,
AvgBPerTransfer,
Drive,
DB,
sample_ms,
num_of_reads,
num_of_bytes_read,
io_stall_read_ms,
num_of_writes,
num_of_bytes_written,
io_stall_write_ms,
io_stall,
size_on_disk_MB,
physical_name
)
SELECT
--virtual file latency
GETDATE() AS CaptureDateTime,
CASE WHEN [num_of_reads] = 0 THEN 0 ELSE ([io_stall_read_ms] / [num_of_reads]) END [ReadLatency],
CASE WHEN [io_stall_write_ms] = 0 THEN 0 ELSE ([io_stall_write_ms] / [num_of_writes]) END [WriteLatency],
CASE WHEN ([num_of_reads] = 0 AND [num_of_writes] = 0) THEN 0 ELSE ([io_stall] / ([num_of_reads] + [num_of_writes])) END [Latency],
--avg bytes per IOP
CASE WHEN [num_of_reads] = 0 THEN 0 ELSE ([num_of_bytes_read] / [num_of_reads]) END [AvgBPerRead],
CASE WHEN [io_stall_write_ms] = 0 THEN 0 ELSE ([num_of_bytes_written] / [num_of_writes]) END [AvgBPerWrite],
CASE WHEN ([num_of_reads] = 0 AND [num_of_writes] = 0) THEN 0 ELSE (([num_of_bytes_read] + [num_of_bytes_written]) / ([num_of_reads] + [num_of_writes])) END [AvgBPerTransfer],
LEFT([mf].[physical_name], 2) [Drive],
DB_NAME([vfs].[database_id]) [DB],
[vfs].[sample_ms],
[vfs].[num_of_reads],
[vfs].[num_of_bytes_read],
[vfs].[io_stall_read_ms],
[vfs].[num_of_writes],
[vfs].[num_of_bytes_written],
[vfs].[io_stall_write_ms],
[vfs].[io_stall],
[vfs].[size_on_disk_bytes] / 1024 / 1024. [size_on_disk_MB],
[mf].[physical_name]
FROM [sys].[dm_io_virtual_file_stats](NULL, NULL) AS vfs
JOIN [sys].database_files [mf]
ON [vfs].[database_id] = DB_ID()
AND [vfs].[file_id] = [mf].file_id
ORDER BY [Latency] DESC;
/* select data test */
/* 2000 iterations, 15 threads */
set statistics time, io on
DECLARE @sec INT = (SELECT DATEPART(SECOND,SYSDATETIME()))
IF(@sec BETWEEN 0 AND 5)
SELECT top 100 val3, val5, val7 FROM dbo.PerfTest
ORDER BY val1
IF(@sec BETWEEN 6 AND 11)
SELECT top 100 val6, val8, val10 FROM dbo.PerfTest
ORDER BY val3
IF(@sec BETWEEN 12 AND 18)
SELECT top 100 val4, val6, val8 FROM dbo.PerfTest
ORDER BY val6
IF(@sec BETWEEN 19 AND 28)
SELECT top 100 val9, val11, val13 FROM dbo.PerfTest
ORDER BY val4
IF(@sec BETWEEN 29 AND 38)
SELECT top 100 val7, val9, val11 FROM dbo.PerfTest
ORDER BY val9
IF(@sec BETWEEN 29 AND 48)
SELECT top 100 val10, val12, val14 FROM dbo.PerfTest
ORDER BY val7
IF(@sec BETWEEN 49 AND 58)
SELECT top 100 val12, val14, val16 FROM dbo.PerfTest
ORDER BY val10
ELSE
SELECT top 100 val1,val3,val5 FROM dbo.PerfTest
ORDER BY val12
/* Check the wait stats! */
SELECT
GETDATE() AS [DATETIME],
dm.wait_type AS WaitType,
dm.wait_time_ms - ws.wst_WaitTime AS WaitTime,
dm.waiting_tasks_count - ws.wst_WaitingTasks AS WaitingTasks,
dm.signal_wait_time_ms - ws.wst_SignalWaitTime AS SignalWaitTime
FROM sys.dm_os_wait_stats dm
INNER JOIN ws_Capture ws ON dm.wait_type = ws.wst_WaitType
where dm.wait_type NOT IN (
N'BROKER_EVENTHANDLER' ,N'BROKER_RECEIVE_WAITFOR',N'BROKER_TASK_STOP',N'BROKER_TO_FLUSH',N'BROKER_TRANSMITTER',N'CHECKPOINT_QUEUE',N'CHKPT',N'CLR_AUTO_EVENT',N'CLR_MANUAL_EVENT',N'CLR_SEMAPHORE'
,-- Maybe uncomment these four if you have mirroring issues
N'DBMIRROR_DBM_EVENT',N'DBMIRROR_EVENTS_QUEUE',N'DBMIRROR_WORKER_QUEUE',N'DBMIRRORING_CMD',N'DIRTY_PAGE_POLL',N'DISPATCHER_QUEUE_SEMAPHORE',N'EXECSYNC',N'FSAGENT',N'FT_IFTS_SCHEDULER_IDLE_WAIT',N'FT_IFTSHC_MUTEX',
-- Maybe uncomment these six if you have AG issues
N'HADR_CLUSAPI_CALL',N'HADR_FILESTREAM_IOMGR_IOCOMPLETION',N'HADR_LOGCAPTURE_WAIT',N'HADR_NOTIFICATION_DEQUEUE' ,N'HADR_TIMER_TASK',N'HADR_WORK_QUEUE',N'KSOURCE_WAKEUP',N'LAZYWRITER_SLEEP',N'LOGMGR_QUEUE',N'MEMORY_ALLOCATION_EXT',N'ONDEMAND_TASK_QUEUE',N'PREEMPTIVE_XE_GETTARGETSTATE',N'PWAIT_ALL_COMPONENTS_INITIALIZED',N'PWAIT_DIRECTLOGCONSUMER_GETNEXT',N'QDS_PERSIST_TASK_MAIN_LOOP_SLEEP'
,N'QDS_ASYNC_QUEUE',N'QDS_CLEANUP_STALE_QUERIES_TASK_MAIN_LOOP_SLEEP',N'QDS_SHUTDOWN_QUEUE',N'REDO_THREAD_PENDING_WORK',N'REQUEST_FOR_DEADLOCK_SEARCH',N'RESOURCE_QUEUE',N'SERVER_IDLE_CHECK',N'SLEEP_BPOOL_FLUSH',N'SLEEP_DBSTARTUP',N'SLEEP_DCOMSTARTUP',N'SLEEP_MASTERDBREADY',N'SLEEP_MASTERMDREADY',N'SLEEP_MASTERUPGRADED',N'SLEEP_MSDBSTARTUP',N'SLEEP_SYSTEMTASK',N'SLEEP_TASK',N'SLEEP_TEMPDBSTARTUP'
,N'SNI_HTTP_ACCEPT',N'SP_SERVER_DIAGNOSTICS_SLEEP',N'SQLTRACE_BUFFER_FLUSH',N'SQLTRACE_INCREMENTAL_FLUSH_SLEEP',N'SQLTRACE_WAIT_ENTRIES',N'WAIT_FOR_RESULTS',N'WAITFOR',N'WAITFOR_TASKSHUTDOWN',N'WAIT_XTP_RECOVERY',N'WAIT_XTP_HOST_WAIT',N'WAIT_XTP_OFFLINE_CKPT_NEW_LOG',N'WAIT_XTP_CKPT_CLOSE',N'XE_DISPATCHER_JOIN',N'XE_DISPATCHER_WAIT',N'XE_TIMER_EVENT', N'SOS_WORK_DISPATCHER')
and dm.wait_time_ms - ws.wst_WaitTime > 0
order by WaitTime DESC;
DROP TABLE ws_Capture;
/* Check the disk latency */
SELECT
--virtual file latency
GETDATE() AS CaptureDateTime,
F.CaptureDateTime as PrevCaptureDateTime,
CASE WHEN vfs.[num_of_reads] = 0 THEN 0 ELSE (vfs.[io_stall_read_ms] / vfs.[num_of_reads]) END - F.ReadLatency [ReadLatency],
CASE WHEN vfs.[io_stall_write_ms] = 0 THEN 0 ELSE (vfs.[io_stall_write_ms] / vfs.[num_of_writes]) END - F.WriteLatency [WriteLatency],
CASE WHEN (vfs.[num_of_reads] = 0 AND vfs.[num_of_writes] = 0) THEN 0 ELSE (vfs.[io_stall] / (vfs.[num_of_reads] + vfs.[num_of_writes])) END - F.Latency [Latency],
--avg bytes per IOP
CASE WHEN vfs.[num_of_reads] = 0 THEN 0 ELSE (vfs.[num_of_bytes_read] / vfs.[num_of_reads]) END [AvgBPerRead],
CASE WHEN vfs.[io_stall_write_ms] = 0 THEN 0 ELSE (vfs.[num_of_bytes_written] / vfs.[num_of_writes]) END [AvgBPerWrite],
CASE WHEN (vfs.[num_of_reads] = 0 AND vfs.[num_of_writes] = 0) THEN 0 ELSE ((vfs.[num_of_bytes_read] + vfs.[num_of_bytes_written]) / (vfs.[num_of_reads] + vfs.[num_of_writes])) END [AvgBPerTransfer],
LEFT([mf].[physical_name], 2) [Drive],
DB_NAME([vfs].[database_id]) [DB],
[vfs].[sample_ms],
[vfs].[num_of_reads],
[vfs].[num_of_bytes_read],
[vfs].[io_stall_read_ms],
[vfs].[num_of_writes],
[vfs].[num_of_bytes_written],
[vfs].[io_stall_write_ms],
[vfs].[io_stall],
[vfs].[size_on_disk_bytes] / 1024 / 1024. [size_on_disk_MB],
[mf].[physical_name]
FROM [sys].[dm_io_virtual_file_stats](NULL, NULL) AS vfs
JOIN [sys].database_files [mf]
ON [vfs].[database_id] = DB_ID()
AND [vfs].[file_id] = [mf].file_id
JOIN FileLatency F on DB_NAME([vfs].[database_id]) = F.DB
ORDER BY [Latency] DESC;
DROP TABLE FileLatency
/* next step */
/* Wait Stats Monitoring start */
/* Run this script only once, just before your start the delete data test! */
CREATE TABLE ws_Capture
(
wst_WaitType VARCHAR(150),
wst_WaitTime BIGINT,
wst_WaitingTasks BIGINT,
wst_SignalWaitTime BIGINT
)
INSERT INTO ws_Capture
SELECT
wait_type,
wait_time_ms,
waiting_tasks_count,
signal_wait_time_ms
FROM sys.dm_os_wait_stats
/* File latency monitoring start */
CREATE TABLE FileLatency
(
CaptureDateTime DATETIME,
[ReadLatency] BIGINT,
[WriteLatency] BIGINT,
[Latency] BIGINT,
[AvgBPerRead] BIGINT,
[AvgBPerWrite] BIGINT,
[AvgBPerTransfer] BIGINT,
[Drive] VARCHAR(10),
[DB] VARCHAR(150),
[sample_ms] BIGINT,
[num_of_reads] BIGINT,
[num_of_bytes_read] BIGINT,
[io_stall_read_ms] BIGINT,
[num_of_writes] BIGINT,
[num_of_bytes_written] BIGINT,
[io_stall_write_ms] BIGINT,
[io_stall] BIGINT,
[size_on_disk_MB]DECIMAL(12,6),
[physical_name] VARCHAR(250)
)
INSERT INTO FileLatency
(
CaptureDateTime,
ReadLatency,
WriteLatency,
Latency,
AvgBPerRead,
AvgBPerWrite,
AvgBPerTransfer,
Drive,
DB,
sample_ms,
num_of_reads,
num_of_bytes_read,
io_stall_read_ms,
num_of_writes,
num_of_bytes_written,
io_stall_write_ms,
io_stall,
size_on_disk_MB,
physical_name
)
SELECT
--virtual file latency
GETDATE() AS CaptureDateTime,
CASE WHEN [num_of_reads] = 0 THEN 0 ELSE ([io_stall_read_ms] / [num_of_reads]) END [ReadLatency],
CASE WHEN [io_stall_write_ms] = 0 THEN 0 ELSE ([io_stall_write_ms] / [num_of_writes]) END [WriteLatency],
CASE WHEN ([num_of_reads] = 0 AND [num_of_writes] = 0) THEN 0 ELSE ([io_stall] / ([num_of_reads] + [num_of_writes])) END [Latency],
--avg bytes per IOP
CASE WHEN [num_of_reads] = 0 THEN 0 ELSE ([num_of_bytes_read] / [num_of_reads]) END [AvgBPerRead],
CASE WHEN [io_stall_write_ms] = 0 THEN 0 ELSE ([num_of_bytes_written] / [num_of_writes]) END [AvgBPerWrite],
CASE WHEN ([num_of_reads] = 0 AND [num_of_writes] = 0) THEN 0 ELSE (([num_of_bytes_read] + [num_of_bytes_written]) / ([num_of_reads] + [num_of_writes])) END [AvgBPerTransfer],
LEFT([mf].[physical_name], 2) [Drive],
DB_NAME([vfs].[database_id]) [DB],
[vfs].[sample_ms],
[vfs].[num_of_reads],
[vfs].[num_of_bytes_read],
[vfs].[io_stall_read_ms],
[vfs].[num_of_writes],
[vfs].[num_of_bytes_written],
[vfs].[io_stall_write_ms],
[vfs].[io_stall],
[vfs].[size_on_disk_bytes] / 1024 / 1024. [size_on_disk_MB],
[mf].[physical_name]
FROM [sys].[dm_io_virtual_file_stats](NULL, NULL) AS vfs
JOIN [sys].database_files [mf]
ON [vfs].[database_id] = DB_ID()
AND [vfs].[file_id] = [mf].file_id
ORDER BY [Latency] DESC;
/* Delete test */
/* 2000 iterations, 150 threads */
DECLARE @delid int = (SELECT ABS(CHECKSUM(NEWID()) % 20000))
DELETE from dbo.PerfTest where Id = @delid
/* Check the wait stats! */
SELECT
GETDATE() AS [DATETIME],
dm.wait_type AS WaitType,
dm.wait_time_ms - ws.wst_WaitTime AS WaitTime,
dm.waiting_tasks_count - ws.wst_WaitingTasks AS WaitingTasks,
dm.signal_wait_time_ms - ws.wst_SignalWaitTime AS SignalWaitTime
FROM sys.dm_os_wait_stats dm
INNER JOIN ws_Capture ws ON dm.wait_type = ws.wst_WaitType
where dm.wait_type NOT IN (
N'BROKER_EVENTHANDLER' ,N'BROKER_RECEIVE_WAITFOR',N'BROKER_TASK_STOP',N'BROKER_TO_FLUSH',N'BROKER_TRANSMITTER',N'CHECKPOINT_QUEUE',N'CHKPT',N'CLR_AUTO_EVENT',N'CLR_MANUAL_EVENT',N'CLR_SEMAPHORE'
,-- Maybe uncomment these four if you have mirroring issues
N'DBMIRROR_DBM_EVENT',N'DBMIRROR_EVENTS_QUEUE',N'DBMIRROR_WORKER_QUEUE',N'DBMIRRORING_CMD',N'DIRTY_PAGE_POLL',N'DISPATCHER_QUEUE_SEMAPHORE',N'EXECSYNC',N'FSAGENT',N'FT_IFTS_SCHEDULER_IDLE_WAIT',N'FT_IFTSHC_MUTEX',
-- Maybe uncomment these six if you have AG issues
N'HADR_CLUSAPI_CALL',N'HADR_FILESTREAM_IOMGR_IOCOMPLETION',N'HADR_LOGCAPTURE_WAIT',N'HADR_NOTIFICATION_DEQUEUE' ,N'HADR_TIMER_TASK',N'HADR_WORK_QUEUE',N'KSOURCE_WAKEUP',N'LAZYWRITER_SLEEP',N'LOGMGR_QUEUE',N'MEMORY_ALLOCATION_EXT',N'ONDEMAND_TASK_QUEUE',N'PREEMPTIVE_XE_GETTARGETSTATE',N'PWAIT_ALL_COMPONENTS_INITIALIZED',N'PWAIT_DIRECTLOGCONSUMER_GETNEXT',N'QDS_PERSIST_TASK_MAIN_LOOP_SLEEP'
,N'QDS_ASYNC_QUEUE',N'QDS_CLEANUP_STALE_QUERIES_TASK_MAIN_LOOP_SLEEP',N'QDS_SHUTDOWN_QUEUE',N'REDO_THREAD_PENDING_WORK',N'REQUEST_FOR_DEADLOCK_SEARCH',N'RESOURCE_QUEUE',N'SERVER_IDLE_CHECK',N'SLEEP_BPOOL_FLUSH',N'SLEEP_DBSTARTUP',N'SLEEP_DCOMSTARTUP',N'SLEEP_MASTERDBREADY',N'SLEEP_MASTERMDREADY',N'SLEEP_MASTERUPGRADED',N'SLEEP_MSDBSTARTUP',N'SLEEP_SYSTEMTASK',N'SLEEP_TASK',N'SLEEP_TEMPDBSTARTUP'
,N'SNI_HTTP_ACCEPT',N'SP_SERVER_DIAGNOSTICS_SLEEP',N'SQLTRACE_BUFFER_FLUSH',N'SQLTRACE_INCREMENTAL_FLUSH_SLEEP',N'SQLTRACE_WAIT_ENTRIES',N'WAIT_FOR_RESULTS',N'WAITFOR',N'WAITFOR_TASKSHUTDOWN',N'WAIT_XTP_RECOVERY',N'WAIT_XTP_HOST_WAIT',N'WAIT_XTP_OFFLINE_CKPT_NEW_LOG',N'WAIT_XTP_CKPT_CLOSE',N'XE_DISPATCHER_JOIN',N'XE_DISPATCHER_WAIT',N'XE_TIMER_EVENT', N'SOS_WORK_DISPATCHER')
and dm.wait_time_ms - ws.wst_WaitTime > 0
order by WaitTime DESC;
DROP TABLE ws_Capture;
/* Check the disk latency */
SELECT
--virtual file latency
GETDATE() AS CaptureDateTime,
F.CaptureDateTime as PrevCaptureDateTime,
CASE WHEN vfs.[num_of_reads] = 0 THEN 0 ELSE (vfs.[io_stall_read_ms] / vfs.[num_of_reads]) END - F.ReadLatency [ReadLatency],
CASE WHEN vfs.[io_stall_write_ms] = 0 THEN 0 ELSE (vfs.[io_stall_write_ms] / vfs.[num_of_writes]) END - F.WriteLatency [WriteLatency],
CASE WHEN (vfs.[num_of_reads] = 0 AND vfs.[num_of_writes] = 0) THEN 0 ELSE (vfs.[io_stall] / (vfs.[num_of_reads] + vfs.[num_of_writes])) END - F.Latency [Latency],
--avg bytes per IOP
CASE WHEN vfs.[num_of_reads] = 0 THEN 0 ELSE (vfs.[num_of_bytes_read] / vfs.[num_of_reads]) END [AvgBPerRead],
CASE WHEN vfs.[io_stall_write_ms] = 0 THEN 0 ELSE (vfs.[num_of_bytes_written] / vfs.[num_of_writes]) END [AvgBPerWrite],
CASE WHEN (vfs.[num_of_reads] = 0 AND vfs.[num_of_writes] = 0) THEN 0 ELSE ((vfs.[num_of_bytes_read] + vfs.[num_of_bytes_written]) / (vfs.[num_of_reads] + vfs.[num_of_writes])) END [AvgBPerTransfer],
LEFT([mf].[physical_name], 2) [Drive],
DB_NAME([vfs].[database_id]) [DB],
[vfs].[sample_ms],
[vfs].[num_of_reads],
[vfs].[num_of_bytes_read],
[vfs].[io_stall_read_ms],
[vfs].[num_of_writes],
[vfs].[num_of_bytes_written],
[vfs].[io_stall_write_ms],
[vfs].[io_stall],
[vfs].[size_on_disk_bytes] / 1024 / 1024. [size_on_disk_MB],
[mf].[physical_name]
FROM [sys].[dm_io_virtual_file_stats](NULL, NULL) AS vfs
JOIN [sys].database_files [mf]
ON [vfs].[database_id] = DB_ID()
AND [vfs].[file_id] = [mf].file_id
JOIN FileLatency F on DB_NAME([vfs].[database_id]) = F.DB
ORDER BY [Latency] DESC;
DROP TABLE FileLatency
/* Final step */
/* Wait Stats Monitoring start */
/* Run this script only once, just before your start the delete data test! */
CREATE TABLE ws_Capture
(
wst_WaitType VARCHAR(150),
wst_WaitTime BIGINT,
wst_WaitingTasks BIGINT,
wst_SignalWaitTime BIGINT
)
INSERT INTO ws_Capture
SELECT
wait_type,
wait_time_ms,
waiting_tasks_count,
signal_wait_time_ms
FROM sys.dm_os_wait_stats
/* File latency monitoring start */
CREATE TABLE FileLatency
(
CaptureDateTime DATETIME,
[ReadLatency] BIGINT,
[WriteLatency] BIGINT,
[Latency] BIGINT,
[AvgBPerRead] BIGINT,
[AvgBPerWrite] BIGINT,
[AvgBPerTransfer] BIGINT,
[Drive] VARCHAR(10),
[DB] VARCHAR(150),
[sample_ms] BIGINT,
[num_of_reads] BIGINT,
[num_of_bytes_read] BIGINT,
[io_stall_read_ms] BIGINT,
[num_of_writes] BIGINT,
[num_of_bytes_written] BIGINT,
[io_stall_write_ms] BIGINT,
[io_stall] BIGINT,
[size_on_disk_MB]DECIMAL(12,6),
[physical_name] VARCHAR(250)
)
INSERT INTO FileLatency
(
CaptureDateTime,
ReadLatency,
WriteLatency,
Latency,
AvgBPerRead,
AvgBPerWrite,
AvgBPerTransfer,
Drive,
DB,
sample_ms,
num_of_reads,
num_of_bytes_read,
io_stall_read_ms,
num_of_writes,
num_of_bytes_written,
io_stall_write_ms,
io_stall,
size_on_disk_MB,
physical_name
)
SELECT
--virtual file latency
GETDATE() AS CaptureDateTime,
CASE WHEN [num_of_reads] = 0 THEN 0 ELSE ([io_stall_read_ms] / [num_of_reads]) END [ReadLatency],
CASE WHEN [io_stall_write_ms] = 0 THEN 0 ELSE ([io_stall_write_ms] / [num_of_writes]) END [WriteLatency],
CASE WHEN ([num_of_reads] = 0 AND [num_of_writes] = 0) THEN 0 ELSE ([io_stall] / ([num_of_reads] + [num_of_writes])) END [Latency],
--avg bytes per IOP
CASE WHEN [num_of_reads] = 0 THEN 0 ELSE ([num_of_bytes_read] / [num_of_reads]) END [AvgBPerRead],
CASE WHEN [io_stall_write_ms] = 0 THEN 0 ELSE ([num_of_bytes_written] / [num_of_writes]) END [AvgBPerWrite],
CASE WHEN ([num_of_reads] = 0 AND [num_of_writes] = 0) THEN 0 ELSE (([num_of_bytes_read] + [num_of_bytes_written]) / ([num_of_reads] + [num_of_writes])) END [AvgBPerTransfer],
LEFT([mf].[physical_name], 2) [Drive],
DB_NAME([vfs].[database_id]) [DB],
[vfs].[sample_ms],
[vfs].[num_of_reads],
[vfs].[num_of_bytes_read],
[vfs].[io_stall_read_ms],
[vfs].[num_of_writes],
[vfs].[num_of_bytes_written],
[vfs].[io_stall_write_ms],
[vfs].[io_stall],
[vfs].[size_on_disk_bytes] / 1024 / 1024. [size_on_disk_MB],
[mf].[physical_name]
FROM [sys].[dm_io_virtual_file_stats](NULL, NULL) AS vfs
JOIN [sys].database_files [mf]
ON [vfs].[database_id] = DB_ID()
AND [vfs].[file_id] = [mf].file_id
ORDER BY [Latency] DESC;
/* Update test */
/* 2000 iterations, 150 threads */
DECLARE @upid int = (SELECT ABS(CHECKSUM(NEWID()) % 20000))
UPDATE dbo.PerfTest
set val2 = CONCAT(val2, ' ', val3),
val6 = val6 * 12,
val9 = val9 / 1245,
val16 = val16 + 34234324
where Id = @upid
/* Check the wait stats! */
SELECT
GETDATE() AS [DATETIME],
dm.wait_type AS WaitType,
dm.wait_time_ms - ws.wst_WaitTime AS WaitTime,
dm.waiting_tasks_count - ws.wst_WaitingTasks AS WaitingTasks,
dm.signal_wait_time_ms - ws.wst_SignalWaitTime AS SignalWaitTime
FROM sys.dm_os_wait_stats dm
INNER JOIN ws_Capture ws ON dm.wait_type = ws.wst_WaitType
where dm.wait_type NOT IN (
N'BROKER_EVENTHANDLER' ,N'BROKER_RECEIVE_WAITFOR',N'BROKER_TASK_STOP',N'BROKER_TO_FLUSH',N'BROKER_TRANSMITTER',N'CHECKPOINT_QUEUE',N'CHKPT',N'CLR_AUTO_EVENT',N'CLR_MANUAL_EVENT',N'CLR_SEMAPHORE'
,-- Maybe uncomment these four if you have mirroring issues
N'DBMIRROR_DBM_EVENT',N'DBMIRROR_EVENTS_QUEUE',N'DBMIRROR_WORKER_QUEUE',N'DBMIRRORING_CMD',N'DIRTY_PAGE_POLL',N'DISPATCHER_QUEUE_SEMAPHORE',N'EXECSYNC',N'FSAGENT',N'FT_IFTS_SCHEDULER_IDLE_WAIT',N'FT_IFTSHC_MUTEX',
-- Maybe uncomment these six if you have AG issues
N'HADR_CLUSAPI_CALL',N'HADR_FILESTREAM_IOMGR_IOCOMPLETION',N'HADR_LOGCAPTURE_WAIT',N'HADR_NOTIFICATION_DEQUEUE' ,N'HADR_TIMER_TASK',N'HADR_WORK_QUEUE',N'KSOURCE_WAKEUP',N'LAZYWRITER_SLEEP',N'LOGMGR_QUEUE',N'MEMORY_ALLOCATION_EXT',N'ONDEMAND_TASK_QUEUE',N'PREEMPTIVE_XE_GETTARGETSTATE',N'PWAIT_ALL_COMPONENTS_INITIALIZED',N'PWAIT_DIRECTLOGCONSUMER_GETNEXT',N'QDS_PERSIST_TASK_MAIN_LOOP_SLEEP'
,N'QDS_ASYNC_QUEUE',N'QDS_CLEANUP_STALE_QUERIES_TASK_MAIN_LOOP_SLEEP',N'QDS_SHUTDOWN_QUEUE',N'REDO_THREAD_PENDING_WORK',N'REQUEST_FOR_DEADLOCK_SEARCH',N'RESOURCE_QUEUE',N'SERVER_IDLE_CHECK',N'SLEEP_BPOOL_FLUSH',N'SLEEP_DBSTARTUP',N'SLEEP_DCOMSTARTUP',N'SLEEP_MASTERDBREADY',N'SLEEP_MASTERMDREADY',N'SLEEP_MASTERUPGRADED',N'SLEEP_MSDBSTARTUP',N'SLEEP_SYSTEMTASK',N'SLEEP_TASK',N'SLEEP_TEMPDBSTARTUP'
,N'SNI_HTTP_ACCEPT',N'SP_SERVER_DIAGNOSTICS_SLEEP',N'SQLTRACE_BUFFER_FLUSH',N'SQLTRACE_INCREMENTAL_FLUSH_SLEEP',N'SQLTRACE_WAIT_ENTRIES',N'WAIT_FOR_RESULTS',N'WAITFOR',N'WAITFOR_TASKSHUTDOWN',N'WAIT_XTP_RECOVERY',N'WAIT_XTP_HOST_WAIT',N'WAIT_XTP_OFFLINE_CKPT_NEW_LOG',N'WAIT_XTP_CKPT_CLOSE',N'XE_DISPATCHER_JOIN',N'XE_DISPATCHER_WAIT',N'XE_TIMER_EVENT', N'SOS_WORK_DISPATCHER')
and dm.wait_time_ms - ws.wst_WaitTime > 0
order by WaitTime DESC;
DROP TABLE ws_Capture;
/* Check the disk latency */
SELECT
--virtual file latency
GETDATE() AS CaptureDateTime,
F.CaptureDateTime as PrevCaptureDateTime,
CASE WHEN vfs.[num_of_reads] = 0 THEN 0 ELSE (vfs.[io_stall_read_ms] / vfs.[num_of_reads]) END - F.ReadLatency [ReadLatency],
CASE WHEN vfs.[io_stall_write_ms] = 0 THEN 0 ELSE (vfs.[io_stall_write_ms] / vfs.[num_of_writes]) END - F.WriteLatency [WriteLatency],
CASE WHEN (vfs.[num_of_reads] = 0 AND vfs.[num_of_writes] = 0) THEN 0 ELSE (vfs.[io_stall] / (vfs.[num_of_reads] + vfs.[num_of_writes])) END - F.Latency [Latency],
--avg bytes per IOP
CASE WHEN vfs.[num_of_reads] = 0 THEN 0 ELSE (vfs.[num_of_bytes_read] / vfs.[num_of_reads]) END [AvgBPerRead],
CASE WHEN vfs.[io_stall_write_ms] = 0 THEN 0 ELSE (vfs.[num_of_bytes_written] / vfs.[num_of_writes]) END [AvgBPerWrite],
CASE WHEN (vfs.[num_of_reads] = 0 AND vfs.[num_of_writes] = 0) THEN 0 ELSE ((vfs.[num_of_bytes_read] + vfs.[num_of_bytes_written]) / (vfs.[num_of_reads] + vfs.[num_of_writes])) END [AvgBPerTransfer],
LEFT([mf].[physical_name], 2) [Drive],
DB_NAME([vfs].[database_id]) [DB],
[vfs].[sample_ms],
[vfs].[num_of_reads],
[vfs].[num_of_bytes_read],
[vfs].[io_stall_read_ms],
[vfs].[num_of_writes],
[vfs].[num_of_bytes_written],
[vfs].[io_stall_write_ms],
[vfs].[io_stall],
[vfs].[size_on_disk_bytes] / 1024 / 1024. [size_on_disk_MB],
[mf].[physical_name]
FROM [sys].[dm_io_virtual_file_stats](NULL, NULL) AS vfs
JOIN [sys].database_files [mf]
ON [vfs].[database_id] = DB_ID()
AND [vfs].[file_id] = [mf].file_id
JOIN FileLatency F on DB_NAME([vfs].[database_id]) = F.DB
ORDER BY [Latency] DESC;
DROP TABLE FileLatency