{"id":86,"date":"2008-08-28T20:33:34","date_gmt":"2008-08-28T18:33:34","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/?p=86"},"modified":"2008-08-28T20:33:34","modified_gmt":"2008-08-28T18:33:34","slug":"an-example-exec-plan","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/08\/28\/an-example-exec-plan\/","title":{"rendered":"An example exec plan"},"content":{"rendered":"<p>Back to the technical posts&#8230;<\/p>\n<p>I&#8217;m going to take an example of a very simple execution plan that I used in a presentation a few weeks back and go over it in some detail, pointing out information that is available and what can be read from the plan.<\/p>\n<p>The execution plan can be downloaded (in xml format) here &#8211; <a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2008\/08\/execplandemo.zip\">ExecPlanDem01.zip<\/a><\/p>\n<p>The query that the plan came from is a very simple two table query. It&#8217;s not a very optimal plan, but that&#8217;s because I forced an index hint in order to generate a more interesting plan. Without the hint, it&#8217;s a simple exec plan with two index seeks and a nested loop join.<\/p>\n<p><strong>Overview<\/strong><\/p>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2008\/08\/execplanwalkthrough1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-117\" title=\"execplanwalkthrough_thumb1\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2008\/08\/execplanwalkthrough_thumb1.png\" alt=\"\" width=\"320\" height=\"78\" \/><\/a><\/p>\n<p>The first thing that can be seen from the execution plan is that most of the cost of the query is in two operations, a key lookup (formerly called a bookmark lookup) and a clustered index seek. The high cost of the key lookup is a good sign that the query is using an inappropriate index. (in a future post I&#8217;ll discuss using the exec plan to choose indexes)<\/p>\n<p><!--more-->The second thing that can be seen is that there is a filter operator. This again suggests that there is not an appropriate index, as this plan means that SQL will first select rows from the table and later filter out rows that aren&#8217;t needed.<\/p>\n<p><strong>Index seek<\/strong><\/p>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2008\/08\/execplanwalkthrough2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft alignnone size-full wp-image-118\" style=\"float: left; margin-left: 10px; margin-right: 10px;\" title=\"execplanwalkthrough_thumb2\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2008\/08\/execplanwalkthrough_thumb2.png\" alt=\"\" width=\"109\" height=\"201\" \/><\/a><\/p>\n<p>The first thing to notice about the index seek is that there are two seek predicates, one an equality, one an inequality. Having them both as seek predicates indicated that the columns in this index are in the correct order to support the two predicates. (I&#8217;ll do an entire post sometime on index key column ordering)<\/p>\n<p>The second thing to note is that the estimated and actual rows are identical. This is good, it indicates that the statistics on this index are up to date and that the optimiser managed to get an accurate estimate of the rows that would be returned.<\/p>\n<p><strong>Key Lookup<\/strong><\/p>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2008\/08\/execplanwalkthrough3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft alignnone size-full wp-image-119\" style=\"float: left; margin-left: 10px; margin-right: 10px;\" title=\"execplanwalkthrough_thumb3\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2008\/08\/execplanwalkthrough_thumb3.png\" alt=\"\" width=\"169\" height=\"198\" \/><\/a>The presence of a key lookup operator indicates that the nonclustered index that was used to locate the rows affected by the query did not have all the columns required by the query. The missing columns must be looked up from the clustered index. The output list shows what columns were fetched by the key lookup. This can help when altering indexes to get a covering index.<\/p>\n<p>One important thing to note is that the estimated rows for this operator is 1. That&#8217;s not a cardinality problem, it&#8217;s because the estimated rows is per execution of the operator and the actual rows is total over all executions. The number of times an operator is executed is stored within the XML, however the SQL 2005 management studio doesn&#8217;t show it. Management studio in SQL 2008 does.<\/p>\n<p>This shows one of the reasons why the key lookup can be a bottleneck. For each row returned by the index seek, the key lookup does a clustered index seek returning a single row. For a small number of rows, that&#8217;s not too much or a problem, for hundreds of rows it can be a major bottleneck in the query.<\/p>\n<p><strong>Filter<\/strong><\/p>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2008\/08\/execplanwalkthrough4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-120\" style=\"margin-left: 10px; margin-right: 10px; float: left;\" title=\"execplanwalkthrough_thumb4\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2008\/08\/execplanwalkthrough_thumb4.png\" alt=\"\" width=\"159\" height=\"240\" \/><\/a><\/p>\n<p>The last interesting part of the execution plan is the filter.<\/p>\n<p>Having a filter in the exec plan indicates that there was some condition that could not be evaluated as part of the index seek\/scan. This may be because in involves an aggregate, because it involves columns in multiple tables or, as in this case, because the column was not part of the index and had to be looked up separatly.<\/p>\n<p>As with the index seek, the estimated and actual row counts are very similar, indicating that the cardinality estimate is accurate for this query.<\/p>\n<p>So, that&#8217;s most of the interesting things that can be gleaned from the exec plan. I have a couple more posts planned in the series on reading exec plans, one showing a more complex plan and the other going into more detail on the % costs that the plan shows.<\/p>\n<p>That&#8217;s all for now. I&#8217;ll see if &#8216;i can dig up a more complex plan to work through.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back to the technical posts&#8230; I&#8217;m going to take an example of a very simple execution plan that I used in a presentation a few weeks back and go over it in some detail, pointing out information that is available&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/08\/28\/an-example-exec-plan\/\">(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],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-execution-plans","category-sql-server"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-1o","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/86","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=86"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}