-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathPlanModels.cs
More file actions
562 lines (483 loc) · 20.4 KB
/
PlanModels.cs
File metadata and controls
562 lines (483 loc) · 20.4 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
using System.Collections.Generic;
using System.Linq;
namespace PlanViewer.Core.Models;
public class ParsedPlan
{
public string RawXml { get; set; } = "";
public string? ParseError { get; set; }
public string? BuildVersion { get; set; }
public string? Build { get; set; }
public bool ClusteredMode { get; set; }
public List<PlanBatch> Batches { get; set; } = new();
public List<MissingIndex> AllMissingIndexes => Batches
.SelectMany(b => b.Statements)
.SelectMany(s => s.MissingIndexes)
.ToList();
}
public class PlanBatch
{
public List<PlanStatement> Statements { get; set; } = new();
}
public class PlanStatement
{
public string StatementText { get; set; } = "";
public string StatementType { get; set; } = "";
public double StatementSubTreeCost { get; set; }
public double StatementEstRows { get; set; }
public PlanNode? RootNode { get; set; }
public List<MissingIndex> MissingIndexes { get; set; } = new();
public MemoryGrantInfo? MemoryGrant { get; set; }
// Statement-level metadata
public int CardinalityEstimationModelVersion { get; set; }
public long CompileTimeMs { get; set; }
public long CompileMemoryKB { get; set; }
public long CompileCPUMs { get; set; }
public string? NonParallelPlanReason { get; set; }
public string? QueryHash { get; set; }
public string? QueryPlanHash { get; set; }
public long CachedPlanSizeKB { get; set; }
public int DegreeOfParallelism { get; set; }
public bool RetrievedFromCache { get; set; }
// Additional StmtSimple attributes
public string? StatementOptmLevel { get; set; }
public string? StatementOptmEarlyAbortReason { get; set; }
public int StatementParameterizationType { get; set; }
public string? StatementSqlHandle { get; set; }
public long DatabaseContextSettingsId { get; set; }
public int ParentObjectId { get; set; }
public bool SecurityPolicyApplied { get; set; }
public bool BatchModeOnRowStoreUsed { get; set; }
// QueryPlan sub-elements
public OptimizerHardwareInfo? HardwareProperties { get; set; }
public List<OptimizerStatsUsageItem> StatsUsage { get; set; } = new();
public ThreadStatInfo? ThreadStats { get; set; }
public SetOptionsInfo? SetOptions { get; set; }
public List<PlanParameter> Parameters { get; set; } = new();
public List<WaitStatInfo> WaitStats { get; set; } = new();
public QueryTimeInfo? QueryTimeStats { get; set; }
// MaxQueryMemory + QueryPlan-level warnings
public long MaxQueryMemoryKB { get; set; }
public List<PlanWarning> PlanWarnings { get; set; } = new();
// DOP feedback, plan guide, parameterized text, QS hints, trace flags, indexed views
public int EffectiveDOP { get; set; }
public string? DOPFeedbackAdjusted { get; set; }
public string? PlanGuideDB { get; set; }
public string? PlanGuideName { get; set; }
public bool UsePlan { get; set; }
public string? ParameterizedText { get; set; }
public int QueryStoreStatementHintId { get; set; }
public string? QueryStoreStatementHintText { get; set; }
public string? QueryStoreStatementHintSource { get; set; }
public List<TraceFlagInfo> TraceFlags { get; set; } = new();
public List<string> IndexedViews { get; set; } = new();
// Cursor plan metadata
public string? CursorName { get; set; }
public string? CursorActualType { get; set; }
public string? CursorRequestedType { get; set; }
public string? CursorConcurrency { get; set; }
public bool CursorForwardOnly { get; set; }
// Statement-level identifiers
public int StatementId { get; set; }
public int StatementCompId { get; set; }
// Template plan guide
public string? TemplatePlanGuideDB { get; set; }
public string? TemplatePlanGuideName { get; set; }
// Handle attributes
public string? ParameterizedPlanHandle { get; set; }
public string? BatchSqlHandle { get; set; }
// Feature flags
public bool ContainsLedgerTables { get; set; }
public bool ContainsInterleavedExecutionCandidates { get; set; }
public bool ContainsInlineScalarTsqlUdfs { get; set; }
// QueryPlan metadata
public int QueryVariantID { get; set; }
public string? DispatcherPlanHandle { get; set; }
public bool ExclusiveProfileTimeActive { get; set; }
public string? OptimizationReplayScript { get; set; }
// QueryPlan-level MemoryGrant attribute (unsignedLong)
public long QueryPlanMemoryGrantKB { get; set; }
// StmtUseDb: USE database statement
public string? StmtUseDatabaseName { get; set; }
// BaseStmtInfoType
public int QueryCompilationReplay { get; set; }
// QueryTimeStats UDF totals (statement-level, separate from per-node)
public long QueryUdfCpuTimeMs { get; set; }
public long QueryUdfElapsedTimeMs { get; set; }
// CardinalityFeedback
public List<CardinalityFeedbackEntry> CardinalityFeedback { get; set; } = new();
// Dispatcher/PSP
public DispatcherInfo? Dispatcher { get; set; }
// UDF/StoredProc sub-plans
public List<FunctionPlanInfo> UdfPlans { get; set; } = new();
public FunctionPlanInfo? StoredProcPlan { get; set; }
}
public class PlanNode
{
// Identity
public int NodeId { get; set; }
public string PhysicalOp { get; set; } = "";
public string LogicalOp { get; set; } = "";
// Cost metrics
public double EstimatedTotalSubtreeCost { get; set; }
public double EstimatedOperatorCost { get; set; }
public double EstimateRows { get; set; }
public double EstimateIO { get; set; }
public double EstimateCPU { get; set; }
public double EstimateRebinds { get; set; }
public double EstimateRewinds { get; set; }
public int EstimatedRowSize { get; set; }
// Actual runtime stats (0 if estimated plan only)
public long ActualRows { get; set; }
public long ActualExecutions { get; set; }
public long ActualRowsRead { get; set; }
public long ActualRebinds { get; set; }
public long ActualRewinds { get; set; }
public long ActualElapsedMs { get; set; }
public long ActualCPUMs { get; set; }
public long ActualLogicalReads { get; set; }
public long ActualPhysicalReads { get; set; }
public bool HasActualStats { get; set; }
// Parallelism
public bool Parallel { get; set; }
public int EstimatedDOP { get; set; }
public string? ExecutionMode { get; set; }
// Display
public string IconName { get; set; } = "iterator_catch_all";
public int CostPercent { get; set; }
public bool IsExpensive => CostPercent >= 25;
// Detail properties (for tooltip/properties panel)
public string? DatabaseName { get; set; }
public string? ObjectName { get; set; }
public string? FullObjectName { get; set; }
public string? IndexName { get; set; }
public string? SeekPredicates { get; set; }
public string? Predicate { get; set; }
public string? HashKeysProbe { get; set; }
public string? HashKeysBuild { get; set; }
public string? BuildResidual { get; set; }
public string? ProbeResidual { get; set; }
public string? OutputColumns { get; set; }
public bool Ordered { get; set; }
public string? PartitioningType { get; set; }
public string? StorageType { get; set; }
// RelOp-level properties
public bool Partitioned { get; set; }
public bool IsAdaptive { get; set; }
public double AdaptiveThresholdRows { get; set; }
public string? EstimatedJoinType { get; set; }
public string? ActualJoinType { get; set; }
public string? ActualExecutionMode { get; set; }
// Scan/Seek properties
public string? ScanDirection { get; set; }
public bool ForcedIndex { get; set; }
public bool ForceScan { get; set; }
public bool ForceSeek { get; set; }
public bool NoExpandHint { get; set; }
public bool Lookup { get; set; }
public bool DynamicSeek { get; set; }
// Operator-specific properties
public string? OrderBy { get; set; }
public string? OuterReferences { get; set; }
public string? InnerSideJoinColumns { get; set; }
public string? OuterSideJoinColumns { get; set; }
public string? GroupBy { get; set; }
public string? PartitionColumns { get; set; }
public string? DefinedValues { get; set; }
public double TableCardinality { get; set; }
public double EstimatedRowsRead { get; set; }
public string? TopExpression { get; set; }
public bool IsPercent { get; set; }
public bool WithTies { get; set; }
public bool ManyToMany { get; set; }
public bool BitmapCreator { get; set; }
public string? SetPredicate { get; set; }
public string? SegmentColumn { get; set; }
public bool SortDistinct { get; set; }
public bool StartupExpression { get; set; }
// Nested Loops properties
public bool NLOptimized { get; set; }
public bool WithOrderedPrefetch { get; set; }
public bool WithUnorderedPrefetch { get; set; }
// Parallelism properties
public bool Remoting { get; set; }
public bool LocalParallelism { get; set; }
// Extended actual I/O stats
public long ActualScans { get; set; }
public long ActualReadAheads { get; set; }
public long ActualLobLogicalReads { get; set; }
public long ActualLobPhysicalReads { get; set; }
public long ActualLobReadAheads { get; set; }
// Memory
public long? MemoryGrantKB { get; set; }
public long? DesiredMemoryKB { get; set; }
public long? MaxUsedMemoryKB { get; set; }
// Per-operator runtime memory grant (from RunTimeCountersPerThread)
public long InputMemoryGrantKB { get; set; }
public long OutputMemoryGrantKB { get; set; }
public long UsedMemoryGrantKB { get; set; }
// Warnings
public List<PlanWarning> Warnings { get; set; } = new();
public bool HasWarnings => Warnings.Count > 0;
// Tree structure
public List<PlanNode> Children { get; set; } = new();
public PlanNode? Parent { get; set; }
// Layout coordinates (set by layout engine)
public double X { get; set; }
public double Y { get; set; }
// Merge/NL residual + pass-thru, parallelism hash keys, Top extras
public string? MergeResidual { get; set; }
public string? PassThru { get; set; }
public string? HashKeys { get; set; }
public string? OffsetExpression { get; set; }
public bool RowCount { get; set; }
public int TopRows { get; set; }
// MemoryFractions, RunTimePartitionSummary, Spool, Update DML, Columnstore, UDF
public double MemoryFractionInput { get; set; }
public double MemoryFractionOutput { get; set; }
public int PartitionsAccessed { get; set; }
public string? PartitionRanges { get; set; }
public bool SpoolStack { get; set; }
public int PrimaryNodeId { get; set; }
public bool DMLRequestSort { get; set; }
public string? ActionColumn { get; set; }
public long ActualSegmentReads { get; set; }
public long ActualSegmentSkips { get; set; }
public long UdfCpuTimeMs { get; set; }
public long UdfElapsedTimeMs { get; set; }
// RelOp-level metadata
public bool GroupExecuted { get; set; }
public bool RemoteDataAccess { get; set; }
public bool OptimizedHalloweenProtectionUsed { get; set; }
public long StatsCollectionId { get; set; }
// Object metadata
public string? ServerName { get; set; }
public string? ObjectAlias { get; set; }
public string? IndexKind { get; set; }
public bool FilteredIndex { get; set; }
public int TableReferenceId { get; set; }
// Per-thread runtime data
public List<PerThreadRuntimeInfo> PerThreadStats { get; set; } = new();
// Operator-level properties
public int ForceSeekColumnCount { get; set; }
public string? PartitionId { get; set; }
public bool IsStarJoin { get; set; }
public string? StarJoinOperationType { get; set; }
public string? ProbeColumn { get; set; }
public bool InRow { get; set; }
public bool ComputeSequence { get; set; }
public int RollupHighestLevel { get; set; }
public List<int> RollupLevels { get; set; } = new();
public string? TvfParameters { get; set; }
public string? OriginalActionColumn { get; set; }
public List<ScalarUdfReference> ScalarUdfs { get; set; } = new();
public string? TieColumns { get; set; }
public string? UdxName { get; set; }
public List<string> OperatorIndexedViews { get; set; } = new();
public List<NamedParameterInfo> NamedParameters { get; set; } = new();
// Eager spool index suggestion
public string? SuggestedIndex { get; set; }
// Remote operator metadata
public string? RemoteDestination { get; set; }
public string? RemoteSource { get; set; }
public string? RemoteObject { get; set; }
public string? RemoteQuery { get; set; }
// GuessedSelectivity — optimizer guessed selectivity on predicates
public bool GuessedSelectivity { get; set; }
// ForeignKeyReferenceCheck attributes
public int ForeignKeyReferencesCount { get; set; }
public int NoMatchingIndexCount { get; set; }
public int PartialMatchingIndexCount { get; set; }
// Modification operator: nonclustered indexes maintained
public int NonClusteredIndexCount { get; set; }
public List<string> NonClusteredIndexNames { get; set; } = new();
// ConstantScan Values (parsed rows as displayable string)
public string? ConstantScanValues { get; set; }
// SpillOccurred detail flag (node-level, from Warnings element)
public bool SpillOccurredDetail { get; set; }
// UDX UsedUDXColumns (column references for CLR aggregate operators)
public string? UdxUsedColumns { get; set; }
// Row Goal: the optimizer's estimate before TOP/EXISTS reduced it
public double EstimateRowsWithoutRowGoal { get; set; }
}
public class MissingIndex
{
public string Database { get; set; } = "";
public string Schema { get; set; } = "";
public string Table { get; set; } = "";
public double Impact { get; set; }
public List<string> EqualityColumns { get; set; } = new();
public List<string> InequalityColumns { get; set; } = new();
public List<string> IncludeColumns { get; set; } = new();
public string CreateStatement { get; set; } = "";
}
public class PlanWarning
{
public string WarningType { get; set; } = "";
public string Message { get; set; } = "";
public PlanWarningSeverity Severity { get; set; }
public SpillDetail? SpillDetails { get; set; }
/// <summary>
/// Maximum percentage of elapsed time that could be saved by addressing this finding.
/// null = not quantifiable, 0 = calculated as negligible.
/// </summary>
public double? MaxBenefitPercent { get; set; }
/// <summary>
/// Short actionable fix suggestion (e.g., "Add INCLUDE (columns) to index").
/// </summary>
public string? ActionableFix { get; set; }
}
public enum PlanWarningSeverity { Info, Warning, Critical }
public class MemoryGrantInfo
{
public long SerialRequiredMemoryKB { get; set; }
public long SerialDesiredMemoryKB { get; set; }
public long RequiredMemoryKB { get; set; }
public long DesiredMemoryKB { get; set; }
public long RequestedMemoryKB { get; set; }
public long GrantedMemoryKB { get; set; }
public long MaxUsedMemoryKB { get; set; }
public long GrantWaitTimeMs { get; set; }
public long LastRequestedMemoryKB { get; set; }
public string? IsMemoryGrantFeedbackAdjusted { get; set; }
}
public class OptimizerHardwareInfo
{
public long EstimatedAvailableMemoryGrant { get; set; }
public long EstimatedPagesCached { get; set; }
public int EstimatedAvailableDOP { get; set; }
public long MaxCompileMemory { get; set; }
}
public class OptimizerStatsUsageItem
{
public string StatisticsName { get; set; } = "";
public string TableName { get; set; } = "";
public string? DatabaseName { get; set; }
public string? SchemaName { get; set; }
public long ModificationCount { get; set; }
public double SamplingPercent { get; set; }
public string? LastUpdate { get; set; }
}
public class ThreadStatInfo
{
public int Branches { get; set; }
public int UsedThreads { get; set; }
public List<ThreadReservation> Reservations { get; set; } = new();
}
public class SetOptionsInfo
{
public bool AnsiNulls { get; set; }
public bool AnsiPadding { get; set; }
public bool AnsiWarnings { get; set; }
public bool ArithAbort { get; set; }
public bool ConcatNullYieldsNull { get; set; }
public bool NumericRoundAbort { get; set; }
public bool QuotedIdentifier { get; set; }
}
public class PlanParameter
{
public string Name { get; set; } = "";
public string DataType { get; set; } = "";
public string? CompiledValue { get; set; }
public string? RuntimeValue { get; set; }
}
public class WaitStatInfo
{
public string WaitType { get; set; } = "";
public long WaitTimeMs { get; set; }
public long WaitCount { get; set; }
}
public class QueryTimeInfo
{
public long CpuTimeMs { get; set; }
public long ElapsedTimeMs { get; set; }
}
public class TraceFlagInfo
{
public int Value { get; set; }
public string Scope { get; set; } = "";
public bool IsCompileTime { get; set; }
}
public class CardinalityFeedbackEntry
{
public long Key { get; set; }
public long Value { get; set; }
}
public class ThreadReservation
{
public int NodeId { get; set; }
public int ReservedThreads { get; set; }
}
public class DispatcherInfo
{
public List<ParameterSensitivePredicateInfo> ParameterSensitivePredicates { get; set; } = new();
public List<OptionalParameterPredicateInfo> OptionalParameterPredicates { get; set; } = new();
}
public class OptionalParameterPredicateInfo
{
public string? PredicateText { get; set; }
}
public class ParameterSensitivePredicateInfo
{
public double LowBoundary { get; set; }
public double HighBoundary { get; set; }
public string? PredicateText { get; set; }
public List<OptimizerStatsUsageItem> Statistics { get; set; } = new();
}
public class FunctionPlanInfo
{
public string ProcName { get; set; } = "";
public bool IsNativelyCompiled { get; set; }
public List<PlanStatement> Statements { get; set; } = new();
}
public class PerThreadRuntimeInfo
{
public int ThreadId { get; set; }
public long ActualRows { get; set; }
public long ActualExecutions { get; set; }
public long ActualElapsedMs { get; set; }
public long ActualCPUMs { get; set; }
public long ActualRowsRead { get; set; }
public long ActualLogicalReads { get; set; }
public long ActualPhysicalReads { get; set; }
public long ActualScans { get; set; }
public long ActualReadAheads { get; set; }
// Timing metadata
public long FirstActiveTime { get; set; }
public long LastActiveTime { get; set; }
public long OpenTime { get; set; }
public long FirstRowTime { get; set; }
public long LastRowTime { get; set; }
public long CloseTime { get; set; }
// Per-thread memory
public long InputMemoryGrant { get; set; }
public long OutputMemoryGrant { get; set; }
public long UsedMemoryGrant { get; set; }
// Batch mode
public long Batches { get; set; }
public long ActualEndOfScans { get; set; }
// Other
public long ActualLocallyAggregatedRows { get; set; }
public bool IsInterleavedExecuted { get; set; }
public long RowRequalifications { get; set; }
}
public class SpillDetail
{
public string SpillType { get; set; } = ""; // "Sort", "Hash", "Exchange"
public long GrantedMemoryKB { get; set; }
public long UsedMemoryKB { get; set; }
public long WritesToTempDb { get; set; }
public long ReadsFromTempDb { get; set; }
}
public class ScalarUdfReference
{
public string FunctionName { get; set; } = "";
public bool IsClrFunction { get; set; }
public string? ClrAssembly { get; set; }
public string? ClrClass { get; set; }
public string? ClrMethod { get; set; }
}
public class NamedParameterInfo
{
public string Name { get; set; } = "";
public string? ScalarString { get; set; }
}