You are on page 1of 9

Concatenation Physical Operation: Does it

guarantee order of execution?


Ask Question
Asked 7 years, 3 months ago
Active 2 years, 10 months ago
Viewed 1k times
12
1
In standard SQL, the result of a union all is not guaranteed to be in any order. So, something like:
select 'A' as c union all select 'B'
Could return two rows in any order (although, in practice on any database I know of, 'A' will come
before 'B').

In SQL Server, this turns into an execution plan using a "concatenation" physical operation.

I could easily imagine that the concatenation operation would scan its inputs, returning whatever
input has records available. However, I found the following statement on the web (here):
The Query Processor will execute this plan in the order that the operators appear in the plan, the first
is the top one and the last is the end one.
Question: Is this true in practice? Is this guaranteed to be true?

I haven't found any reference in Microsoft documentation that the inputs are scanned in order, from
the first to the last. On the other hand, whenever I try running it, the results suggest that the inputs
are, indeed, processed in order.

Is there a way to have the engine process more than one input at a time? My tests (using much more
complicated expressions than constants) are on a parallel-enabled 8-core machine, and most queries
do take advantage of the parallelism.

sql-server execution-plan union database-internals

share improve this question follow


edited Aug 9 '17 at 12:50

Paul White 9♦
54.8k1616 gold badges336336 silver badges515515 bronze badges
asked Apr 2 '13 at 18:40

Gordon Linoff
2,03411 gold badge1313 silver badges1717 bronze badges
add a comment
3 Answers

ActiveOldestVotes

10
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.
SELECT * FROM sys.dm_exec_query_transformation_stats
WHERE name LIKE '%CON%' OR name LIKE '%UNIA%'
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 a UNIAReorderInputs transformation,
which can reorder the inputs. There are also two physical operators that can be used to implement a
logical Union All, UNIAtoCON and UNIAtoMERGE (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 of UNIAReorderInputs on the SQL Servers
I have readily accessible. We don't know the circumstances that would make the optimizer
use UNIAReorderInputs; 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.
Is there a way to have the engine process more than one input at a time?
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:

SELECT userid, regdate FROM ( --Users table is around 3mil rows


SELECT userid, RegDate FROM users WHERE userid > 1000000
UNION
SELECT userid, RegDate FROM users WHERE userid < 1000000
UNION all
SELECT userid, RegDate FROM users WHERE userid < 2000000
) d ORDER BY RegDate OPTION (RECOMPILE)
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.

share improve this answer follow


edited Aug 9 '17 at 12:36

Paul White 9♦
54.8k1616 gold badges336336 silver badges515515 bronze badges
answered Apr 8 '13 at 16:55

StrayCatDBA
1,7171212 silver badges1515 bronze badges
add a comment
8
According to Craig Freedman the order of execution for the concatenation operator is guaranteed.
From his blog post Viewing Query Plans on MSDN Blogs:
Note that when an operator has more than one child, the order of the children matters. The topmost
child is the first child while the bottommost child is the second. The concatenation operator processes
the children in this order.
And from books online Showplan Logical and Physical Operators Reference
The Concatenation physical operator has two or more inputs and one output. Concatenation copies
rows from the first input stream to the output stream, then repeats this operation for each additional
input stream.
share improve this answer follow
edited Jun 22 '14 at 17:24
answered Jun 22 '14 at 12:30

Mikael Eriksson
20.3k44 gold badges5151 silver badges9393 bronze badges
 That quote is pretty close to what I was looking for. I am willing to take the leap from being executed in
that order to being returned in that order -- although it is disappointing that the documentation precludes
parallel processing in this case. – Gordon Linoff Jun 23 '14 at 6:00
add a comment
3
Community wiki answer:
I don't know if you can prove that any observed behavior is always guaranteed, one way or the other,
unless you can manufacture a counter-example. In the absence of that, the way to fix the order that
results are returned, of course, is to add an ORDER BY.
I don't know if there is a "fix", or that there exists a need for a fix, if you can demonstrate that in
some scenarios the queries are processed in a different order.

The lack of any explicit, official documentation suggests to me that you should not depend on this.
This is exactly the kind of thing that got people into trouble with ORDER BY in a view, and GROUP
BY without ORDER BY, 8 years ago when SQL Server 2005's optimizer was released.
With all of the new features in the newer versions of SQL Server (with more coming), even if you
think you can guarantee a specific behavior today, I wouldn't expect it to hold true (until it is
documented to do so).

Even if you're not depending on this behavior, what are you going to do with the results? Anyway, I
wouldn't call a Simple Talk article by an outsider official. For all we know this is just a guess based
on observation.
Microsoft is never going to publish official documentation saying 'x' is not guaranteed to do 'y'. This
is one of the reasons we still, almost a decade later, have trouble convincing people that they can't
rely on observed ordering without ORDER BY - there is no documentation that states "it is not
guaranteed."
share improve this answer follow
edited Aug 9 '17 at 13:05
community wiki

2 revs
user126897
add a comment
Your Answer
















Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password

Post as a guest
Name

Email
Required, but never shown

Post Your Answer


By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged sql-server execution-

plan union database-internals or ask your own question.


The Overflow Blog
 The Overflow #27: A simulation
 Does scrum ruin great engineers or are you doing it wrong?
Featured on Meta

We're switching to CommonMark


New post lock available on meta sites: Policy Lock


Jobs near you

Senior Software Developer


Titansoft Pte LtdSingapore
SGD 72K - SGD 132K
c#.netcore

Blockchain Developer
Plus65 Interactive Pte LtdSingapore
gitmysql

AI Full-Stack Engineer
Amaris.AISingapore
SGD 72K - SGD 120K
pythonjava

Backend Software Engineer (Golang), APAC


foodpandaSingapore
RELOCATION
gophp

Engineering Manager (Golang) - APAC


foodpandaSingapore
goagile

Software Developer
Titansoft Pte LtdSingapore
SGD 48K - SGD 66K
c#.net-core

Solutions Architect
Time AccessSingapore
SGD 72K - SGD 108K
javascriptreact.js


Database Administrator
iAPPSSingapore
SGD 60K - SGD 84K
mysqldatabase

View more jobs on Stack Overflow

Linked

11
order of clauses in “EXISTS (…) OR EXISTS (…)”

2
Backend Table/Row Locking Structure using CTE Queries

Related

2
stored procedure times out while subsequent runs take 1/6 the time

26
How to efficiently check EXISTS on multiple columns?

20
T-SQL query using completely different plan depending on number of rows I'm updating

3
How to determine cause of runtime increase given two query plans with SpillToTempDb warning

20
Execution plan vs STATISTICS IO order

6
Actual rows read in a table scan is multiplied by the number of threads used for the scan

10
Strange query plan when using OR in JOIN clause - Constant scan for every row in table

4
Reading 300% rows - Problematic execution plan

Hot Network Questions


 My botched ordering task
 Split paper into two, one was accepted, is it fine to negotiate the rejected paper to be accepted?
 What is the cutting edge for open-source Force-Field generation?
 Is Python a viable language to do statistical analysis in?
 How and when did the word "nuclear" replace the word "atomic"?
 Can a Changeling with the actor feat have permanent advantage and performance and Deception checks?
 Should non-priority technical debt tickets be pruned from backlog?
 Creating all possible line segments between all points
 Bash script variable with comma seperated values is not comma seperated when appended to a different file
 Why pkexec does not launch Qt-based application as root user?
 Why does a ping pong ball bounce higher when it is dropped together with a cup of water?
 How can we conclude from Maxwell's wave equation that the speed of light is the same regardless of the state of motion of
the observers?
 Understanding gate threshold voltage of logic level mosfets from datasheet
 How to let users know that the updates are not real time
 How to protect my code from “insider” threats when hiring my first employee?
 Does casting dimension door or compulsion with the magic dagger Bookmark expend your bonus action and your action?
 Why does the T-1000 kill the trucker?
 Consistency between "yes and" with "no, because I'm the DM"
 4k reputation special: "I hate square numbers!"
 AC Delco | Should this be worrying me? The battery is just 9 months old
 Can a 1.8V linear regulator be avoided with 5V power and 3.3V?
 Etiquette for sparring with women - How to find the balance and not do disservice?
 Why do bike tires suffer from frequent punctures whereas car tires don't?
 What is 吗 doing in 你有什么事吗?

Question feed

DATABASE ADMINISTRATORS
 Tour
 Help
 Chat
 Contact
 Feedback
 Mobile
 Disable Responsiveness
COMPANY
 Stack Overflow
 Stack Overflow Business
 Developer Jobs
 About
 Press
 Legal
 Privacy Policy
STACK EXCHANGE
NETWORK
 Technology
 Life / Arts
 Culture / Recreation
 Science
 Other
 Blog
 Facebook
 Twitter
 LinkedIn
 Instagram

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2020.6.29.37151

You might also like