{"id":488,"date":"2012-05-10T02:26:00","date_gmt":"2012-05-10T02:26:00","guid":{"rendered":"\/blogs\/paul\/post\/Survey-most-prevalent-latch-waits.aspx"},"modified":"2016-05-23T17:02:32","modified_gmt":"2016-05-24T00:02:32","slug":"survey-most-prevalent-latch-waits-code-to-run","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/","title":{"rendered":"Survey: most prevalent latch waits (code to run)"},"content":{"rendered":"<p><span style=\"font-family: verdana, geneva; font-size: small;\">I first started blogging about latches and some of the deeper parts of SQL Server internals last year (see <\/span><a href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/advanced-performance-troubleshooting-waits-latches-spinlocks\/\" target=\"_blank\"><span style=\"font-family: verdana, geneva; font-size: small;\">Advanced performance troubleshooting: waits, latches, spinlocks<\/span><\/a><span style=\"font-family: verdana, geneva; font-size: small;\">) and now I&#8217;d like to pick up that thread (no scheduling pun intended :-)) and blog some more about some of the common latches that could be a performance bottleneck.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva; font-size: small;\">To that end, I&#8217;ve got some code below (plus example output)\u00a0that will show the most common latch waits that have occurred on your system.<\/span><\/p>\n<blockquote><p><span style=\"font-family: 'courier new', courier; font-size: small;\">WITH Latches AS<br \/>\n(SELECT<br \/>\nlatch_class,<br \/>\nwait_time_ms \/ 1000.0 AS WaitS,<br \/>\nwaiting_requests_count AS WaitCount,<br \/>\n100.0 * wait_time_ms \/ SUM (wait_time_ms) OVER() AS Percentage,<br \/>\nROW_NUMBER() OVER(ORDER BY wait_time_ms DESC) AS RowNum<br \/>\nFROM sys.dm_os_latch_stats<br \/>\nWHERE latch_class NOT IN (<br \/>\n&#8216;BUFFER&#8217;)<br \/>\nAND wait_time_ms &gt; 0<\/span><span style=\"font-family: 'courier new', courier; font-size: small;\"><br \/>\n)<br \/>\nSELECT<br \/>\nW1.latch_class AS LatchClass,<br \/>\nCAST (W1.WaitS AS DECIMAL(14, 2)) AS Wait_S,<br \/>\nW1.WaitCount AS WaitCount,<br \/>\nCAST (W1.Percentage AS DECIMAL(14, 2)) AS Percentage,<br \/>\nCAST ((W1.WaitS \/ W1.WaitCount) AS DECIMAL (14, 4)) AS AvgWait_S<br \/>\nFROM Latches AS W1<br \/>\nINNER JOIN Latches AS W2<br \/>\nON W2.RowNum &lt;= W1.RowNum<br \/>\nWHERE W1.WaitCount &gt; 0<br \/>\nGROUP BY W1.RowNum, W1.latch_class, W1.WaitS, W1.WaitCount, W1.Percentage<br \/>\nHAVING SUM (W2.Percentage) &#8211; W1.Percentage &lt; 95; &#8212; percentage threshold<br \/>\nGO<\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: small;\">LatchClass\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Wait_S\u00a0 WaitCount\u00a0 Percentage\u00a0 AvgWait_S<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; &#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;-<br \/>\nLOG_MANAGER\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 221.43\u00a0 4659\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 45.81\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.0475<br \/>\nACCESS_METHODS_HOBT_VIRTUAL_ROOT\u00a0 199.56\u00a0 7017\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 41.28\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.0284<br \/>\nFGCB_ADD_REMOVE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 35.17\u00a0\u00a0 1047\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 7.27\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.0336<br \/>\nDBCC_OBJECT_METADATA\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 26.85\u00a0\u00a0 256490\u00a0\u00a0\u00a0\u00a0 5.55\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.0001<br \/>\n<\/span><\/p><\/blockquote>\n<p><span style=\"font-family: verdana, geneva; font-size: small;\">I&#8217;d like you to run the code and send me the output (either as a comment or in <a href=\"mailto:paul@sqlskills.com?subject=My latch statistics\" target=\"_blank\">email<\/a>). I&#8217;ll collate all your output and do some blogging for your enjoyment. [Update: I don&#8217;t need any more data &#8211; thanks!]<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva; font-size: small;\">Thanks!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I first started blogging about latches and some of the deeper parts of SQL Server internals last year (see Advanced performance troubleshooting: waits, latches, spinlocks) and now I&#8217;d like to pick up that thread (no scheduling pun intended :-)) and blog some more about some of the common latches that could be a performance bottleneck. [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55,66,91,101],"tags":[],"class_list":["post-488","post","type-post","status-publish","format-standard","hentry","category-latches","category-performance-tuning","category-surveys","category-wait-stats"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Survey: most prevalent latch waits (code to run) - 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\/survey-most-prevalent-latch-waits-code-to-run\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Survey: most prevalent latch waits (code to run) - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"I first started blogging about latches and some of the deeper parts of SQL Server internals last year (see Advanced performance troubleshooting: waits, latches, spinlocks) and now I&#8217;d like to pick up that thread (no scheduling pun intended :-)) and blog some more about some of the common latches that could be a performance bottleneck. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2012-05-10T02:26:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-05-24T00:02:32+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/\",\"name\":\"Survey: most prevalent latch waits (code to run) - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2012-05-10T02:26:00+00:00\",\"dateModified\":\"2016-05-24T00:02:32+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Survey: most prevalent latch waits (code to run)\"}]},{\"@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":"Survey: most prevalent latch waits (code to run) - 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\/survey-most-prevalent-latch-waits-code-to-run\/","og_locale":"en_US","og_type":"article","og_title":"Survey: most prevalent latch waits (code to run) - Paul S. Randal","og_description":"I first started blogging about latches and some of the deeper parts of SQL Server internals last year (see Advanced performance troubleshooting: waits, latches, spinlocks) and now I&#8217;d like to pick up that thread (no scheduling pun intended :-)) and blog some more about some of the common latches that could be a performance bottleneck. [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/","og_site_name":"Paul S. Randal","article_published_time":"2012-05-10T02:26:00+00:00","article_modified_time":"2016-05-24T00:02:32+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/","name":"Survey: most prevalent latch waits (code to run) - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2012-05-10T02:26:00+00:00","dateModified":"2016-05-24T00:02:32+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/survey-most-prevalent-latch-waits-code-to-run\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"Survey: most prevalent latch waits (code to run)"}]},{"@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\/488","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=488"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/488\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}