{"id":694,"date":"2010-05-01T18:26:00","date_gmt":"2010-05-01T18:26:00","guid":{"rendered":"\/blogs\/paul\/post\/Adventures-in-query-tuning-unexpected-key-lookups.aspx"},"modified":"2017-07-20T08:08:33","modified_gmt":"2017-07-20T15:08:33","slug":"adventures-in-query-tuning-unexpected-key-lookups","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/","title":{"rendered":"Adventures in query tuning: unexpected key lookups"},"content":{"rendered":"<p style=\"text-align: justify;\">I&#8217;m starting a new blog category to talk about some of weird and confusing stuff I see while query tuning.<\/p>\n<p style=\"text-align: justify;\">First up is the case of the unexpected <em>Key Lookup (Clustered)<\/em> in a query that looks like it should be covered. This is a follow on from the post <a href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/missing-index-dmvs-bug-that-could-cost-your-sanity\/\">Missing index DMVs bug that could cost your sanity&#8230;<\/a><\/p>\n<p style=\"text-align: justify;\">Today I was tracking down some performance issues on a client site and came across something in a query plan that confused me for a bit. All code and screenshots below are from my simplified repro on my test machine at home.<\/p>\n<p style=\"text-align: justify;\">The code is using a cursor to drive a process, and the <em>SELECT<\/em> statement driving the cursor is covered by a non-clustered index.<\/p>\n<p style=\"text-align: justify;\">However, the query plan for the cursor-driving statement is as below:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" src=\"\/blogs\/paul\/wp-content\/uploads\/2010\/5\/missing3.jpg\" alt=\"\" width=\"1114\" height=\"222\" \/><\/p>\n<p style=\"text-align: justify;\">You can see the SELECT statement above, and you can see that the query plan is using the nonclustered index on <em>c2<\/em> and <em>c3<\/em> to satisfy the <em>SELECT<\/em>. So where is the <em>Key Lookup<\/em> coming from? If I hover over the <em>Key Lookup<\/em> operator, I see the details of the operator in a tool-tip:<\/p>\n<p><img decoding=\"async\" src=\"\/blogs\/paul\/wp-content\/uploads\/2010\/5\/missing4.jpg\" alt=\"\" width=\"452\" height=\"451\" \/><\/p>\n<p style=\"text-align: justify;\">According to the details, the output list of the <em>Key Lookup<\/em> is <em>Chk1002<\/em>. What&#8217;s <em>Chk1002<\/em>? It&#8217;s not a column in my table, and it&#8217;s not a check constraint on the table either &#8211; I don&#8217;t have any. And why is it looking up the cluster key *anyway*?<\/p>\n<p style=\"text-align: justify;\">This is where I turned to Kimberly to help figure out what&#8217;s going on, who said it&#8217;s to do with the cursor.<\/p>\n<p style=\"text-align: justify;\">I then proceeded to work things out and got it a little twisted up. Check out the comments discussion with Brad who explained things, and it&#8217;s in Books Online too.<\/p>\n<p style=\"text-align: justify;\">Because the cursor is a dynamic optimistic cursor (by default as I didn&#8217;t specify anything else) SQL Server persists a checksum of all rows picked up by the cursor-driving query in a worktable in tempdb (which, if this is a heavily executed query, can cause perf issues with tempdb). When I say <em>FETCH NEXT<\/em>, it goes back to the table to get the next value, but if I want to update a column in the row I&#8217;m working on, it recalculates the checksum to make sure that nothing has changed in that row outside the confines of my cursor. If so, my update fails &#8211; if not, it allows it.<\/p>\n<p style=\"text-align: justify;\">There are plenty of ways to improve the situation (e.g. removing the cursor altogether and using a nice set-based operation&#8230; or changing the cursor type)\u00a0but that&#8217;s\u00a0not the point of this post. I just wanted you to be aware of this, and how the query operator properties isn&#8217;t explicit in working out what&#8217;s going on.<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m starting a new blog category to talk about some of weird and confusing stuff I see while query tuning. First up is the case of the unexpected Key Lookup (Clustered) in a query that looks like it should be covered. This is a follow on from the post Missing index DMVs bug that could [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66,73,93],"tags":[],"class_list":["post-694","post","type-post","status-publish","format-standard","hentry","category-performance-tuning","category-query-tuning","category-tempdb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Adventures in query tuning: unexpected key lookups - Paul S. Randal<\/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\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Adventures in query tuning: unexpected key lookups - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"I&#8217;m starting a new blog category to talk about some of weird and confusing stuff I see while query tuning. First up is the case of the unexpected Key Lookup (Clustered) in a query that looks like it should be covered. This is a follow on from the post Missing index DMVs bug that could [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2010-05-01T18:26:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-07-20T15:08:33+00:00\" \/>\n<meta name=\"author\" content=\"Paul Randal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paul Randal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/\",\"name\":\"Adventures in query tuning: unexpected key lookups - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2010-05-01T18:26:00+00:00\",\"dateModified\":\"2017-07-20T15:08:33+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Adventures in query tuning: unexpected key lookups\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\",\"name\":\"Paul S. Randal\",\"description\":\"In Recovery...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\",\"name\":\"Paul Randal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"caption\":\"Paul Randal\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/paul\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Adventures in query tuning: unexpected key lookups - Paul S. Randal","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\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/","og_locale":"en_US","og_type":"article","og_title":"Adventures in query tuning: unexpected key lookups - Paul S. Randal","og_description":"I&#8217;m starting a new blog category to talk about some of weird and confusing stuff I see while query tuning. First up is the case of the unexpected Key Lookup (Clustered) in a query that looks like it should be covered. This is a follow on from the post Missing index DMVs bug that could [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/","og_site_name":"Paul S. Randal","article_published_time":"2010-05-01T18:26:00+00:00","article_modified_time":"2017-07-20T15:08:33+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/","name":"Adventures in query tuning: unexpected key lookups - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2010-05-01T18:26:00+00:00","dateModified":"2017-07-20T15:08:33+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-unexpected-key-lookups\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"Adventures in query tuning: unexpected key lookups"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/","name":"Paul S. Randal","description":"In Recovery...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce","name":"Paul Randal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","caption":"Paul Randal"},"sameAs":["http:\/\/3.209.169.194\/blogs\/paul"],"url":"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/comments?post=694"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/694\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}