{"id":428,"date":"2008-01-29T20:06:04","date_gmt":"2008-01-29T20:06:04","guid":{"rendered":"\/blogs\/conor\/post\/Operator-of-the-Day-Sequence-Project.aspx"},"modified":"2013-01-01T19:22:42","modified_gmt":"2013-01-01T19:22:42","slug":"operator-of-the-day-sequence-project","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/","title":{"rendered":"Operator of the Day: Sequence Project"},"content":{"rendered":"<p>So I&#8217;ll be posting explanations for each query operator in the output query trees that you can see through &#8220;set showplan_text on&#8221; in SQL Server.&nbsp; I&#8217;m hopeful that this will give you the tools to better read the query plans being generated by the QP when evaluating your system.<\/p>\n<p>So I&#8217;ll start with one that I added into the optimizer &#8211; the &#8220;sequence project&#8221;.<\/p>\n<pre><span style=\"color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;\"><span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">create<\/span> <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">table<\/span> z(col1 <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">int<\/span> <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">identity<\/span>, col2 <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">int<\/span> <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">default<\/span> <span style=\"color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;\">rand<\/span>()*10000)\r\n\r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">declare<\/span> @i <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">int<\/span> \r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">set<\/span> @i=0\r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">while<\/span> @i &lt; 1000\r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">begin<\/span>\r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">insert<\/span> <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">into<\/span> z <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">default<\/span> <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">values<\/span>\r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">set<\/span> @i=@i+1\r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">end<\/span>\r\n\r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">select<\/span> RANK() <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">over<\/span>(<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">order<\/span> <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">by<\/span> col2), * <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">from<\/span> z<\/span><\/pre>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.sqlskills.com\/blogs\/conor\/content\/binary\/rank_plan.jpg\" border=\"0\"><\/p>\n<p>If you look at the graphical showplan you get an output plan that looks a bit funny.&nbsp; A Sequence Project is a special form of projection (the thing that evaluates the scalars in the SELECT list for each row).&nbsp; By default, a regular project in SQL Server does not preserve nor guarantee any sort properties in the physical plan.&nbsp; As a result, ranking functions put in regular projects would not preserve order in all cases deterministically.&nbsp; People seem to complain when the query results are incorrect.<\/p>\n<p>Enter the sequence project&#8230;.This little beauty does do scalar computations, but they are generally limited to a very small number of operations that require order for one reason or another.&nbsp; In the case of ranking functions, an order is required as part of the syntax, and you will see that this particular plan needs a sort since there are no indexes on the base table to satisfy a sort request on col2.&nbsp; (I&#8217;ll talk about segments in a future post, but for now ignore them and assume that they are part of the sequence project when looking at this plan).<\/p>\n<p>The problem with sequence projects are that while they preserve ordering semantics, they interfere with a lot of the other optimization logic in the QP.&nbsp; For example, these can interfere with index matching for predicates above the sequence project in a query tree.&nbsp; Operators that block other optimizations tend to make optimizer developers very unhappy and sometimes elicit violent reactions!&nbsp; So, be careful when proposing &#8220;opaque&#8221; operators to the next query optimization developer you see ;).<\/p>\n<p>So, SQL Server has a small number of operations that in previous releases of the product were &#8220;incorrectly&#8221; put into regular projects that are now in sequence projects in the current versions of the product.&nbsp; (By &#8220;incorrectly&#8221;, I mean that there were potential optimizations that could be run that would break their semantics, not that there were actively executing plans at a customer site that were incorrect.&nbsp; In optimizer terminology, they model was not correctly preserving the semantics even though there were no rules that exposed this fault).<\/p>\n<p>So, the challenge for you is think of some places _beyond sequence functions_ (RANK, DENSE_RANK, NTILE, ROW_NUMBER, &#8230;) that use the sequence projection operator.&nbsp; If you mail them to me with plans, I may be able to tell you more about what is happening for each of them.<\/p>\n<p>Happy hunting!<\/p>\n<p>Conor Cunningham<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I&#8217;ll be posting explanations for each query operator in the output query trees that you can see through &#8220;set showplan_text on&#8221; in SQL Server.&nbsp; I&#8217;m hopeful that this will give you the tools to better read the query plans being generated by the QP when evaluating your system. So I&#8217;ll start with one that [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-428","post","type-post","status-publish","format-standard","hentry","category-sql-server-2008"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Operator of the Day: Sequence Project - Conor Cunningham<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Operator of the Day: Sequence Project - Conor Cunningham\" \/>\n<meta property=\"og:description\" content=\"So I&#8217;ll be posting explanations for each query operator in the output query trees that you can see through &#8220;set showplan_text on&#8221; in SQL Server.&nbsp; I&#8217;m hopeful that this will give you the tools to better read the query plans being generated by the QP when evaluating your system. So I&#8217;ll start with one that [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/\" \/>\n<meta property=\"og:site_name\" content=\"Conor Cunningham\" \/>\n<meta property=\"article:published_time\" content=\"2008-01-29T20:06:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-01T19:22:42+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.sqlskills.com\/blogs\/conor\/content\/binary\/rank_plan.jpg\" \/>\n<meta name=\"author\" content=\"Conor Cunningham\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Conor Cunningham\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/\",\"name\":\"Operator of the Day: Sequence Project - Conor Cunningham\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#website\"},\"datePublished\":\"2008-01-29T20:06:04+00:00\",\"dateModified\":\"2013-01-01T19:22:42+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/f9106e03423de6b5157295891b8c3ae3\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Operator of the Day: Sequence Project\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/\",\"name\":\"Conor Cunningham\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/f9106e03423de6b5157295891b8c3ae3\",\"name\":\"Conor Cunningham\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d9c37eff231ec89c1b244347d966860875eea8b55b366911d2694e8cd9913e57?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d9c37eff231ec89c1b244347d966860875eea8b55b366911d2694e8cd9913e57?s=96&d=mm&r=g\",\"caption\":\"Conor Cunningham\"},\"url\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/author\/conor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Operator of the Day: Sequence Project - Conor Cunningham","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/","og_locale":"en_US","og_type":"article","og_title":"Operator of the Day: Sequence Project - Conor Cunningham","og_description":"So I&#8217;ll be posting explanations for each query operator in the output query trees that you can see through &#8220;set showplan_text on&#8221; in SQL Server.&nbsp; I&#8217;m hopeful that this will give you the tools to better read the query plans being generated by the QP when evaluating your system. So I&#8217;ll start with one that [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/","og_site_name":"Conor Cunningham","article_published_time":"2008-01-29T20:06:04+00:00","article_modified_time":"2013-01-01T19:22:42+00:00","og_image":[{"url":"http:\/\/www.sqlskills.com\/blogs\/conor\/content\/binary\/rank_plan.jpg"}],"author":"Conor Cunningham","twitter_misc":{"Written by":"Conor Cunningham","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/","url":"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/","name":"Operator of the Day: Sequence Project - Conor Cunningham","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#website"},"datePublished":"2008-01-29T20:06:04+00:00","dateModified":"2013-01-01T19:22:42+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/f9106e03423de6b5157295891b8c3ae3"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/operator-of-the-day-sequence-project\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/conor\/"},{"@type":"ListItem","position":2,"name":"Operator of the Day: Sequence Project"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/conor\/","name":"Conor Cunningham","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/conor\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/f9106e03423de6b5157295891b8c3ae3","name":"Conor Cunningham","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d9c37eff231ec89c1b244347d966860875eea8b55b366911d2694e8cd9913e57?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d9c37eff231ec89c1b244347d966860875eea8b55b366911d2694e8cd9913e57?s=96&d=mm&r=g","caption":"Conor Cunningham"},"url":"https:\/\/www.sqlskills.com\/blogs\/conor\/author\/conor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/posts\/428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/comments?post=428"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/posts\/428\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/media?parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/categories?post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/tags?post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}