{"id":294,"date":"2009-09-22T12:00:48","date_gmt":"2009-09-22T10:00:48","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/?p=294"},"modified":"2009-09-22T12:00:48","modified_gmt":"2009-09-22T10:00:48","slug":"estimated-rows-actual-rows-and-execution-count","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2009\/09\/22\/estimated-rows-actual-rows-and-execution-count\/","title":{"rendered":"Estimated rows, actual rows and execution count"},"content":{"rendered":"<p>It&#8217;s often said that a major discrepancy between estimated and actual row counts in a query&#8217;s execution plan is a sign of inaccurate statistics or a poor cardinality estimate and that it&#8217;s a sign of a problem. This is generally true, however there are places where the estimates and actual rows will differ, often quite dramatically, without it been a problem. The reason for this is that these two values show slightly different things.<\/p>\n<p>Let&#8217;s take a look at an example. (table creation code at the end of the post)<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">select bt.id, bt.SomeColumn, st.SomeArbDate\nfrom dbo.BigTable bt\ninner join dbo.SmallerTable st on bt.SomeColumn = st.LookupColumn\nwhere bt.id between 5000 and 5100<\/pre>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2009\/09\/EstimatedActual.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-338\" style=\"border: 1px solid black;\" title=\"Estimated Actual discrepency\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2009\/09\/EstimatedActual-300x213.png\" alt=\"Estimated Actual discrepency\" width=\"300\" height=\"213\" \/><\/a><\/p>\n<p>Estimated rows = 1, actual rows = 101. That&#8217;s a large discrepancy, but what caused it? It&#8217;s not out of date statistics (a usual cause) because the table has only just been created, so why is the estimation so far from the actual.<\/p>\n<p><!--more-->Let&#8217;s take a closer look at that seek. The seek predicate is an equality match on LookupColumn. That can only return 1 row because when the table was populated it was populated with unique values for that column (though the index on that column is not defined unique). So the estimated row count is dead-on, the index seek will return\u00a0 a single row. Now the question is where that 101 for the actual comes from.<\/p>\n<p>This seek is on the inner table of a nested loop join. The way the nested loop join works is to query the outer table of the join and then to query the inner table once for each row returned by the outer table. A look at the details of the clustered index scan that defines the outer table of the nested loop shows that it returns 101 rows.<\/p>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2009\/09\/OuterTable.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-340\" style=\"border: 1px solid black;\" title=\"OuterTable\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2009\/09\/OuterTable-300x205.png\" alt=\"OuterTable\" width=\"300\" height=\"205\" \/><br \/>\n<\/a><\/p>\n<p>Since the outer table returns 101 rows, the seek on the inner table must be done 101 times. That&#8217;s supported by the execution count shown on the inner seek. That&#8217;s where the discrepancy between actual and estimated rows comes from.<\/p>\n<p>When an operator is executed multiple times as part of the query execution, the estimated row count refers to the number of rows that the optimiser estimates will be affected per execution. The actual row count refers to the total number of rows that the operator affected, cumulative over all executions. So when checking to see if there&#8217;s a major discrepancy between estimated and actual rows counts, the actual row count has to be divided by the number of executions.<\/p>\n<p>That&#8217;s fine when using SQL 2008&#8217;s management studio, which exposes the execution count of an operator in the tooltip of that operator. SQL 2005&#8217;s management studio did not display the execution count anywhere convenient, though it is present in the XML of the plan. This is purely a feature of the version of Management Studio. The SQL 2008 management studio will display the execution count regardless of whether it&#8217;s connected to SQL 2005 or to SQL 2008.<\/p>\n<p>For those still using SQL 2005&#8217;s tools, if you want the execution count, this is where to look:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">&lt;RelOp AvgRowSize=&quot;15&quot; EstimateCPU=&quot;0.0001581&quot; EstimateIO=&quot;0.003125&quot; EstimateRebinds=&quot;85.432&quot; EstimateRewinds=&quot;0&quot; EstimateRows=&quot;1&quot; LogicalOp=&quot;Index Seek&quot; NodeId=&quot;2&quot; Parallel=&quot;false&quot; PhysicalOp=&quot;Index Seek&quot; EstimatedTotalSubtreeCost=&quot;0.0480212&quot; TableCardinality=&quot;3956&quot;&gt;\n&lt;OutputList&gt;\n&lt;ColumnReference Database=&quot;&#x5B;Testing]&quot; Schema=&quot;&#x5B;dbo]&quot; Table=&quot;&#x5B;SmallerTable]&quot; Alias=&quot;&#x5B;st]&quot; Column=&quot;SomeArbDate&quot; \/&gt;\n&lt;\/OutputList&gt;\n&lt;RunTimeInformation&gt;\n&lt;RunTimeCountersPerThread Thread=&quot;0&quot; ActualRows=&quot;101&quot; ActualEndOfScans=&quot;101&quot; ActualExecutions=&quot;101&quot; \/&gt;\n&lt;\/RunTimeInformation&gt;<\/pre>\n<p>The number of executions, along with the actual row count is contained within the XML node RunTimeInformation. Obviously this node will not be present when looking at an estimated execution plan or an execution plan retrieved from the plan cache, as neither contains any run-time information.<\/p>\n<p>Table creation code.<\/p>\n<p>Edit: In the original post I left 2 indexes out of the table creation, which changed the behaviour of the query completely (hash joins instead of nested loop). If you tried to reproduce my results and couldn&#8217;t, you should be able to now with the correct indexes.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">Create Table BigTable (\nid int identity primary key,\nSomeColumn char(4),\nFiller char(100)\n)\n\nCreate Table SmallerTable (\nid int identity primary key,\nLookupColumn char(4),\nSomeArbDate Datetime default getdate()\n)\n\nINSERT INTO BigTable (SomeColumn)\nSELECT top 250000\nchar(65+FLOOR(RAND(a.column_id *5645 + b.object_id)*10)) + char(65+FLOOR(RAND(b.column_id *3784 + b.object_id)*12)) +\nchar(65+FLOOR(RAND(b.column_id *6841 + a.object_id)*12)) + char(65+FLOOR(RAND(a.column_id *7544 + b.object_id)*8))\nfrom master.sys.columns a cross join master.sys.columns b\n\nINSERT INTO SmallerTable (LookupColumn)\nSELECT DISTINCT SomeColumn\nFROM BigTable TABLESAMPLE (25 PERCENT)\nGO\n\nCREATE NONCLUSTERED INDEX &#x5B;idx_BigTable_SomeColumn]\n  ON &#x5B;dbo].&#x5B;BigTable] (&#x5B;SomeColumn] ASC)\nGO\n\nCREATE NONCLUSTERED INDEX &#x5B;idx_SmallerTable_LookupColumn]\n  ON &#x5B;dbo].&#x5B;SmallerTable] (&#x5B;LookupColumn] ASC)\n  INCLUDE ( &#x5B;SomeArbDate])\nGO\n<\/pre>\n<div id=\"_mcePaste\" style=\"overflow: hidden; position: absolute; left: -10000px; top: 535px; width: 1px; height: 1px;\">From\u00a0\u00a0 \u00a0Subject\u00a0\u00a0 \u00a0Received\u00a0\u00a0 \u00a0Size<br \/>\nGideon van Zyl &#8211; XE\u00a0\u00a0 \u00a0C-Track info\/docs 4 SAPS\u00a0\u00a0 \u00a015:53\u00a0\u00a0 \u00a024 KB<\/div>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s often said that a major discrepancy between estimated and actual row counts in a query&#8217;s execution plan is a sign of inaccurate statistics or a poor cardinality estimate and that it&#8217;s a sign of a problem. This is generally&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2009\/09\/22\/estimated-rows-actual-rows-and-execution-count\/\">(Read more)<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[23,15,16],"tags":[],"class_list":["post-294","post","type-post","status-publish","format-standard","hentry","category-execution-plans","category-sql-server","category-syndication"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-4K","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=294"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/294\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}