{"id":864,"date":"2010-04-22T00:00:00","date_gmt":"2010-04-22T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/showplan-operator-of-the-week-concatenation\/"},"modified":"2021-08-16T15:02:17","modified_gmt":"2021-08-16T15:02:17","slug":"showplan-operator-of-the-week-concatenation","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/learn\/showplan-operator-of-the-week-concatenation\/","title":{"rendered":"Showplan Operator of the Week &#8211; Concatenation"},"content":{"rendered":"<div id=\"pretty\">\n<p class=\"MsoNormal\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1010-Contatenation001.gif\" width=\"36\" height=\"36\" alt=\"1010-Contatenation001.gif\" \/> Concatenation<\/p>\n<p class=\"start\">Showplan Operators are used by SQL Server&#8217;s Query Optimizer (QO)&#160; to perform a particular 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. This week we&#8217;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 <b>UNION ALL<\/b> command.<\/p>\n<p class=\"MsoNormal\">Concatenation is a classic operator that can receive more than one input. It is both a logical and a physical operator.<\/p>\n<p class=\"MsoNormal\">Before we start to talk about concatenation, we need to understand some important points about showplan operators and execution plans.<\/p>\n<p class=\"MsoNormal\">All operators used in execution plans, implement three methods called <b>Init(),<\/b> <b>GetNext()<\/b> and <b>Close().<\/b> Some operators can receive more than one input, so, these inputs will be processed at the <b>Init()<\/b> method. The concatenation is one example of these operators.<\/p>\n<p class=\"MsoNormal\">At the <b>Init()<\/b> method, the concatenation will initialize itself and set up any required data structures. After that, it will run the GetNext() method to read the first or the subsequent row of the input data, it runs this method until it has read all rows from the input data.<\/p>\n<p class=\"MsoNormal\">Let&#8217;s take the following query as a sample:<\/p>\n<p class=\"MsoNormal\">The following script will create a table TabTeste and populate with some garbage data.<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE tempdb \nGO \nCREATE TABLE TABTeste(ID&#160;&#160; Int Identity(1,1) PRIMARY KEY, \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Nome VarChar(250)&#160;&#160;&#160;&#160;&#160; DEFAULT NewID()) \nGO \nSET NOCOUNT ON \nGO \nINSERT INTO TABTeste DEFAULT VALUES \nGO 10000 \n<\/pre>\n<p class=\"MsoNormal\">The script above will populate 10000 rows at the <b>TabTeste<\/b> table. Now let&#8217;s run one query sample to look at the execution plan.<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">SELECT * \n&#160; FROM TABTeste a \n&#160;INNER JOIN TABTeste b \n&#160;&#160;&#160; ON a.ID = b.ID \n<\/pre>\n<p class=\"MsoNormal\">Graphical execution plan:<\/p>\n<p class=\"MsoNormal\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1010-Contatenation002.jpg\" width=\"538\" height=\"230\" alt=\"1010-Contatenation002.jpg\" \/><\/p>\n<p class=\"MsoNormal\">\n<p class=\"MsoNormal\">Text execution plan:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">SELECT * FROM TABTeste a INNER JOIN TABTeste b ON a.ID = b.ID \n<\/pre>\n<\/p>\n<div class=\"scroller\">\n<pre>&#160;|--Merge Join(Inner Join, MERGE:([b].[ID])=([a].[ID]), RESIDUAL:([TABTeste].[ID] as [b].[ID]=[TABTeste].[ID] as [a].[ID]))&#160;&#160;&#160;&#160;&#160;&#160; |--Clustered Index Scan(OBJECT:([TABTeste].[PK_] AS [b]), ORDERED FORWARD) &#160;&#160;&#160;&#160;&#160;&#160; |--Clustered Index Scan(OBJECT:([TABTeste].[PK_] AS [a]), ORDERED FORWARD)\n\n<\/pre>\n<\/div>\n<p class=\"MsoNormal\">As we can see, this query is using one operator called <b>Merge<\/b> to join the tables, in the plan, the <b>Merge<\/b> operator is receiving two inputs (the table <b>TabTeste<\/b> twice).<\/p>\n<p class=\"MsoNormal\">The operator of the week, <b>Concatenation<\/b>, 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 fours inputs.<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">SELECT * FROM TABTeste \nUNION ALL \nSELECT * FROM TABTeste \nUNION ALL \nSELECT * FROM TABTeste \nUNION ALL \nSELECT * FROM TABTeste \n<\/pre>\n<p class=\"MsoNormal\">Graphical execution plan:<\/p>\n<p class=\"MsoNormal\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1010-Contatenation004.jpg\" width=\"571\" height=\"416\" alt=\"1010-Contatenation004.jpg\" \/><\/p>\n<\/p>\n<p class=\"MsoNormal\">Text execution plan:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">&#160; |--Concatenation \n&#160;&#160;&#160;&#160;&#160;&#160; |--Clustered Index Scan(OBJECT:([TABTeste].[PK_])) \n&#160;&#160;&#160;&#160;&#160;&#160; |--Clustered Index Scan(OBJECT:([TABTeste].[PK_])) \n&#160;&#160;&#160;&#160;&#160;&#160; |--Clustered Index Scan(OBJECT:([TABTeste].[PK_])) \n&#160;&#160;&#160;&#160;&#160;&#160; |--Clustered Index Scan(OBJECT:([TABTeste].[PK_]))\n<\/pre>\n<p class=\"MsoNormal\">The &#160;concatenation operator receives the result of all &#8220;<b>clustered index scan<\/b>&#8220;&#160; and copies all the rows to one output calling the methods <b>Init()<\/b> and <b>GetNext().<\/b> These methods are called to each Input.<\/p>\n<p class=\"MsoNormal\">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&#160; the end one.<\/p>\n<p class=\"MsoNormal\">That&#8217;s all folks, I see you next week with more &#8220;Showplan Operators&#8221;.<\/p>\n<p class=\"note\">If you missed last week&#8217;s thrilling Showplan Operator, The Assert, you can see it <a href=\"http:\/\/www.simple-talk.com\/sql\/learn-sql-server\/showplan-operator-of-the-week---assert\/\">here<\/a> <\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Fabiano continues in his mission to describe, one week at a time, all the major Showplan Operators used by SQL Server&#8217;s Query Optimiser to build the Query Plan. This week he gets the Concatenation operator .&hellip;<\/p>\n","protected":false},"author":65554,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143525],"tags":[4178,4149,5084,5162,4150,4151],"coauthors":[],"class_list":["post-864","post","type-post","status-publish","format-standard","hentry","category-learn","tag-bi","tag-learn-sql-server","tag-optimiser","tag-showplan","tag-sql","tag-sql-server"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/65554"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=864"}],"version-history":[{"count":4,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/864\/revisions"}],"predecessor-version":[{"id":92147,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/864\/revisions\/92147"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=864"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}