{"id":414,"date":"2008-02-21T21:03:40","date_gmt":"2008-02-21T21:03:40","guid":{"rendered":"\/blogs\/conor\/post\/SQL-Server-Scalar-Operator-Costing-aka-umm-what-costing-.aspx"},"modified":"2013-01-01T19:23:54","modified_gmt":"2013-01-01T19:23:54","slug":"sql-server-scalar-operator-costing-aka-umm-what-costing","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/","title":{"rendered":"SQL Server Scalar Operator Costing aka &#8220;umm, what costing?&#8221;"},"content":{"rendered":"<p>One of the earlier comments I received asked about costing of T-SQL and CLR functions.&nbsp; More specifically, they want to know how they are costed and why it is so low.<\/p>\n<p>Let&#8217;s create a motivating example.&nbsp; I&#8217;ll create a table with some size to it (I add a binary(2000) column to make each page take at least 2000 bytes, and this means that about 4 rows fit on a page.&nbsp; So, if I add 100,000 rows to a table, we&#8217;re talking about enough IOs to show up in the costing functions).<\/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;\">use<\/span> t1\r\n\r\n<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> t3(col1 <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">int<\/span>, col2 <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">binary<\/span>(2000))\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> t3(col1, col2) <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">values<\/span> (2, 0x1234)\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> t3(col1, col2) <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">values<\/span> (3, 0x1234)\r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">declare<\/span> @p <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> @p=0\r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">while<\/span> @p &lt; 100000\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> t3(col1, col2) <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">values<\/span> (<span style=\"color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;\">rand<\/span>()*1000, 0x1234)\r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">set<\/span> @p=@p+1\r\n<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">end<\/span>\r\n\r\n\r\n<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;\">function<\/span> f3(@p1 <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">int<\/span>) RETURNS <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;\">BEGIN<\/span>\r\n  <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">return<\/span> (<span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">select<\/span> <span style=\"color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;\">count<\/span>(col1) <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">from<\/span> t3 <span style=\"color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;\">where<\/span> col1=@p1)\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> dbo.f3(2)<\/span><\/pre>\n<p>(I apologize for my lack of real-world examples.. After you&#8217;ve done this a few thousand times you resort to the shortest names you can type ;).<\/p>\n<p>ok, so we have a table and we create a T-SQL scalar function that just runs an aggregate to find the number of occurances of a particular value in the table.&nbsp;&nbsp; Since there are no indees on t3 this will be a table scan and it will read lots of pages into the buffer pool.<\/p>\n<p>So let&#8217;s look at the costing output for each of the elements of this query.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.sqlskills.com\/blogs\/conor\/content\/binary\/1.jpg\" border=\"0\"><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.sqlskills.com\/blogs\/conor\/content\/binary\/2.jpg\" border=\"0\"><\/p>\n<p>Above is the plan for the base query (select dbo.f3(2)).&nbsp; It emits a dummy row from a constant table and then does a &#8220;compute scalar&#8221; operation to run the function and generate a 1 row, 1 column result.<\/p>\n<p>That cost doesn&#8217;t look so big&#8230;<\/p>\n<p>Now let&#8217;s look at the plan for the scalar subquery (well, the top of it since we&#8217;re looking for the total cost)<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.sqlskills.com\/blogs\/conor\/content\/binary\/3.jpg\" border=\"0\"><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.sqlskills.com\/blogs\/conor\/content\/binary\/4.jpg\" border=\"0\"><\/p>\n<p>Well, the laws of physics appear to be violated.&nbsp; <\/p>\n<p>You can try whatever cost query you want in the function &#8211; unfortunately, these aren&#8217;t really costed in the SQL Server QP (at least through 2005 and what I have seen so far on the 2008 CTPs).&nbsp; Back when SQL Server 7 was being created, scalars were generally always cheap and they really didn&#8217;t need costs.&nbsp; There&#8217;s a small amount of CPU cost added in a compute scalar and that generally worked for OLTP plans.&nbsp; As SQL Server started being used in larger and larger scenarios, this issue started to manifest.<\/p>\n<p>As SQL Server has added more features, some of them can be arbitrarily expensive and this issue can show up in plan selection as a problem.&nbsp; Areas where you are likely to see this are T-SQL functions, CLR functions, string functions on varchar(max) and friends, and I&#8217;m sure that there are others.&nbsp; However, it&#8217;s worth noting that this won&#8217;t be a problem in a lot of cases, and often workarounds are possible that may even be better design choices than doing what I&#8217;ve done in this example.&nbsp; I don&#8217;t recommend that you &#8220;hide&#8221; subqueries in scalar functions like this precisely because the optimizer can&#8217;t see and cost them properly, even if they are as simple as this one.<\/p>\n<p>There are a few tricky problems in &#8220;fixing&#8221; this (costing the scalar functions in line with their runtime).&nbsp; First, this isn&#8217;t exactly the sort of thing that people are going to beat down the gates at Microsoft to get fixed.&nbsp; However, it is an area where the model doesn&#8217;t work right, and when a customer does hit this they are often very far into a deployment or POC and can&#8217;t exactly redesign their application easily.&nbsp; Second, there are actually lots of really complex things that happen with the placement of computescalars and how they are evaluated in the optimizer and execution engine.&nbsp; Finally, the SQL Server QP is set up to do relational transformations (A join B is equivalent to B join A, etc.), and the computation fo scalars isn&#8217;t really a true relational operation in this algebra.&nbsp; This makes true costing of compute scalars always something that is different than costing a join or a filter.&nbsp; Third, changing the costing of such a basic operator will probably impact the plan selection for basically EVERY query, as their costs will change slightly &#8211; that&#8217;s a big risk to fix a &#8220;little&#8221; bug. The last tricky bit in fixing this has to do with the way that T-SQL functions like this get exposed into the optimizer.&nbsp; Not all queries really have known costs <i>a priori<\/i>.&nbsp; The example function that I&#8217;ve given you does have a fixed cost, under some definition, but if I start putting procedural logic into the function, then the actual executed path depends on the runtime data, and that means that the cost is based on something you haven&#8217;t interpreted yet.&nbsp; So, there are some at least reasonable explanations as to why this issue persists to this day.<\/p>\n<p>So my advice is that if you have cases where your scalar functions are undercosted and you think it impacts plan selection, then you need to contact Microsoft and let them know.&nbsp; I have seen some cases of this, but I think that it&#8217;s perhaps not the most common problem and that might have some impact on whether it gets addressed in the future.&nbsp; I *guarantee* you that the people who work there are highly skilled and passionate about solving customer issues, as I worked with them, so I suspect that if there&#8217;s enough squeaky wheels then this is something that may be addressed.<\/p>\n<p>Your other option is to send me money, I guess ;). <\/p>\n<p>I hope that gives you a bit of background on the issues you need to know when examining your query plans when you have expensive scalars in them.<\/p>\n<p>Conor Cunningham<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the earlier comments I received asked about costing of T-SQL and CLR functions.&nbsp; More specifically, they want to know how they are costed and why it is so low. Let&#8217;s create a motivating example.&nbsp; I&#8217;ll create a table with some size to it (I add a binary(2000) column to make each page take [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-414","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SQL Server Scalar Operator Costing aka &quot;umm, what costing?&quot;  - 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\/sql-server-scalar-operator-costing-aka-umm-what-costing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server Scalar Operator Costing aka &quot;umm, what costing?&quot;  - Conor Cunningham\" \/>\n<meta property=\"og:description\" content=\"One of the earlier comments I received asked about costing of T-SQL and CLR functions.&nbsp; More specifically, they want to know how they are costed and why it is so low. Let&#8217;s create a motivating example.&nbsp; I&#8217;ll create a table with some size to it (I add a binary(2000) column to make each page take [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/\" \/>\n<meta property=\"og:site_name\" content=\"Conor Cunningham\" \/>\n<meta property=\"article:published_time\" content=\"2008-02-21T21:03:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-01T19:23:54+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.sqlskills.com\/blogs\/conor\/content\/binary\/1.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=\"5 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\/sql-server-scalar-operator-costing-aka-umm-what-costing\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/\",\"name\":\"SQL Server Scalar Operator Costing aka \\\"umm, what costing?\\\" - Conor Cunningham\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#website\"},\"datePublished\":\"2008-02-21T21:03:40+00:00\",\"dateModified\":\"2013-01-01T19:23:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/f9106e03423de6b5157295891b8c3ae3\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Scalar Operator Costing aka &#8220;umm, what costing?&#8221;\"}]},{\"@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":"SQL Server Scalar Operator Costing aka \"umm, what costing?\"  - 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\/sql-server-scalar-operator-costing-aka-umm-what-costing\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server Scalar Operator Costing aka \"umm, what costing?\"  - Conor Cunningham","og_description":"One of the earlier comments I received asked about costing of T-SQL and CLR functions.&nbsp; More specifically, they want to know how they are costed and why it is so low. Let&#8217;s create a motivating example.&nbsp; I&#8217;ll create a table with some size to it (I add a binary(2000) column to make each page take [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/","og_site_name":"Conor Cunningham","article_published_time":"2008-02-21T21:03:40+00:00","article_modified_time":"2013-01-01T19:23:54+00:00","og_image":[{"url":"http:\/\/www.sqlskills.com\/blogs\/conor\/content\/binary\/1.jpg"}],"author":"Conor Cunningham","twitter_misc":{"Written by":"Conor Cunningham","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/","url":"https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/","name":"SQL Server Scalar Operator Costing aka \"umm, what costing?\" - Conor Cunningham","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#website"},"datePublished":"2008-02-21T21:03:40+00:00","dateModified":"2013-01-01T19:23:54+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/f9106e03423de6b5157295891b8c3ae3"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/sql-server-scalar-operator-costing-aka-umm-what-costing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/conor\/"},{"@type":"ListItem","position":2,"name":"SQL Server Scalar Operator Costing aka &#8220;umm, what costing?&#8221;"}]},{"@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\/414","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=414"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/posts\/414\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/media?parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/categories?post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/tags?post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}