{"id":1385,"date":"2018-07-10T15:20:31","date_gmt":"2018-07-10T22:20:31","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/glenn\/?p=1385"},"modified":"2018-07-10T15:20:31","modified_gmt":"2018-07-10T22:20:31","slug":"t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/","title":{"rendered":"T-SQL Tuesday #104: Code I Have Written That I Would Hate to Live Without"},"content":{"rendered":"<p><font size=\"2\">Bert Wagner (<a href=\"https:\/\/bertwagner.com\/\">b<\/a>\/<a href=\"https:\/\/twitter.com\/bertwagner\">t<\/a>) is hosting T-SQL Tuesday #104. The invitation is to write about code you\u2019ve written that you would hate to live without. <font size=\"2\">For me, this is almost a no-brainer!<\/font><\/font><\/p>\n<p><font size=\"2\">My <a href=\"https:\/\/www.sqlskills.com\/blogs\/glenn\/category\/dmv-queries\/\">DMV Diagnostic Queries<\/a> represent <strong><u>a lot of code<\/u><\/strong> that I would hate to live without. I use them on a daily basis to gather information about SQL Server instances and databases and to help more quickly understand what configuration and performance issues they have. I<font size=\"2\">\u2019ve been publicly posting these queries since 2009, but I actually started developing them for my own personal use back in 2006. The story about how they came about is kind of interesting\u2026 <\/font><\/font><\/p>\n<p><font size=\"2\">Back in about August of 2006, I was the sole DBA for <a href=\"http:\/\/web.archive.org\/web\/20080601095439\/http:\/\/www.newsgator.com:80\/Individuals\/Default.aspx\">NewsGator<\/a>, which was (<a href=\"http:\/\/inessential.com\/2014\/05\/10\/what_happened_at_newsgator\">at that time<\/a>) an RSS aggregation company. Our main product\/service was the ability to let people \u201csubscribe\u201d to RSS feeds for web sites and blogs, and then have us download the RSS feeds of those sites. We would also manage the \u201cread state\u201d of the RS feeds that you subscribed to, so that as you read through your subscribed content and marked posts as read, we would synchronize your progress across different devices.<\/font><\/p>\n<p><font size=\"2\">I had only been at the company about three months, and we had recently migrated from 32-bit SQL Server 2000 to 64-bit SQL Server 2005 SP1 on a two-node FCI running on new hardware. Performance had been pretty good since the migration, and it was about 4:30PM on a Friday afternoon, when I started making some final quick checks of the health of my instance before getting ready to leave for the weekend.<\/font><\/p>\n<p><font size=\"2\">I noticed that my CPU utilization was running about 90-95%, which was much higher than normal. I tried a few of my standard DBA tricks (at that time) to correct the issue, such as running sp_updatestats, running DBCC FREEPROCCACHE, etc. with no real improvement. I even took the emergency step of \u201cshutting down\u201d the content servers (which were application servers that downloaded the RSS feeds, that typically generated about 90% of my database load). This had no appreciable effect on my CPU utilization.<\/font><\/p>\n<p><font size=\"2\">By now, I was getting worried, since we had a problem that I did not immediately know how to diagnose and correct. By this time, our support team and many of the senior executives in the company were aware that we had a problem because our applications were starting to time out and throw errors. I had a literal parade of different people coming to my desk asking some variation\/combination of \u201cWhat\u2019s wrong with the database?\u201d or \u201cWhat can we do to help?\u201d.<\/font><\/p>\n<p><font size=\"2\">This got so bad that the CTO\/Founder of NewsGator (<a href=\"http:\/\/www.rassoc.com\/gregr\/weblog\/\">Greg Reinaker<\/a>) grabbed a large rolling whiteboard, and wrote something like \u201cGlenn knows there is a problem. He is working on it. Please leave him alone\u201d, which was actually pretty helpful.<\/font><\/p>\n<p><font size=\"2\">So after some time, it ended up being just me and the best developer on the Platform Team (<a href=\"https:\/\/www.linkedin.com\/in\/jeff-tingley-43a14691\/\">Jeff Tingley<\/a>) staying late into the night and next morning, on a call with Microsoft Premier Support trying to diagnose and troubleshoot the issue. Eventually, we figured out that our problem was mainly caused by parameter sniffing in one stored procedure where we were getting one very inefficient plan stuck in the plan cache.<\/font><\/p>\n<p><font size=\"2\">The short-term fix was to use a local variable to store an input parameter for that stored procedure to disable parameter sniffing for that stored procedure, and to periodically recompile a few other stored procedures that were also part of the problem. Jeff and I finally left around 3AM, with the system being relatively stable. I was exhausted from the time and the stress of feeling like the fate of the company rested on my shoulders. I was convinced that I was in big trouble and was possibly going to be fired since it had taken us so long to figure out the problem. Little did I know\u2026<\/font><\/p>\n<p><font size=\"2\">As it turned out, my boss\u2019s boss decided to give both Jeff and I a $500.00 bonus, plus we got a big round of applause at a company meeting the next Monday (which I appreciated much more).<\/font><\/p>\n<p><font size=\"2\">This incident was the genesis of my DMV Diagnostic queries. I never wanted to be in that situation again! Anytime there was any application slowdown, people always assumed that it was a database problem (which was not always the case). Having a set of queries that I could run to figure out what was going on with the database and database server was the key to being able to answer the \u201cWhat\u2019s wrong with the database?\u201d question.<\/font><\/p>\n<p><font size=\"2\">Many thousands of people around the world use my queries on a regular basis, and seem to find them very useful, at least based on the feedback I have gotten over the years. Now you know the story of how they came into being.<\/font><\/p>\n<p><a href=\"https:\/\/bertwagner.com\/2018\/07\/03\/code-youd-hate-to-live-without-t-sql-tuesday-104-invitation\/\"><img fetchpriority=\"high\" decoding=\"async\" width=\"300\" height=\"300\" alt=\"\" src=\"https:\/\/bertwagner.com\/wp-content\/uploads\/2018\/02\/MJ-t-sql-Tuesday.png\"><\/a><\/p>\n<p><font size=\"2\"><a href=\"https:\/\/bertwagner.com\/2018\/07\/03\/code-youd-hate-to-live-without-t-sql-tuesday-104-invitation\/\">Figure 1: Link to Invitation Post<\/a><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bert Wagner (b\/t) is hosting T-SQL Tuesday #104. The invitation is to write about code you\u2019ve written that you would hate to live without. For me, this is almost a no-brainer! My DMV Diagnostic Queries represent a lot of code that I would hate to live without. I use them on a daily basis to [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176,372],"tags":[373],"class_list":["post-1385","post","type-post","status-publish","format-standard","hentry","category-dmv-queries","category-t-sql-tuesday","tag-t-sql-tuesday"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>T-SQL Tuesday #104: Code I Have Written That I Would Hate to Live Without - Glenn Berry<\/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\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"T-SQL Tuesday #104: Code I Have Written That I Would Hate to Live Without - Glenn Berry\" \/>\n<meta property=\"og:description\" content=\"Bert Wagner (b\/t) is hosting T-SQL Tuesday #104. The invitation is to write about code you\u2019ve written that you would hate to live without. For me, this is almost a no-brainer! My DMV Diagnostic Queries represent a lot of code that I would hate to live without. I use them on a daily basis to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/\" \/>\n<meta property=\"og:site_name\" content=\"Glenn Berry\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-10T22:20:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/bertwagner.com\/wp-content\/uploads\/2018\/02\/MJ-t-sql-Tuesday.png\" \/>\n<meta name=\"author\" content=\"Glenn Berry\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Glenn Berry\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/\",\"name\":\"T-SQL Tuesday #104: Code I Have Written That I Would Hate to Live Without - Glenn Berry\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/bertwagner.com\/wp-content\/uploads\/2018\/02\/MJ-t-sql-Tuesday.png\",\"datePublished\":\"2018-07-10T22:20:31+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/#\/schema\/person\/57a8972435106bac7970692fcf5edfa7\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/#primaryimage\",\"url\":\"https:\/\/bertwagner.com\/wp-content\/uploads\/2018\/02\/MJ-t-sql-Tuesday.png\",\"contentUrl\":\"https:\/\/bertwagner.com\/wp-content\/uploads\/2018\/02\/MJ-t-sql-Tuesday.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"T-SQL Tuesday #104: Code I Have Written That I Would Hate to Live Without\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/\",\"name\":\"Glenn Berry\",\"description\":\"Semi-random musings about SQL Server performance\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/?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\/glenn\/#\/schema\/person\/57a8972435106bac7970692fcf5edfa7\",\"name\":\"Glenn Berry\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/64bdac8830f25f2f8cc780f8a1286c66ff1182218009271e7a953639596f7e25?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/64bdac8830f25f2f8cc780f8a1286c66ff1182218009271e7a953639596f7e25?s=96&d=mm&r=g\",\"caption\":\"Glenn Berry\"},\"sameAs\":[\"https:\/\/www.sqlskills.com\/blogs\/glenn\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/author\/glenn\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"T-SQL Tuesday #104: Code I Have Written That I Would Hate to Live Without - Glenn Berry","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\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/","og_locale":"en_US","og_type":"article","og_title":"T-SQL Tuesday #104: Code I Have Written That I Would Hate to Live Without - Glenn Berry","og_description":"Bert Wagner (b\/t) is hosting T-SQL Tuesday #104. The invitation is to write about code you\u2019ve written that you would hate to live without. For me, this is almost a no-brainer! My DMV Diagnostic Queries represent a lot of code that I would hate to live without. I use them on a daily basis to [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/","og_site_name":"Glenn Berry","article_published_time":"2018-07-10T22:20:31+00:00","og_image":[{"url":"https:\/\/bertwagner.com\/wp-content\/uploads\/2018\/02\/MJ-t-sql-Tuesday.png","type":"","width":"","height":""}],"author":"Glenn Berry","twitter_misc":{"Written by":"Glenn Berry","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/","url":"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/","name":"T-SQL Tuesday #104: Code I Have Written That I Would Hate to Live Without - Glenn Berry","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/#primaryimage"},"thumbnailUrl":"https:\/\/bertwagner.com\/wp-content\/uploads\/2018\/02\/MJ-t-sql-Tuesday.png","datePublished":"2018-07-10T22:20:31+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/#\/schema\/person\/57a8972435106bac7970692fcf5edfa7"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/#primaryimage","url":"https:\/\/bertwagner.com\/wp-content\/uploads\/2018\/02\/MJ-t-sql-Tuesday.png","contentUrl":"https:\/\/bertwagner.com\/wp-content\/uploads\/2018\/02\/MJ-t-sql-Tuesday.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/t-sql-tuesday-104-code-i-have-written-that-i-would-hate-to-live-without\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/glenn\/"},{"@type":"ListItem","position":2,"name":"T-SQL Tuesday #104: Code I Have Written That I Would Hate to Live Without"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/glenn\/","name":"Glenn Berry","description":"Semi-random musings about SQL Server performance","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/glenn\/?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\/glenn\/#\/schema\/person\/57a8972435106bac7970692fcf5edfa7","name":"Glenn Berry","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/64bdac8830f25f2f8cc780f8a1286c66ff1182218009271e7a953639596f7e25?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/64bdac8830f25f2f8cc780f8a1286c66ff1182218009271e7a953639596f7e25?s=96&d=mm&r=g","caption":"Glenn Berry"},"sameAs":["https:\/\/www.sqlskills.com\/blogs\/glenn\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/glenn\/author\/glenn\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/posts\/1385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/comments?post=1385"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/posts\/1385\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/media?parent=1385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/categories?post=1385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/tags?post=1385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}