The cost is the same (1%) for both the slow and fast cases. Does that
mean the warning can be ignored? Is there a way to show "actual" times
or costs. That would be so much better! Actual row counts are the same
for the operation with the spill.
The cost shown is always the optimizer's estimated cost of the iterator, computed according to its internal model. This model does not reflect your server's particular performance characteristics; it is an abstraction that happens to produce reasonable plan shapes most of the time for most queries on most systems. There is no way to show 'actual' costs/execution times per iterator.
Besides performing a manual text diff of xml execution plans to find
the differences in warnings, how can I tell what the 1500% increase in
runtime is actually due to?
Typically, you can't. Spill warnings (sorts, hashes, exchanges) are new in execution plans for 2012, but they are just an indication of something you should investigate and look to eliminate if possible. The impact of a particular spill is something that needs to be measured - it is not possible to say that a spill of a particular type will always result in an x% performance drop for example.
For slow case, tempdb before/after (select *
sys.fn_virtualfilestats(db_id('tempdb'),null)) (only showing a few
100ms of latency)
Spilling to tempdb and back is certainly undesirable, but the overall impact is hard to assess. For sort and hash spills, the impact is largely due to the I/O and access pattern, which may be small-block synchronous I/O e.g. for sort spills. With ~100ms of latency, you don't need too many synchronous I/Os to introduce a significant delay. The nature of the process and I/O patterns means tempdb spills can still be a problem on very low latency storage systems like fusion-io.
For exchange spills, there is an extra delay. The intra-query deadlock must be detected by the regular deadlock monitor, which by default only wakes up once every 5 seconds (more frequently if a deadlock has been found recently).
The resolver must then choose one or more victims, and spool exchange buffers to tempdb until the deadlock is resolved. The amount of spooling needed and the complexity of the deadlock will largely determine how long this takes.
Ultimately, preserved ordering is a Very Bad Thing for parallelism in general. Ideally, we want multiple concurrent threads operating on data streams with no inter-dependence. Preserving sort order introduces dependencies, so producer and consumer threads in different parallel branches can become deadlocked waiting for order-preserving iterators to receive rows to decide which input sorts next in sequence.
The precise nature of the deadlock depends on data distribution and per-thread sort order at runtime, so it is typically very hard to debug. Hence my recommendation to avoid order-preserving iterators in parallel plans, especially at high DOP. I do explain a very simplified example of an order-preserving parallel deadlock in some talks I do, but real examples are always more complex, though the underlying cause is the same.
In case the concepts are not familiar, it may help to follow the following example, reproduced from the (somewhat epic) 1993 paper Query Evaluation Techniques for Large Databases by Goetz Graefe:
If a different partitioning strategy than range-partitioning is used,
sorting with subsequent partitioning is not guaranteed to be
deadlock-free in all situations. Deadlock will occur if (i) multiple
consumers feed multiple producers, and (ii) each producer produces a
sorted stream and each consumer merges multiple sorted streams, and
(iii) some key-based partitioning rule is used other than range
partitioning, i.e., hash partitioning, and (iv) flow control is
enabled, and (v) the data distribution is particularly unfortunate.
Figure 37 shows a scenario with two producer and two consumer
processes, i.e., both the producer operators and the consumer
operators are executed with a degree of parallelism of two. The
circles in Figure 37 indicate processes, and the arrows indicate data
paths. Presume that the left sort produces the stream 1, 3, 5, 7, ...,
999, 1002, 1004, 1006, 1008, ..., 2000 while the right sort produces
2, 4, 6, 8, ..., 1000, 1001, 1003, 1005, 1007, ..., 1999.
The merge operations in the consumer processes must receive the first
item from each producer process before they can create their first
output item and remove additional items from their input buffers.
However, the producers will need to produce 500 items each (and insert
them into one consumer’s input buffer, all 500 for one consumer)
before they will send their first item to the other consumer. The data
exchange buffer needs to hold 1000 items at one point of time, 500 on
each side of Figure 37. If flow control is enabled and the exchange
buffer (flow control slack) is less than 500 items, deadlock will
occur.
The reason deadlock can occur in this situation is that the producer
processes need to ship data in the order obtained from their input
subplan (the sort in Figure 37) while the consumer processes need to
receive data in sorted order as required by the merge. Thus, there are
two sides which both require absolute control over the order in which
data pass over the process boundary. If the two requirements are
incompatible, an unbounded buffer is required to ensure freedom from
deadlock.
Best Answer
No, there is no documentation from Microsoft guaranteeing the behavior, therefore it is not guaranteed.
Additionally, assuming that the Simple Talk article is correct, and that the Concatenation physical operator always processes inputs in the order shown in the plan (very likely to be true), then without a guarantee that SQL Server will always generate plans that keep the same the order between the query text and the query plan, you're only slightly better off.
We can investigate this further though. If the query optimizer was able to reorder the Concatenation operator input, there should exist rows in the undocumented DMV,
sys.dm_exec_query_transformation_stats
corresponding to that optimization.On SQL Server 2012 Enterprise Edition, this produces 24 rows. Ignoring the false matches for transformations related to constants, there is one transformation related to the Concatenation Physical Operator
UNIAtoCON
(Union All to Concatenation). So, at the physical operator level, it appears that once a concatenation operator is selected, it will be processed in the order of the logical Union All operator it was derived from.In fact that is not quite true. Post-optimization rewrites exist that can reorder the inputs to a physical Concatenation operator after cost-based optimization has completed. One example occurs when the Concatenation is subject to a row goal (so it may be important to read from the cheaper input first). See
UNION ALL
Optimization by Paul White for more details.That late physical rewrite was functional up to and including SQL Server 2008 R2, but a regression meant it no longer applied to SQL Server 2012 and later. A fix has been issued that reinstates this rewrite for SQL Server 2014 and later (not 2012) with query optimizer hotfixes enabled (e.g. trace flag 4199).
But about the Logical Union All operator (
UNIA
)? There is aUNIAReorderInputs
transformation, which can reorder the inputs. There are also two physical operators that can be used to implement a logical Union All,UNIAtoCON
andUNIAtoMERGE
(Union All to Merge Union).Therefore it appears that the query optimizer can reorder the inputs for a
UNION ALL
; however, it doesn't appear to be a common transformation (zero uses ofUNIAReorderInputs
on the SQL Servers I have readily accessible. We don't know the circumstances that would make the optimizer useUNIAReorderInputs
; though it is certainly used when a plan guide or use plan hint is used to force a plan generated using the row goal physical reordered inputs mentioned above.The Concatenation physical operator can exist within a parallel section of a plan. With some difficulty, I was able to produce a plan with parallel concatenations using the following query:
So, in the strictest sense, the physical Concatenation operator does seem to always process inputs in a consistent fashion (top one first, bottom second); however, the optimizer could switch the order of the inputs before choosing the physical operator, or use a Merge union instead of a Concatenation.