{"id":4502,"date":"2015-05-07T09:08:19","date_gmt":"2015-05-07T16:08:19","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/paul\/?p=4502"},"modified":"2017-04-13T11:42:01","modified_gmt":"2017-04-13T18:42:01","slug":"identifying-queries-with-sos_scheduler_yield-waits","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/identifying-queries-with-sos_scheduler_yield-waits\/","title":{"rendered":"Identifying queries with SOS_SCHEDULER_YIELD waits"},"content":{"rendered":"<p>(Check out my Pluralsight online training course: <em><a href=\"http:\/\/www.pluralsight.com\/training\/Courses\/TableOfContents\/sqlserver-waits\" target=\"_blank\">SQL Server: Performance Troubleshooting Using Wait Statistics<\/a>\u00a0<\/em>and my\u00a0<a href=\"https:\/\/www.sqlskills.com\/help\/waits\/\" target=\"_blank\">comprehensive library of all wait types and latch classes<\/a>.)<\/p>\n<p>One of the problems with the <a href=\"http:\/\/www.sqlskills.com\/help\/waits\/sos_scheduler_yield\/\" target=\"_blank\">SOS_SCHEDULER_YIELD<\/a> wait type is that it&#8217;s not really a wait type. When this wait type occurs, it&#8217;s because a thread exhausted its 4ms scheduling quantum and voluntarily yielded the CPU, going directly to the bottom of the Runnable Queue for the scheduler, bypassing the Waiter List. A wait has to be registered though when a thread goes off the processor, so SOS_SCHEDULER_YIELD is used.<\/p>\n<p>You can read more about this wait type:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/sos_scheduler_yield-waits-and-the-lock_hash-spinlock\/\" target=\"_blank\">Here<\/a>\u00a0on my blog<\/li>\n<li><a href=\"http:\/\/sqlperformance.com\/2014\/02\/sql-performance\/knee-jerk-waits-sos-scheduler-yield\" target=\"_blank\">Here<\/a>\u00a0in my post on the SQLPerformance.com blog<\/li>\n<\/ul>\n<p>You want to investigate these waits if they&#8217;re a prevalent wait on your server, as they could be an indicator of large scans happening (of data that&#8217;s already in memory) where you&#8217;d really rather have small index seeks.<\/p>\n<p>The problem is that they&#8217;re not a real wait type, so you can&#8217;t use\u00a0<a href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/advanced-performance-troubleshooting-waits-latches-spinlocks\/\" target=\"_blank\">my script<\/a>\u00a0to look at\u00a0<em>sys.dm_os_waiting_tasks<\/em>\u00a0and get the query plans of threads incurring that wait type, because these threads aren&#8217;t waiting for a resource, so don&#8217;t show up in the output of\u00a0<em>sys.dm_os_waiting_tasks<\/em>!<\/p>\n<p>The solution is to use the\u00a0<em>sys.dm_exec_requests DMV<\/em>, as that will show the\u00a0<em>last_wait_type<\/em>\u00a0for all running requests. Below is a script you can use.<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nSELECT\r\n\t&#x5B;er].&#x5B;session_id],\r\n\t&#x5B;es].&#x5B;program_name],\r\n\t&#x5B;est].text,\r\n\t&#x5B;er].&#x5B;database_id],\r\n\t&#x5B;eqp].&#x5B;query_plan],\r\n\t&#x5B;er].&#x5B;cpu_time]\r\nFROM sys.dm_exec_requests &#x5B;er]\r\nINNER JOIN sys.dm_exec_sessions &#x5B;es] ON\r\n\t&#x5B;es].&#x5B;session_id] = &#x5B;er].&#x5B;session_id]\r\nOUTER APPLY sys.dm_exec_sql_text (&#x5B;er].&#x5B;sql_handle]) &#x5B;est]\r\nOUTER APPLY sys.dm_exec_query_plan (&#x5B;er].&#x5B;plan_handle]) &#x5B;eqp]\r\nWHERE\r\n    &#x5B;es].&#x5B;is_user_process] = 1\r\n\tAND &#x5B;er].&#x5B;last_Wait_type] = N'SOS_SCHEDULER_YIELD'\r\nORDER BY\r\n    &#x5B;er].&#x5B;session_id];\r\nGO\r\n<\/pre>\n<p>That will give you the code and query plan of what&#8217;s happening, but even with that it might not be obvious\u00a0which exact operator is causing that wait so you may need to resort to capturing SQL Server call stacks, as I explain in the first blog post link above.<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Check out my Pluralsight online training course: SQL Server: Performance Troubleshooting Using Wait Statistics\u00a0and my\u00a0comprehensive library of all wait types and latch classes.) One of the problems with the SOS_SCHEDULER_YIELD wait type is that it&#8217;s not really a wait type. When this wait type occurs, it&#8217;s because a thread exhausted its 4ms scheduling quantum and [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,66,101],"tags":[],"class_list":["post-4502","post","type-post","status-publish","format-standard","hentry","category-example-scripts","category-performance-tuning","category-wait-stats"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Identifying queries with SOS_SCHEDULER_YIELD waits - 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\/identifying-queries-with-sos_scheduler_yield-waits\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Identifying queries with SOS_SCHEDULER_YIELD waits - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"(Check out my Pluralsight online training course: SQL Server: Performance Troubleshooting Using Wait Statistics\u00a0and my\u00a0comprehensive library of all wait types and latch classes.) One of the problems with the SOS_SCHEDULER_YIELD wait type is that it&#8217;s not really a wait type. When this wait type occurs, it&#8217;s because a thread exhausted its 4ms scheduling quantum and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/identifying-queries-with-sos_scheduler_yield-waits\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2015-05-07T16:08:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-04-13T18:42:01+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\/identifying-queries-with-sos_scheduler_yield-waits\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/identifying-queries-with-sos_scheduler_yield-waits\/\",\"name\":\"Identifying queries with SOS_SCHEDULER_YIELD waits - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2015-05-07T16:08:19+00:00\",\"dateModified\":\"2017-04-13T18:42:01+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/identifying-queries-with-sos_scheduler_yield-waits\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/identifying-queries-with-sos_scheduler_yield-waits\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/identifying-queries-with-sos_scheduler_yield-waits\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Identifying queries with SOS_SCHEDULER_YIELD waits\"}]},{\"@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":"Identifying queries with SOS_SCHEDULER_YIELD waits - 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\/identifying-queries-with-sos_scheduler_yield-waits\/","og_locale":"en_US","og_type":"article","og_title":"Identifying queries with SOS_SCHEDULER_YIELD waits - Paul S. Randal","og_description":"(Check out my Pluralsight online training course: SQL Server: Performance Troubleshooting Using Wait Statistics\u00a0and my\u00a0comprehensive library of all wait types and latch classes.) One of the problems with the SOS_SCHEDULER_YIELD wait type is that it&#8217;s not really a wait type. When this wait type occurs, it&#8217;s because a thread exhausted its 4ms scheduling quantum and [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/identifying-queries-with-sos_scheduler_yield-waits\/","og_site_name":"Paul S. Randal","article_published_time":"2015-05-07T16:08:19+00:00","article_modified_time":"2017-04-13T18:42:01+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\/identifying-queries-with-sos_scheduler_yield-waits\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/identifying-queries-with-sos_scheduler_yield-waits\/","name":"Identifying queries with SOS_SCHEDULER_YIELD waits - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2015-05-07T16:08:19+00:00","dateModified":"2017-04-13T18:42:01+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/identifying-queries-with-sos_scheduler_yield-waits\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/identifying-queries-with-sos_scheduler_yield-waits\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/identifying-queries-with-sos_scheduler_yield-waits\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"Identifying queries with SOS_SCHEDULER_YIELD waits"}]},{"@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\/4502","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=4502"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/4502\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=4502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=4502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=4502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}