【Complete Showplan Operators】Chapter 2: Concatenation

Showplan operators are used by SQL Server's Query Optimizer (QO) to perform a particular(adj. 特别的; 详细的; 独有的; 挑剔的;) operation within a query plan. A query plan will usually contain several of these physical operators. Each physical operation is represented in the Query Plan by an operator, and each operator is shown in the graphical execution plan by an icon. In this chapter, we'll be featuring the Concatenation Showplan operator. Its behavior is quite simple; it receives one or more input streams and returns all the rows from each input stream in turn. We can see its effect whenever we use the Transact–SQL UNION ALL command.

Concatenation is a classic operator that can receive more than one input. It is both a logical and a physical operator.

Before we start to talk about concatenation, we need to understand some important points about Showplan operators and execution plans.

All operators used in execution plans, implement three methods called Init(), GetNext() and Close(). Some operators can receive more than one input, so, these inputs will be processed at the Init() method. The concatenation is one example of these operators. 

 At the Init() method, the concatenation will initialize itself and set up any required data structures. After that, it will run the GetNext() method to read the frst or the subsequent(adj. 随后的; 后来的; 作为结果而发生的; 附随的;) row of the input data, it runs this method until it has read all rows from the input data.

Let's take the following query as a sample:
The following script will create a table TabTeste and populate with some garbage data.

USE tempdb
GO
CREATE TABLE TABTeste(ID Int Identity(1,1) PRIMARY KEY,
Nome VarChar(250) DEFAULT NewID())
GO
SET NOCOUNT ON
GO
INSERT INTO TABTeste DEFAULT VALUES
GO 10000

The script above will populate 10000 rows at the TabTeste table. Now let's run one query sample to look at the execution plan.

SELECT *
FROM TABTeste a
INNER JOIN TABTeste b
ON a.ID = b.ID

Graphical execution plan. 

SELECT * FROM TABTeste a INNER JOIN TABTeste b ON a.ID = b.ID
|--Merge Join(Inner Join, MERGE:([b].[ID])=([a].[ID]), RESIDUAL:([TABTeste].[ID] as [b].[ID]=[TABTeste].[ID] as [a].[ID]))
|--Clustered Index Scan(OBJECT:([TABTeste].[PK_] AS [b]), ORDERED FORWARD)
|--Clustered Index Scan(OBJECT:([TABTeste].[PK_] AS [a]), ORDERED FORWARD)

Text execution plan.

 As we can see, this query is using one operator called Merge to join the tables, in the plan,the Merge operator is receiving two inputs (the table TabTeste twice).
Concatenation is a good example of an operator that receives more than one input. If, for example, we run the following query, we will see that it receives four inputs.  

SELECT * FROM TABTeste
UNION ALL
SELECT * FROM TABTeste
UNION ALL
SELECT * FROM TABTeste
UNION ALL
SELECT * FROM TABTeste

 Graphical execution plan. 

|--Concatenation
|--Clustered Index Scan(OBJECT:([TABTeste].[PK_]))
|--Clustered Index Scan(OBJECT:([TABTeste].[PK_]))
|--Clustered Index Scan(OBJECT:([TABTeste].[PK_]))
|--Clustered Index Scan(OBJECT:([TABTeste].[PK_]))

Text execution plan.

The concatenation operator receives the result of all clustered index scan and copies all the rows to one output calling the methods Init() and GetNext(). These methods are called to each Input.
The Query Processor will execute this plan in the order that the operators appear in the plan, the frst is the top one and the last is the end one.

posted @ 2017-07-11 16:36  FH1004322  阅读(93)  评论(0)    收藏  举报