-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy path02_create_tables.sql
More file actions
1496 lines (1419 loc) · 49.5 KB
/
02_create_tables.sql
File metadata and controls
1496 lines (1419 loc) · 49.5 KB
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2026 Darling Data, LLC
https://www.erikdarling.com/
*/
SET ANSI_NULLS ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
SET ARITHABORT ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
SET IMPLICIT_TRANSACTIONS OFF;
SET STATISTICS TIME, IO OFF;
GO
USE PerformanceMonitor;
GO
/*
Cleanup: session_wait_stats removed in v1.4
*/
IF OBJECT_ID(N'report.session_wait_analysis', N'V') IS NOT NULL DROP VIEW report.session_wait_analysis;
IF OBJECT_ID(N'collect.session_wait_stats_collector', N'P') IS NOT NULL DROP PROCEDURE collect.session_wait_stats_collector;
IF OBJECT_ID(N'collect.session_wait_stats', N'U') IS NOT NULL DROP TABLE collect.session_wait_stats;
IF OBJECT_ID(N'config.collection_schedule', N'U') IS NOT NULL DELETE FROM config.collection_schedule WHERE collector_name = N'session_wait_stats_collector';
GO
/*
Collection tables for the 7 core collectors
*/
/*
1. Wait Stats with Deltas
*/
IF OBJECT_ID(N'collect.wait_stats', N'U') IS NULL
BEGIN
CREATE TABLE
collect.wait_stats
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
server_start_time datetime2(7) NOT NULL,
wait_type nvarchar(60) NOT NULL,
waiting_tasks_count bigint NOT NULL,
wait_time_ms bigint NOT NULL,
signal_wait_time_ms bigint NOT NULL,
/*Delta calculations*/
waiting_tasks_count_delta bigint NULL,
wait_time_ms_delta bigint NULL,
signal_wait_time_ms_delta bigint NULL,
sample_interval_seconds integer NULL,
/*Analysis helpers*/
wait_time_ms_per_second AS
(
wait_time_ms_delta /
NULLIF(sample_interval_seconds, 0)
),
signal_wait_time_ms_per_second AS
(
signal_wait_time_ms_delta /
NULLIF(sample_interval_seconds, 0)
),
CONSTRAINT
PK_wait_stats
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.wait_stats table';
END;
/*
2. Query Performance with Deltas
*/
IF OBJECT_ID(N'collect.query_stats', N'U') IS NULL
BEGIN
CREATE TABLE
collect.query_stats
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
server_start_time datetime2(7) NOT NULL,
object_type nvarchar(20) NOT NULL
DEFAULT N'STATEMENT', /*PROCEDURE, TRIGGER, FUNCTION*/
database_name sysname NOT NULL,
object_name sysname NULL,
schema_name sysname NULL,
sql_handle varbinary(64) NOT NULL,
statement_start_offset integer NOT NULL,
statement_end_offset integer NOT NULL,
plan_generation_num bigint NOT NULL,
plan_handle varbinary(64) NOT NULL,
creation_time datetime2(7) NOT NULL,
last_execution_time datetime2(7) NOT NULL,
/*Raw cumulative values*/
execution_count bigint NOT NULL,
total_worker_time bigint NOT NULL,
min_worker_time bigint NOT NULL,
max_worker_time bigint NOT NULL,
total_physical_reads bigint NOT NULL,
min_physical_reads bigint NOT NULL,
max_physical_reads bigint NOT NULL,
total_logical_writes bigint NOT NULL,
total_logical_reads bigint NOT NULL,
total_clr_time bigint NOT NULL,
total_elapsed_time bigint NOT NULL,
min_elapsed_time bigint NOT NULL,
max_elapsed_time bigint NOT NULL,
query_hash binary(8) NULL,
query_plan_hash binary(8) NULL,
total_rows bigint NOT NULL,
min_rows bigint NOT NULL,
max_rows bigint NOT NULL,
statement_sql_handle varbinary(64) NULL,
statement_context_id bigint NULL,
min_dop bigint NOT NULL,
max_dop bigint NOT NULL,
min_grant_kb bigint NOT NULL,
max_grant_kb bigint NOT NULL,
min_used_grant_kb bigint NOT NULL,
max_used_grant_kb bigint NOT NULL,
min_ideal_grant_kb bigint NOT NULL,
max_ideal_grant_kb bigint NOT NULL,
min_reserved_threads bigint NOT NULL,
max_reserved_threads bigint NOT NULL,
min_used_threads bigint NOT NULL,
max_used_threads bigint NOT NULL,
total_spills bigint NOT NULL,
min_spills bigint NOT NULL,
max_spills bigint NOT NULL,
/*Delta calculations*/
execution_count_delta bigint NULL,
total_worker_time_delta bigint NULL,
total_elapsed_time_delta bigint NULL,
total_logical_reads_delta bigint NULL,
total_physical_reads_delta bigint NULL,
total_logical_writes_delta bigint NULL,
sample_interval_seconds integer NULL,
/*Analysis helpers - computed columns*/
avg_rows AS
(
total_rows /
NULLIF(execution_count, 0)
),
avg_worker_time_ms AS
(
total_worker_time /
NULLIF(execution_count, 0) / 1000.
),
avg_elapsed_time_ms AS
(
total_elapsed_time /
NULLIF(execution_count, 0) / 1000.
),
avg_physical_reads AS
(
total_physical_reads /
NULLIF(execution_count, 0)
),
worker_time_per_second AS
(
total_worker_time_delta /
NULLIF(sample_interval_seconds, 0) / 1000.
),
/*Query text and execution plan (compressed with COMPRESS/DECOMPRESS)*/
query_text varbinary(max) NULL,
query_plan_text varbinary(max) NULL,
/*Deduplication hash for skipping unchanged rows*/
row_hash binary(32) NULL,
CONSTRAINT
PK_query_stats
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.query_stats table';
END;
/*
2b. Query Stats Dedup Tracking
One row per natural key, updated on each collection cycle
*/
IF OBJECT_ID(N'collect.query_stats_latest_hash', N'U') IS NULL
BEGIN
CREATE TABLE
collect.query_stats_latest_hash
(
sql_handle varbinary(64) NOT NULL,
statement_start_offset integer NOT NULL,
statement_end_offset integer NOT NULL,
plan_handle varbinary(64) NOT NULL,
row_hash binary(32) NOT NULL,
last_seen datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
CONSTRAINT
PK_query_stats_latest_hash
PRIMARY KEY CLUSTERED
(sql_handle, statement_start_offset,
statement_end_offset, plan_handle)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.query_stats_latest_hash table';
END;
/*
3. Memory Pressure
*/
IF OBJECT_ID(N'collect.memory_stats', N'U') IS NULL
BEGIN
CREATE TABLE
collect.memory_stats
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
/*Memory clerks summary*/
buffer_pool_mb decimal(19,2) NOT NULL,
plan_cache_mb decimal(19,2) NOT NULL,
other_memory_mb decimal(19,2) NOT NULL,
total_memory_mb decimal(19,2) NOT NULL,
/*Process memory*/
physical_memory_in_use_mb decimal(19,2) NOT NULL,
available_physical_memory_mb decimal(19,2) NOT NULL,
memory_utilization_percentage integer NOT NULL,
/*Server and target memory*/
total_physical_memory_mb decimal(19,2) NULL,
committed_target_memory_mb decimal(19,2) NULL,
/*Pressure warnings*/
buffer_pool_pressure_warning bit NOT NULL DEFAULT 0,
plan_cache_pressure_warning bit NOT NULL DEFAULT 0,
/*Analysis helpers - computed columns*/
buffer_pool_percentage AS
(
buffer_pool_mb * 100.0 /
NULLIF(total_memory_mb, 0)
),
plan_cache_percentage AS
(
plan_cache_mb * 100.0 /
NULLIF(total_memory_mb, 0)
),
CONSTRAINT
PK_memory_stats
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.memory_stats table';
END;
/*Add columns for existing installs*/
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID(N'collect.memory_stats') AND name = N'total_physical_memory_mb')
BEGIN
ALTER TABLE collect.memory_stats ADD total_physical_memory_mb decimal(19,2) NULL;
PRINT 'Added total_physical_memory_mb to collect.memory_stats';
END;
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID(N'collect.memory_stats') AND name = N'committed_target_memory_mb')
BEGIN
ALTER TABLE collect.memory_stats ADD committed_target_memory_mb decimal(19,2) NULL;
PRINT 'Added committed_target_memory_mb to collect.memory_stats';
END;
/*
4. I/O Performance - handled by sp_PressureDetector
NOTE: I/O metrics are collected by sp_PressureDetector into collect.PressureDetector_FileMetrics
to ensure proper cloud platform compatibility (Azure SQL DB, Managed Instance, AWS RDS)
*/
/*
5a. Memory Pressure Events from Ring Buffer
*/
IF OBJECT_ID(N'collect.memory_pressure_events', N'U') IS NULL
BEGIN
CREATE TABLE
collect.memory_pressure_events
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
sample_time datetime2(7) NOT NULL,
memory_notification nvarchar(100) NOT NULL,
memory_indicators_process integer NOT NULL,
memory_indicators_system integer NOT NULL,
CONSTRAINT
PK_memory_pressure_events
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.memory_pressure_events table';
END;
/*
5b. CPU Utilization Events
NOTE: CPU utilization is collected via collect.cpu_utilization_stats
using the 32_collect_cpu_utilization_stats.sql collector which reads
from RING_BUFFER_SCHEDULER_MONITOR ring buffer.
*/
/*
6. System Health Data (handled by sp_HealthParser)
NOTE: sp_HealthParser creates its own tables in the collect schema:
- collect.HealthParser_SignificantWaits
- collect.HealthParser_WaitsByCount
- collect.HealthParser_WaitsByDuration
- collect.HealthParser_IOIssues
- collect.HealthParser_CPUTasks
- collect.HealthParser_MemoryConditions
- collect.HealthParser_MemoryBroker
- collect.HealthParser_MemoryNodeOOM
- collect.HealthParser_SystemHealth
- collect.HealthParser_SchedulerIssues
- collect.HealthParser_SevereErrors
These tables are created automatically by sp_HealthParser when first called.
*/
/*
8. Raw XML Collection Tables (Fast Collection + Later Parsing)
*/
/*
8a. Deadlock XML Storage
Raw deadlock XML for later analysis with sp_BlitzLock
*/
IF OBJECT_ID(N'collect.deadlock_xml', N'U') IS NULL
BEGIN
CREATE TABLE
collect.deadlock_xml
(
id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
event_time datetime2(7) NULL,
deadlock_xml xml NOT NULL,
is_processed bit NOT NULL DEFAULT 0,
CONSTRAINT
PK_deadlock_xml
PRIMARY KEY CLUSTERED
(collection_time, id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.deadlock_xml table';
END;
/*
8b. Blocked Process XML Storage
Raw blocked process XML for later analysis with sp_HumanEventsBlockViewer
*/
IF OBJECT_ID(N'collect.blocked_process_xml', N'U') IS NULL
BEGIN
CREATE TABLE
collect.blocked_process_xml
(
id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
event_time datetime2(7) NULL,
blocked_process_xml xml NOT NULL,
is_processed bit NOT NULL DEFAULT 0,
CONSTRAINT
PK_blocked_process_xml
PRIMARY KEY CLUSTERED
(collection_time, id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.blocked_process_xml table';
END;
/*
9. Procedure, Trigger, and Function Stats
*/
IF OBJECT_ID(N'collect.procedure_stats', N'U') IS NULL
BEGIN
CREATE TABLE
collect.procedure_stats
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
server_start_time datetime2(7) NOT NULL,
object_type nvarchar(20) NOT NULL, /*PROCEDURE, TRIGGER, FUNCTION*/
database_name sysname NOT NULL,
object_id integer NOT NULL,
object_name sysname NULL,
schema_name sysname NULL,
type_desc nvarchar(60) NULL,
sql_handle varbinary(64) NOT NULL,
plan_handle varbinary(64) NOT NULL,
cached_time datetime2(7) NOT NULL,
last_execution_time datetime2(7) NOT NULL,
/*Raw cumulative values*/
execution_count bigint NOT NULL,
total_worker_time bigint NOT NULL,
min_worker_time bigint NOT NULL,
max_worker_time bigint NOT NULL,
total_elapsed_time bigint NOT NULL,
min_elapsed_time bigint NOT NULL,
max_elapsed_time bigint NOT NULL,
total_logical_reads bigint NOT NULL,
min_logical_reads bigint NOT NULL,
max_logical_reads bigint NOT NULL,
total_physical_reads bigint NOT NULL,
min_physical_reads bigint NOT NULL,
max_physical_reads bigint NOT NULL,
total_logical_writes bigint NOT NULL,
min_logical_writes bigint NOT NULL,
max_logical_writes bigint NOT NULL,
total_spills bigint NULL,
min_spills bigint NULL,
max_spills bigint NULL,
/*Delta calculations*/
execution_count_delta bigint NULL,
total_worker_time_delta bigint NULL,
total_elapsed_time_delta bigint NULL,
total_logical_reads_delta bigint NULL,
total_physical_reads_delta bigint NULL,
total_logical_writes_delta bigint NULL,
sample_interval_seconds integer NULL,
/*Analysis helpers - computed columns*/
avg_worker_time_ms AS
(
total_worker_time /
NULLIF(execution_count, 0) / 1000.
),
avg_elapsed_time_ms AS
(
total_elapsed_time /
NULLIF(execution_count, 0) / 1000.
),
avg_physical_reads AS
(
total_physical_reads /
NULLIF(execution_count, 0)
),
worker_time_per_second AS
(
total_worker_time_delta /
NULLIF(sample_interval_seconds, 0) / 1000.
),
/*Execution plan (compressed with COMPRESS/DECOMPRESS)*/
query_plan_text varbinary(max) NULL,
/*Deduplication hash for skipping unchanged rows*/
row_hash binary(32) NULL,
CONSTRAINT
PK_procedure_stats
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.procedure_stats table';
END;
/*
9b. Procedure Stats Dedup Tracking
One row per natural key, updated on each collection cycle
*/
IF OBJECT_ID(N'collect.procedure_stats_latest_hash', N'U') IS NULL
BEGIN
CREATE TABLE
collect.procedure_stats_latest_hash
(
database_name sysname NOT NULL,
object_id integer NOT NULL,
plan_handle varbinary(64) NOT NULL,
row_hash binary(32) NOT NULL,
last_seen datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
CONSTRAINT
PK_procedure_stats_latest_hash
PRIMARY KEY CLUSTERED
(database_name, object_id, plan_handle)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.procedure_stats_latest_hash table';
END;
/*
10. Currently Executing Query Snapshots
Table is created dynamically by sp_WhoIsActive on first collection
The collector (18_collect_query_snapshots.sql) uses sp_WhoIsActive with @return_schema
to generate the table definition based on the sp_WhoIsActive version installed
This ensures compatibility with the sp_WhoIsActive schema and includes all columns
that sp_WhoIsActive provides (collection_time, session details, blocking, waits, plans, etc.)
*/
PRINT 'Query snapshots table will be created by sp_WhoIsActive on first collection';
/*
11. Query Store Data
*/
IF OBJECT_ID(N'collect.query_store_data', N'U') IS NULL
BEGIN
CREATE TABLE
collect.query_store_data
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
database_name sysname NOT NULL,
query_id bigint NOT NULL,
plan_id bigint NOT NULL,
execution_type_desc nvarchar(60) NULL,
utc_first_execution_time datetimeoffset(7) NOT NULL,
utc_last_execution_time datetimeoffset(7) NOT NULL,
server_first_execution_time datetime2(7) NOT NULL,
server_last_execution_time datetime2(7) NOT NULL,
module_name nvarchar(261) NULL,
query_sql_text varbinary(max) NULL,
query_hash binary(8) NULL,
/*Execution count*/
count_executions bigint NOT NULL,
/*Duration metrics (microseconds)*/
avg_duration bigint NOT NULL,
min_duration bigint NOT NULL,
max_duration bigint NOT NULL,
/*CPU time metrics (microseconds)*/
avg_cpu_time bigint NOT NULL,
min_cpu_time bigint NOT NULL,
max_cpu_time bigint NOT NULL,
/*Logical IO reads*/
avg_logical_io_reads bigint NOT NULL,
min_logical_io_reads bigint NOT NULL,
max_logical_io_reads bigint NOT NULL,
/*Logical IO writes*/
avg_logical_io_writes bigint NOT NULL,
min_logical_io_writes bigint NOT NULL,
max_logical_io_writes bigint NOT NULL,
/*Physical IO reads*/
avg_physical_io_reads bigint NOT NULL,
min_physical_io_reads bigint NOT NULL,
max_physical_io_reads bigint NOT NULL,
/*Number of physical IO reads - NULL on SQL 2016*/
avg_num_physical_io_reads bigint NULL,
min_num_physical_io_reads bigint NULL,
max_num_physical_io_reads bigint NULL,
/*CLR time (microseconds)*/
avg_clr_time bigint NOT NULL,
min_clr_time bigint NOT NULL,
max_clr_time bigint NOT NULL,
/*DOP (degree of parallelism)*/
min_dop bigint NOT NULL,
max_dop bigint NOT NULL,
/*Memory grant (8KB pages)*/
avg_query_max_used_memory bigint NOT NULL,
min_query_max_used_memory bigint NOT NULL,
max_query_max_used_memory bigint NOT NULL,
/*Row count*/
avg_rowcount bigint NOT NULL,
min_rowcount bigint NOT NULL,
max_rowcount bigint NOT NULL,
/*Log bytes used*/
avg_log_bytes_used bigint NULL,
min_log_bytes_used bigint NULL,
max_log_bytes_used bigint NULL,
/*Tempdb space used (8KB pages)*/
avg_tempdb_space_used bigint NULL,
min_tempdb_space_used bigint NULL,
max_tempdb_space_used bigint NULL,
/*Plan information*/
plan_type nvarchar(60) NULL,
is_forced_plan bit NOT NULL,
force_failure_count bigint NULL,
last_force_failure_reason_desc nvarchar(128) NULL,
plan_forcing_type nvarchar(60) NULL,
compatibility_level smallint NULL,
query_plan_text varbinary(max) NULL,
compilation_metrics varbinary(max) NULL,
query_plan_hash binary(8) NULL,
/*Deduplication hash for skipping unchanged rows*/
row_hash binary(32) NULL,
CONSTRAINT
PK_query_store_data
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.query_store_data table';
END;
/*
11b. Query Store Data Dedup Tracking
One row per natural key, updated on each collection cycle
*/
IF OBJECT_ID(N'collect.query_store_data_latest_hash', N'U') IS NULL
BEGIN
CREATE TABLE
collect.query_store_data_latest_hash
(
database_name sysname NOT NULL,
query_id bigint NOT NULL,
plan_id bigint NOT NULL,
row_hash binary(32) NOT NULL,
last_seen datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
CONSTRAINT
PK_query_store_data_latest_hash
PRIMARY KEY CLUSTERED
(database_name, query_id, plan_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.query_store_data_latest_hash table';
END;
/*
Trace analysis table - stores processed trace file data
*/
IF OBJECT_ID(N'collect.trace_analysis', N'U') IS NULL
BEGIN
CREATE TABLE
collect.trace_analysis
(
analysis_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL DEFAULT SYSDATETIME(),
trace_file_name nvarchar(260) NOT NULL,
event_class integer NOT NULL,
event_name nvarchar(50) NOT NULL,
database_name nvarchar(128) NULL,
login_name nvarchar(128) NULL,
nt_user_name nvarchar(128) NULL,
application_name nvarchar(256) NULL,
host_name nvarchar(128) NULL,
spid integer NULL,
duration_ms bigint NULL,
cpu_ms bigint NULL,
reads bigint NULL,
writes bigint NULL,
row_counts bigint NULL,
start_time datetime2(7) NULL,
end_time datetime2(7) NULL,
sql_text nvarchar(max) NULL,
object_id bigint NULL,
client_process_id integer NULL,
session_context nvarchar(500) NULL,
CONSTRAINT
PK_trace_analysis
PRIMARY KEY CLUSTERED
(collection_time, analysis_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.trace_analysis table';
END;
/*
Default trace events table - stores system events from default trace
*/
IF OBJECT_ID(N'collect.default_trace_events', N'U') IS NULL
BEGIN
CREATE TABLE
collect.default_trace_events
(
event_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL DEFAULT SYSDATETIME(),
event_time datetime2(7) NOT NULL,
event_name nvarchar(128) NOT NULL,
event_class integer NOT NULL,
spid integer NULL,
database_name sysname NULL,
database_id integer NULL,
login_name nvarchar(128) NULL,
host_name nvarchar(128) NULL,
application_name nvarchar(256) NULL,
server_name nvarchar(128) NULL,
object_name sysname NULL,
filename nvarchar(260) NULL,
integer_data bigint NULL,
integer_data_2 bigint NULL,
text_data nvarchar(max) NULL,
binary_data varbinary(max) NULL,
session_login_name nvarchar(128) NULL,
error_number integer NULL,
severity integer NULL,
state integer NULL,
event_sequence bigint NULL,
is_system bit NULL,
request_id integer NULL,
duration_us bigint NULL,
end_time datetime2(7) NULL,
CONSTRAINT
PK_default_trace_events
PRIMARY KEY
(collection_time, event_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.default_trace_events table';
END;
/*
File I/O Statistics Table
*/
IF OBJECT_ID(N'collect.file_io_stats', N'U') IS NULL
BEGIN
CREATE TABLE
collect.file_io_stats
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL DEFAULT SYSDATETIME(),
server_start_time datetime2(7) NOT NULL,
database_id integer NOT NULL,
database_name sysname NULL,
file_id integer NOT NULL,
file_name sysname NULL,
file_type_desc nvarchar(60) NULL,
physical_name nvarchar(260) NULL,
size_on_disk_bytes bigint NULL,
num_of_reads bigint NULL,
num_of_bytes_read bigint NULL,
io_stall_read_ms bigint NULL,
num_of_writes bigint NULL,
num_of_bytes_written bigint NULL,
io_stall_write_ms bigint NULL,
io_stall_ms bigint NULL,
io_stall_queued_read_ms bigint NULL,
io_stall_queued_write_ms bigint NULL,
sample_ms bigint NULL,
/*Delta columns calculated by framework*/
num_of_reads_delta bigint NULL,
num_of_bytes_read_delta bigint NULL,
io_stall_read_ms_delta bigint NULL,
num_of_writes_delta bigint NULL,
num_of_bytes_written_delta bigint NULL,
io_stall_write_ms_delta bigint NULL,
io_stall_ms_delta bigint NULL,
io_stall_queued_read_ms_delta bigint NULL,
io_stall_queued_write_ms_delta bigint NULL,
sample_ms_delta bigint NULL,
CONSTRAINT
PK_file_io_stats
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.file_io_stats table';
END;
/*
Memory Grant Statistics Table
*/
IF OBJECT_ID(N'collect.memory_grant_stats', N'U') IS NULL
BEGIN
CREATE TABLE
collect.memory_grant_stats
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL DEFAULT SYSDATETIME(),
server_start_time datetime2(7) NOT NULL,
resource_semaphore_id smallint NOT NULL,
pool_id integer NOT NULL,
target_memory_mb decimal(19,2) NULL,
max_target_memory_mb decimal(19,2) NULL,
total_memory_mb decimal(19,2) NULL,
available_memory_mb decimal(19,2) NULL,
granted_memory_mb decimal(19,2) NULL,
used_memory_mb decimal(19,2) NULL,
grantee_count integer NULL,
waiter_count integer NULL,
timeout_error_count bigint NULL,
forced_grant_count bigint NULL,
/*Delta columns calculated by framework*/
timeout_error_count_delta bigint NULL,
forced_grant_count_delta bigint NULL,
sample_interval_seconds integer NULL,
CONSTRAINT
PK_memory_grant_stats
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.memory_grant_stats table';
END;
/*
CPU Scheduler Statistics Table
*/
IF OBJECT_ID(N'collect.cpu_scheduler_stats', N'U') IS NULL
BEGIN
CREATE TABLE
collect.cpu_scheduler_stats
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL DEFAULT SYSDATETIME(),
max_workers_count integer NULL,
scheduler_count integer NULL,
cpu_count integer NULL,
total_runnable_tasks_count integer NULL,
total_work_queue_count integer NULL,
total_current_workers_count integer NULL,
avg_runnable_tasks_count decimal(38,2) NULL,
total_active_request_count integer NULL,
total_queued_request_count integer NULL,
total_blocked_task_count integer NULL,
total_active_parallel_thread_count integer NULL,
runnable_request_count integer NULL,
total_request_count integer NULL,
runnable_percent decimal(38,2) NULL,
/*Pressure warnings*/
worker_thread_exhaustion_warning bit NULL,
runnable_tasks_warning bit NULL,
blocked_tasks_warning bit NULL,
queued_requests_warning bit NULL,
/*OS Memory metrics from sys.dm_os_sys_memory*/
total_physical_memory_kb bigint NULL,
available_physical_memory_kb bigint NULL,
system_memory_state_desc nvarchar(120) NULL,
physical_memory_pressure_warning bit NULL,
/*NUMA node metrics from sys.dm_os_nodes*/
total_node_count integer NULL,
nodes_online_count integer NULL,
offline_cpu_count integer NULL,
offline_cpu_warning bit NULL,
CONSTRAINT
PK_cpu_scheduler_stats
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.cpu_scheduler_stats table';
END;
/*
Memory Clerks Statistics Table
*/
IF OBJECT_ID(N'collect.memory_clerks_stats', N'U') IS NULL
BEGIN
CREATE TABLE
collect.memory_clerks_stats
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL DEFAULT SYSDATETIME(),
server_start_time datetime2(7) NOT NULL,
clerk_type nvarchar(60) NOT NULL,
memory_node_id smallint NOT NULL,
/*Raw cumulative values*/
pages_kb bigint NULL,
virtual_memory_reserved_kb bigint NULL,
virtual_memory_committed_kb bigint NULL,
awe_allocated_kb bigint NULL,
shared_memory_reserved_kb bigint NULL,
shared_memory_committed_kb bigint NULL,
/*Delta calculations*/
pages_kb_delta bigint NULL,
virtual_memory_reserved_kb_delta bigint NULL,
virtual_memory_committed_kb_delta bigint NULL,
awe_allocated_kb_delta bigint NULL,
shared_memory_reserved_kb_delta bigint NULL,
shared_memory_committed_kb_delta bigint NULL,
sample_interval_seconds integer NULL,
CONSTRAINT
PK_memory_clerks_stats
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.memory_clerks_stats table';
END;
/*
Performance Monitor Statistics Table
*/
IF OBJECT_ID(N'collect.perfmon_stats', N'U') IS NULL
BEGIN
CREATE TABLE
collect.perfmon_stats
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL DEFAULT SYSDATETIME(),
server_start_time datetime2(7) NOT NULL,
object_name sysname NOT NULL,
counter_name sysname NOT NULL,
instance_name sysname NOT NULL,
cntr_value bigint NOT NULL,
cntr_type bigint NOT NULL,
/*Delta column calculated by framework for cumulative counters*/
cntr_value_delta bigint NULL,
sample_interval_seconds integer NULL,
/*Analysis helper - per-second rate*/
cntr_value_per_second AS
(
cntr_value_delta /
NULLIF(sample_interval_seconds, 0)
),
CONSTRAINT
PK_perfmon_stats
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.perfmon_stats table';
END;
/*
CPU Utilization Statistics Table
*/
IF OBJECT_ID(N'collect.cpu_utilization_stats', N'U') IS NULL
BEGIN
CREATE TABLE
collect.cpu_utilization_stats
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL DEFAULT SYSDATETIME(),
sample_time datetime2(7) NOT NULL,
sqlserver_cpu_utilization integer NOT NULL,
other_process_cpu_utilization integer NOT NULL,
total_cpu_utilization AS (sqlserver_cpu_utilization + other_process_cpu_utilization) PERSISTED,
CONSTRAINT PK_cpu_utilization_stats PRIMARY KEY CLUSTERED (collection_time, collection_id) WITH (DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.cpu_utilization_stats table';
END;
/*
Blocking and deadlock aggregate statistics table
Tracks blocking and deadlock events by database over time with delta calculations
Enables trend analysis and alerting for blocking/deadlock increases
*/
IF OBJECT_ID(N'collect.blocking_deadlock_stats', N'U') IS NULL
BEGIN
CREATE TABLE
collect.blocking_deadlock_stats
(
collection_id bigint IDENTITY NOT NULL,
collection_time datetime2(7) NOT NULL
DEFAULT SYSDATETIME(),
database_name sysname NOT NULL,
/*Blocking metrics from sp_HumanEventsBlockViewer*/
blocking_event_count bigint NOT NULL
DEFAULT 0,
total_blocking_duration_ms bigint NOT NULL
DEFAULT 0,
max_blocking_duration_ms bigint NOT NULL
DEFAULT 0,
avg_blocking_duration_ms decimal(19,2) NULL,
/*Deadlock metrics from sp_BlitzLock*/
deadlock_count bigint NOT NULL
DEFAULT 0,
total_deadlock_wait_time_ms bigint NOT NULL
DEFAULT 0,
victim_count bigint NOT NULL
DEFAULT 0,
/*Delta calculations*/
blocking_event_count_delta bigint NULL,
total_blocking_duration_ms_delta bigint NULL,
max_blocking_duration_ms_delta bigint NULL,
deadlock_count_delta bigint NULL,
total_deadlock_wait_time_ms_delta bigint NULL,
victim_count_delta bigint NULL,
sample_interval_seconds integer NULL,
CONSTRAINT
PK_blocking_deadlock_stats
PRIMARY KEY CLUSTERED
(collection_time, collection_id)
WITH
(DATA_COMPRESSION = PAGE)
);
PRINT 'Created collect.blocking_deadlock_stats table';
END;
/*