forked from JustinPealing/html-query-plan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qp.xslt
437 lines (393 loc) · 19.4 KB
/
qp.xslt
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
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:s="http://schemas.microsoft.com/sqlserver/2004/07/showplan"
exclude-result-prefixes="msxsl s xsl">
<xsl:output method="html" indent="no" omit-xml-declaration="yes" />
<!-- Disable built-in recursive processing templates -->
<xsl:template match="*|/|text()|@*" mode="NodeLabel2" />
<xsl:template match="*|/|text()|@*" mode="ToolTipDescription" />
<xsl:template match="*|/|text()|@*" mode="ToolTipDetails" />
<!-- Default template -->
<xsl:template match="/">
<xsl:apply-templates select="s:ShowPlanXML" />
</xsl:template>
<!-- Outermost div that contains all statement plans. -->
<xsl:template match="s:ShowPlanXML">
<div class="qp-root">
<xsl:apply-templates select="s:BatchSequence/s:Batch/s:Statements/s:StmtSimple" />
</div>
</xsl:template>
<!-- Matches a branch in the query plan (either an operation or a statement) -->
<xsl:template match="s:RelOp|s:StmtSimple">
<div class="qp-tr">
<div>
<div class="qp-node">
<xsl:apply-templates select="." mode="NodeIcon" />
<xsl:apply-templates select="." mode="NodeLabel" />
<xsl:apply-templates select="." mode="NodeLabel2" />
<xsl:apply-templates select="." mode="NodeCostLabel" />
<xsl:call-template name="ToolTip" />
</div>
</div>
<div><xsl:apply-templates select="*/s:RelOp" /></div>
</div>
</xsl:template>
<!-- Writes the tool tip -->
<xsl:template name="ToolTip">
<div class="qp-tt">
<div class="qp-tt-header"><xsl:value-of select="@PhysicalOp | @StatementType" /></div>
<div><xsl:apply-templates select="." mode="ToolTipDescription" /></div>
<xsl:call-template name="ToolTipGrid" />
<xsl:apply-templates select="* | @* | */* | */@*" mode="ToolTipDetails" />
</div>
</xsl:template>
<!-- Writes the grid of node properties to the tool tip -->
<xsl:template name="ToolTipGrid">
<table>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Condition" select="s:QueryPlan/@CachedPlanSize" />
<xsl:with-param name="Label">Cached plan size</xsl:with-param>
<xsl:with-param name="Value" select="concat(s:QueryPlan/@CachedPlanSize, ' B')" />
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Label">Physical Operation</xsl:with-param>
<xsl:with-param name="Value" select="@PhysicalOp" />
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Label">Logical Operation</xsl:with-param>
<xsl:with-param name="Value" select="@LogicalOp" />
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Label">Actual Number of Rows</xsl:with-param>
<xsl:with-param name="Value" select="s:RunTimeInformation/s:RunTimeCountersPerThread/@ActualRows" />
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Condition" select="@EstimateIO" />
<xsl:with-param name="Label">Estimated I/O Cost</xsl:with-param>
<xsl:with-param name="Value">
<xsl:call-template name="round">
<xsl:with-param name="value" select="@EstimateIO" />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Condition" select="@EstimateCPU" />
<xsl:with-param name="Label">Estimated CPU Cost</xsl:with-param>
<xsl:with-param name="Value">
<xsl:call-template name="round">
<xsl:with-param name="value" select="@EstimateCPU" />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
<!-- TODO: Estimated Number of Executions -->
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Label">Number of Executions</xsl:with-param>
<xsl:with-param name="Value" select="s:RunTimeInformation/s:RunTimeCountersPerThread/@ActualExecutions" />
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Label">Degree of Parallelism</xsl:with-param>
<xsl:with-param name="Value" select="s:QueryPlan/@DegreeOfParallelism" />
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Label">Memory Grant</xsl:with-param>
<xsl:with-param name="Value" select="s:QueryPlan/@MemoryGrant" />
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Condition" select="@EstimateIO | @EstimateCPU" />
<xsl:with-param name="Label">Estimated Operator Cost</xsl:with-param>
<xsl:with-param name="Value">
<xsl:variable name="EstimatedOperatorCost">
<xsl:call-template name="EstimatedOperatorCost" />
</xsl:variable>
<xsl:variable name="TotalCost">
<xsl:value-of select="ancestor::s:StmtSimple/@StatementSubTreeCost" />
</xsl:variable>
<xsl:call-template name="round">
<xsl:with-param name="value" select="$EstimatedOperatorCost" />
</xsl:call-template>
(<xsl:value-of select="format-number(number($EstimatedOperatorCost) div number($TotalCost), '0%')" />)
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Condition" select="@StatementSubTreeCost | @EstimatedTotalSubtreeCost" />
<xsl:with-param name="Label">Estimated Subtree Cost</xsl:with-param>
<xsl:with-param name="Value">
<xsl:call-template name="round">
<xsl:with-param name="value" select="@StatementSubTreeCost | @EstimatedTotalSubtreeCost" />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Label">Estimated Number of Rows</xsl:with-param>
<xsl:with-param name="Value" select="@StatementEstRows | @EstimateRows" />
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Condition" select="@AvgRowSize" />
<xsl:with-param name="Label">Estimated Row Size</xsl:with-param>
<xsl:with-param name="Value" select="concat(@AvgRowSize, ' B')" />
</xsl:call-template>
<!-- TODO: Actual Rebinds
TODO: Actual Rewinds -->
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Condition" select="s:IndexScan/@Ordered" />
<xsl:with-param name="Label">Ordered</xsl:with-param>
<xsl:with-param name="Value">
<xsl:choose>
<xsl:when test="s:IndexScan/@Ordered = 1">True</xsl:when>
<xsl:otherwise>False</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="ToolTipRow">
<xsl:with-param name="Label">Node ID</xsl:with-param>
<xsl:with-param name="Value" select="@NodeId" />
</xsl:call-template>
</table>
</xsl:template>
<!-- Calculates the estimated operator cost. -->
<xsl:template name="EstimatedOperatorCost">
<xsl:variable name="EstimateIO">
<xsl:call-template name="convertSciToNumString">
<xsl:with-param name="inputVal" select="@EstimateIO" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="EstimateCPU">
<xsl:call-template name="convertSciToNumString">
<xsl:with-param name="inputVal" select="@EstimateCPU" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="number($EstimateIO) + number($EstimateCPU)" />
</xsl:template>
<!-- Renders a row in the tool tip details table. -->
<xsl:template name="ToolTipRow">
<xsl:param name="Label" />
<xsl:param name="Value" />
<xsl:param name="Condition" select="$Value" />
<xsl:if test="$Condition">
<tr>
<th><xsl:value-of select="$Label" /></th>
<td><xsl:value-of select="$Value" /></td>
</tr>
</xsl:if>
</xsl:template>
<!-- Prints the name of an object. -->
<xsl:template match="s:Object | s:ColumnReference" mode="ObjectName">
<xsl:param name="ExcludeDatabaseName" select="false()" />
<xsl:choose>
<xsl:when test="$ExcludeDatabaseName">
<xsl:for-each select="@Table | @Index | @Column | @Alias">
<xsl:value-of select="." />
<xsl:if test="position() != last()">.</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="@Database | @Schema | @Table | @Index | @Column | @Alias">
<xsl:value-of select="." />
<xsl:if test="position() != last()">.</xsl:if>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Displays the node cost label. -->
<xsl:template match="s:RelOp" mode="NodeCostLabel">
<xsl:variable name="EstimatedOperatorCost"><xsl:call-template name="EstimatedOperatorCost" /></xsl:variable>
<xsl:variable name="TotalCost"><xsl:value-of select="ancestor::s:StmtSimple/@StatementSubTreeCost" /></xsl:variable>
<div>Cost: <xsl:value-of select="format-number(number($EstimatedOperatorCost) div number($TotalCost), '0%')" /></div>
</xsl:template>
<!-- Dont show the node cost for statements. -->
<xsl:template match="s:StmtSimple" mode="NodeCostLabel" />
<!--
================================
Tool tip detail sections
================================
The following section contains templates used for writing the detail sections at the bottom of the tool tip,
for example listing outputs, or information about the object to which an operator applies.
-->
<xsl:template match="*/s:Object" mode="ToolTipDetails">
<!-- TODO: Make sure this works all the time -->
<div class="qp-bold">Object</div>
<div><xsl:apply-templates select="." mode="ObjectName" /></div>
</xsl:template>
<xsl:template match="s:SetPredicate[s:ScalarOperator/@ScalarString]" mode="ToolTipDetails">
<div class="qp-bold">Predicate</div>
<div><xsl:value-of select="s:ScalarOperator/@ScalarString" /></div>
</xsl:template>
<xsl:template match="s:OutputList[count(s:ColumnReference) > 0]" mode="ToolTipDetails">
<div class="qp-bold">Output List</div>
<xsl:for-each select="s:ColumnReference">
<div><xsl:apply-templates select="." mode="ObjectName" /></div>
</xsl:for-each>
</xsl:template>
<xsl:template match="s:NestedLoops/s:OuterReferences[count(s:ColumnReference) > 0]" mode="ToolTipDetails">
<div class="qp-bold">Outer References</div>
<xsl:for-each select="s:ColumnReference">
<div><xsl:apply-templates select="." mode="ObjectName" /></div>
</xsl:for-each>
</xsl:template>
<xsl:template match="@StatementText" mode="ToolTipDetails">
<div class="qp-bold">Statement</div>
<div><xsl:value-of select="." /></div>
</xsl:template>
<xsl:template match="s:Sort/s:OrderBy[count(s:OrderByColumn/s:ColumnReference) > 0]" mode="ToolTipDetails">
<div class="qp-bold">Order By</div>
<xsl:for-each select="s:OrderByColumn">
<div>
<xsl:apply-templates select="s:ColumnReference" mode="ObjectName" />
<xsl:choose>
<xsl:when test="@Ascending = 1"> Ascending</xsl:when>
<xsl:otherwise> Descending</xsl:otherwise>
</xsl:choose>
</div>
</xsl:for-each>
</xsl:template>
<!-- TODO: Seek Predicates -->
<!--
================================
Node icons
================================
The following templates determine what icon should be shown for a given node
-->
<!-- Use the logical operation to determine the icon for the "Parallelism" operators. -->
<xsl:template match="s:RelOp[@PhysicalOp = 'Parallelism']" mode="NodeIcon" priority="1">
<xsl:element name="div">
<xsl:attribute name="class">qp-icon-<xsl:value-of select="translate(@LogicalOp, ' ', '')" /></xsl:attribute>
</xsl:element>
</xsl:template>
<!-- Use the physical operation to determine icon if it is present. -->
<xsl:template match="*[@PhysicalOp]" mode="NodeIcon">
<xsl:element name="div">
<xsl:attribute name="class">qp-icon-<xsl:value-of select="translate(@PhysicalOp, ' ', '')" /></xsl:attribute>
</xsl:element>
</xsl:template>
<!-- Matches all statements. -->
<xsl:template match="s:StmtSimple" mode="NodeIcon">
<div class="qp-icon-Statement"></div>
</xsl:template>
<!-- Fallback template - show the Bitmap icon. -->
<xsl:template match="*" mode="NodeIcon">
<div class="qp-icon-Catchall"></div>
</xsl:template>
<!--
================================
Node labels
================================
The following section contains templates used to determine the first (main) label for a node.
-->
<xsl:template match="s:RelOp" mode="NodeLabel">
<div><xsl:value-of select="@PhysicalOp" /></div>
</xsl:template>
<xsl:template match="s:StmtSimple" mode="NodeLabel">
<div><xsl:value-of select="@StatementType" /></div>
</xsl:template>
<!--
================================
Node alternate labels
================================
The following section contains templates used to determine the second label to be displayed for a node.
-->
<!-- Display the object for any node that has one -->
<xsl:template match="*[*/s:Object]" mode="NodeLabel2">
<xsl:variable name="ObjectName">
<xsl:apply-templates select="*/s:Object" mode="ObjectName">
<xsl:with-param name="ExcludeDatabaseName" select="true()" />
</xsl:apply-templates>
</xsl:variable>
<div>
<xsl:value-of select="substring($ObjectName, 0, 36)" />
<xsl:if test="string-length($ObjectName) >= 36">…</xsl:if>
</div>
</xsl:template>
<!-- Display the logical operation for any node where it is not the same as the physical operation. -->
<xsl:template match="s:RelOp[@LogicalOp != @PhysicalOp]" mode="NodeLabel2">
<div>(<xsl:value-of select="@LogicalOp" />)</div>
</xsl:template>
<!-- Disable the default template -->
<xsl:template match="*" mode="NodeLabel2" />
<!--
================================
Tool tip descriptions
================================
The following section contains templates used for writing the description shown in the tool tip.
-->
<xsl:template match="*[@PhysicalOp = 'Table Insert']" mode="ToolTipDescription">Insert input rows into the table specified in Argument field.</xsl:template>
<xsl:template match="*[@PhysicalOp = 'Compute Scalar']" mode="ToolTipDescription">Compute new values from existing values in a row.</xsl:template>
<xsl:template match="*[@PhysicalOp = 'Sort']" mode="ToolTipDescription">Sort the input.</xsl:template>
<xsl:template match="*[@PhysicalOp = 'Clustered Index Scan']" mode="ToolTipDescription">Scanning a clustered index, entirely or only a range.</xsl:template>
<xsl:template match="*[@PhysicalOp = 'Stream Aggregate']" mode="ToolTipDescription">Compute summary values for groups of rows in a suitably sorted stream.</xsl:template>
<xsl:template match="*[@PhysicalOp = 'Hash Match']" mode="ToolTipDescription">Use each row from the top input to build a hash table, and each row from the bottom input to probe into the hash table, outputting all matching rows.</xsl:template>
<xsl:template match="*[@PhysicalOp = 'Bitmap']" mode="ToolTipDescription">Bitmap.</xsl:template>
<xsl:template match="*[@PhysicalOp = 'Clustered Index Seek']" mode="ToolTipDescription">Scanning a particular range of rows from a clustered index.</xsl:template>
<xsl:template match="*[@PhysicalOp = 'Index Seek']" mode="ToolTipDescription">Scan a particular range of rows from a nonclustered index.</xsl:template>
<xsl:template match="*[@PhysicalOp = 'Parallelism' and @LogicalOp='Repartition Streams']" mode="ToolTipDescription">Repartition Streams.</xsl:template>
<xsl:template match="*[@PhysicalOp = 'Parallelism']" mode="ToolTipDescription">An operation involving parallelism.</xsl:template>
<xsl:template match="*[s:TableScan]" mode="ToolTipDescription">Scan rows from a table.</xsl:template>
<xsl:template match="*[s:NestedLoops]" mode="ToolTipDescription">For each row in the top (outer) input, scan the bottom (inner) input, and output matching rows.</xsl:template>
<xsl:template match="*[s:Top]" mode="ToolTipDescription">Select the first few rows based on a sort order.</xsl:template>
<!--
================================
Number handling
================================
The following section contains templates used for handling numbers (scientific notation, rounding etc...)
-->
<!-- Outputs a number rounded to 7 decimal places - to be used for displaying all numbers.
This template accepts numbers in scientific notation. -->
<xsl:template name="round">
<xsl:param name="value" select="0" />
<xsl:variable name="number">
<xsl:call-template name="convertSciToNumString">
<xsl:with-param name="inputVal" select="$value" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="round(number($number) * 10000000) div 10000000" />
</xsl:template>
<!-- Template for handling of scientific numbers
See: http://www.orm-designer.com/article/xslt-convert-scientific-notation-to-decimal-number -->
<xsl:variable name="max-exp">
<xsl:value-of select="'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'" />
</xsl:variable>
<xsl:template name="convertSciToNumString">
<xsl:param name="inputVal" select="0" />
<xsl:variable name="numInput">
<xsl:value-of select="translate(string($inputVal),'e','E')" />
</xsl:variable>
<xsl:choose>
<xsl:when test="number($numInput) = $numInput">
<xsl:value-of select="$numInput" />
</xsl:when>
<xsl:otherwise>
<!-- ==== Mantisa ==== -->
<xsl:variable name="numMantisa">
<xsl:value-of select="number(substring-before($numInput,'E'))" />
</xsl:variable>
<!-- ==== Exponent ==== -->
<xsl:variable name="numExponent">
<xsl:choose>
<xsl:when test="contains($numInput,'E+')">
<xsl:value-of select="substring-after($numInput,'E+')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-after($numInput,'E')" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- ==== Coefficient ==== -->
<xsl:variable name="numCoefficient">
<xsl:choose>
<xsl:when test="$numExponent > 0">
<xsl:text>1</xsl:text>
<xsl:value-of select="substring($max-exp, 1, number($numExponent))" />
</xsl:when>
<xsl:when test="$numExponent < 0">
<xsl:text>0.</xsl:text>
<xsl:value-of select="substring($max-exp, 1, -number($numExponent)-1)" />
<xsl:text>1</xsl:text>
</xsl:when>
<xsl:otherwise>1</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="number($numCoefficient) * number($numMantisa)" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>