{"id":1126,"date":"2020-01-28T22:14:00","date_gmt":"2020-01-28T12:14:00","guid":{"rendered":"http:\/\/www.moneystock.net\/wp_e\/?p=1126"},"modified":"2020-01-21T22:15:17","modified_gmt":"2020-01-21T12:15:17","slug":"sql-query-performance-tuning","status":"publish","type":"post","link":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/","title":{"rendered":"SQL query performance tuning"},"content":{"rendered":"\n<p><strong>Tuning quick tips<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Getting the entire record before apply condition. -> Solution: Apply condition and paging before select. <\/li><li>Using sub-query -> Solution: Turn it into join <\/li><li>Using Table Variable being used with large data -> Solution: use Temp Table<\/li><\/ol>\n\n\n\n<p>When a query performs badly, 3 things above are the most common causes I came across. After fixed with the solutions above, it performs at an acceptable speed even with a relatively complex query and large data volumes. <\/p>\n\n\n\n<p><strong>Tools<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.sentryone.com\/plan-explorer\">https:\/\/www.sentryone.com\/plan-explorer<\/a><\/p>\n\n\n\n<p>Sentryone plan explore is freeware and helps to figure out the bottleneck from a complex query. I don&#8217;t understand why the SSMS plan presents the execution plan so badly. If you are not happy with SSMS&#8217;s execution plan like me, I strongly recommend Sentry One plan explorer. <\/p>\n\n\n\n<p><strong>Tip<\/strong><\/p>\n\n\n\n<p>From SQL\n2014, Execution plan assumptions changed which leads to better performance.<\/p>\n\n\n\n<p>This is\nreason to upgrade SQL Server and it&#8217;s compatibility level to &gt; 120.<\/p>\n\n\n\n<p><strong>Tuning Questions<\/strong><\/p>\n\n\n\n<p>These are the questions worth we should ask when tuning an SQL query. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Stats missing?<\/li><li>Stats stale?<\/li><li>Sampling adequate?<\/li><li>Multi-column stat help?<\/li><li>Parameter sniffing?<\/li><li>Table variable\/MSTVF?<\/li><li>Only querying the most recent rows?<\/li><li>Date type conversion issue?<\/li><li>Comparing columns from the same table? <\/li><li>Remote data sources?<\/li><li>Predicates being buried in complexity?<\/li><li>Too much in a single query?<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>About Parameter sniffing<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/20864934\/option-recompile-is-always-faster-why\">https:\/\/stackoverflow.com\/questions\/20864934\/option-recompile-is-always-faster-why<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/40432794\/what-are-the-main-differences-between-optionoptimize-for-unknown-and-optionre\/40437749#40437749\">https:\/\/stackoverflow.com\/questions\/40432794\/what-are-the-main-differences-between-optionoptimize-for-unknown-and-optionre\/40437749#40437749<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tuning quick tips Getting the entire record before apply condition. -> Solution: Apply condition and paging before select. Using sub-query -> Solution: Turn it into join Using Table Variable being used with large data -> Solution: use Temp Table When a query performs badly, 3 things above are the most common causes I came across.&hellip; <a class=\"more-link\" href=\"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/\">Continue reading <span class=\"screen-reader-text\">SQL query performance tuning<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[128],"tags":[143,563,562,567],"class_list":["post-1126","post","type-post","status-publish","format-standard","hentry","category-db","tag-mssql","tag-query","tag-sql","tag-tuning","entry"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.8 - aioseo.com -->\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"rocker8942\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.8\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Joe&#039;s Happy Life | Be positive, have fun\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"SQL query performance tuning | Joe&#039;s Happy Life\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2020-01-28T12:14:00+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-01-21T12:15:17+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"SQL query performance tuning | Joe&#039;s Happy Life\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/01\\\/28\\\/sql-query-performance-tuning\\\/#article\",\"name\":\"SQL query performance tuning | Joe's Happy Life\",\"headline\":\"SQL query performance tuning\",\"author\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/author\\\/rocker8942\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/#organization\"},\"datePublished\":\"2020-01-28T22:14:00+08:00\",\"dateModified\":\"2020-01-21T22:15:17+08:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/01\\\/28\\\/sql-query-performance-tuning\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/01\\\/28\\\/sql-query-performance-tuning\\\/#webpage\"},\"articleSection\":\"DB, mssql, Query, SQL, tuning, English\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/01\\\/28\\\/sql-query-performance-tuning\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/moneystock.net\\\/wp_e\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/web-development\\\/#listItem\",\"name\":\"web development\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/web-development\\\/#listItem\",\"position\":2,\"name\":\"web development\",\"item\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/web-development\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/web-development\\\/db\\\/#listItem\",\"name\":\"DB\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/web-development\\\/db\\\/#listItem\",\"position\":3,\"name\":\"DB\",\"item\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/web-development\\\/db\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/01\\\/28\\\/sql-query-performance-tuning\\\/#listItem\",\"name\":\"SQL query performance tuning\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/web-development\\\/#listItem\",\"name\":\"web development\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/01\\\/28\\\/sql-query-performance-tuning\\\/#listItem\",\"position\":4,\"name\":\"SQL query performance tuning\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/category\\\/web-development\\\/db\\\/#listItem\",\"name\":\"DB\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/#organization\",\"name\":\"Joe's Happy Life\",\"description\":\"Be positive, have fun\",\"url\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/author\\\/rocker8942\\\/#author\",\"url\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/author\\\/rocker8942\\\/\",\"name\":\"rocker8942\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/01\\\/28\\\/sql-query-performance-tuning\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d00fcc2c7b0eaab40443e28428a4429ab4a95e12086262d902988f448d76b128?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"rocker8942\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/01\\\/28\\\/sql-query-performance-tuning\\\/#webpage\",\"url\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/01\\\/28\\\/sql-query-performance-tuning\\\/\",\"name\":\"SQL query performance tuning | Joe's Happy Life\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/2020\\\/01\\\/28\\\/sql-query-performance-tuning\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/author\\\/rocker8942\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/author\\\/rocker8942\\\/#author\"},\"datePublished\":\"2020-01-28T22:14:00+08:00\",\"dateModified\":\"2020-01-21T22:15:17+08:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/#website\",\"url\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/\",\"name\":\"Joe's Happy Life\",\"description\":\"Be positive, have fun\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/moneystock.net\\\/wp_e\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"SQL query performance tuning | Joe's Happy Life","description":"","canonical_url":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/#article","name":"SQL query performance tuning | Joe's Happy Life","headline":"SQL query performance tuning","author":{"@id":"https:\/\/moneystock.net\/wp_e\/author\/rocker8942\/#author"},"publisher":{"@id":"https:\/\/moneystock.net\/wp_e\/#organization"},"datePublished":"2020-01-28T22:14:00+08:00","dateModified":"2020-01-21T22:15:17+08:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/#webpage"},"isPartOf":{"@id":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/#webpage"},"articleSection":"DB, mssql, Query, SQL, tuning, English"},{"@type":"BreadcrumbList","@id":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e#listItem","position":1,"name":"Home","item":"https:\/\/moneystock.net\/wp_e","nextItem":{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/category\/web-development\/#listItem","name":"web development"}},{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/category\/web-development\/#listItem","position":2,"name":"web development","item":"https:\/\/moneystock.net\/wp_e\/category\/web-development\/","nextItem":{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/category\/web-development\/db\/#listItem","name":"DB"},"previousItem":{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/category\/web-development\/db\/#listItem","position":3,"name":"DB","item":"https:\/\/moneystock.net\/wp_e\/category\/web-development\/db\/","nextItem":{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/#listItem","name":"SQL query performance tuning"},"previousItem":{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/category\/web-development\/#listItem","name":"web development"}},{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/#listItem","position":4,"name":"SQL query performance tuning","previousItem":{"@type":"ListItem","@id":"https:\/\/moneystock.net\/wp_e\/category\/web-development\/db\/#listItem","name":"DB"}}]},{"@type":"Organization","@id":"https:\/\/moneystock.net\/wp_e\/#organization","name":"Joe's Happy Life","description":"Be positive, have fun","url":"https:\/\/moneystock.net\/wp_e\/"},{"@type":"Person","@id":"https:\/\/moneystock.net\/wp_e\/author\/rocker8942\/#author","url":"https:\/\/moneystock.net\/wp_e\/author\/rocker8942\/","name":"rocker8942","image":{"@type":"ImageObject","@id":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/d00fcc2c7b0eaab40443e28428a4429ab4a95e12086262d902988f448d76b128?s=96&d=mm&r=g","width":96,"height":96,"caption":"rocker8942"}},{"@type":"WebPage","@id":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/#webpage","url":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/","name":"SQL query performance tuning | Joe's Happy Life","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/moneystock.net\/wp_e\/#website"},"breadcrumb":{"@id":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/#breadcrumblist"},"author":{"@id":"https:\/\/moneystock.net\/wp_e\/author\/rocker8942\/#author"},"creator":{"@id":"https:\/\/moneystock.net\/wp_e\/author\/rocker8942\/#author"},"datePublished":"2020-01-28T22:14:00+08:00","dateModified":"2020-01-21T22:15:17+08:00"},{"@type":"WebSite","@id":"https:\/\/moneystock.net\/wp_e\/#website","url":"https:\/\/moneystock.net\/wp_e\/","name":"Joe's Happy Life","description":"Be positive, have fun","inLanguage":"en-US","publisher":{"@id":"https:\/\/moneystock.net\/wp_e\/#organization"}}]},"og:locale":"en_US","og:site_name":"Joe's Happy Life | Be positive, have fun","og:type":"article","og:title":"SQL query performance tuning | Joe's Happy Life","og:url":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/","article:published_time":"2020-01-28T12:14:00+00:00","article:modified_time":"2020-01-21T12:15:17+00:00","twitter:card":"summary","twitter:title":"SQL query performance tuning | Joe's Happy Life"},"aioseo_meta_data":{"post_id":"1126","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-04-24 04:50:01","updated":"2025-06-03 23:53:29","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/moneystock.net\/wp_e\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/moneystock.net\/wp_e\/category\/web-development\/\" title=\"web development\">web development<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/moneystock.net\/wp_e\/category\/web-development\/db\/\" title=\"DB\">DB<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tSQL query performance tuning\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/moneystock.net\/wp_e"},{"label":"web development","link":"https:\/\/moneystock.net\/wp_e\/category\/web-development\/"},{"label":"DB","link":"https:\/\/moneystock.net\/wp_e\/category\/web-development\/db\/"},{"label":"SQL query performance tuning","link":"https:\/\/moneystock.net\/wp_e\/2020\/01\/28\/sql-query-performance-tuning\/"}],"_links":{"self":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/comments?post=1126"}],"version-history":[{"count":2,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1126\/revisions"}],"predecessor-version":[{"id":1128,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1126\/revisions\/1128"}],"wp:attachment":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/media?parent=1126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/categories?post=1126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/tags?post=1126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}