{"id":822,"date":"2017-03-23T06:00:48","date_gmt":"2017-03-23T13:00:48","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/erin\/?p=822"},"modified":"2017-03-20T12:02:32","modified_gmt":"2017-03-20T19:02:32","slug":"sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/","title":{"rendered":"SQLskills SQL101: Updating SQL Server Statistics Part I \u2013 Automatic Updates"},"content":{"rendered":"<p>One of my favorite topics in SQL Server is statistics, and in my next two posts I want to cover how they are updated: either by SQL Server or by you.<\/p>\n<p>We\u2019ll start with updates by SQL Server, and these happen automatically. In order for automatic updates of statistics to occur, the AUTO UPDATE STATISTICS database option must be enabled for the database:<\/p>\n<figure id=\"attachment_823\" aria-describedby=\"caption-attachment-823\" style=\"width: 690px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2017\/03\/autoupdate.jpg\"><img fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-823\" src=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2017\/03\/autoupdate.jpg\" alt=\"Auto Update Statistics option via SSMS\" width=\"690\" height=\"337\" srcset=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2017\/03\/autoupdate.jpg 690w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2017\/03\/autoupdate-300x147.jpg 300w\" sizes=\"(max-width: 690px) 100vw, 690px\" \/><\/a><figcaption id=\"caption-attachment-823\" class=\"wp-caption-text\">Auto Update Statistics option via SSMS<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>This option is enabled by default for every new database you create in SQL Server 2005 and higher, and it is recommended to leave this option enabled. If you\u2019re not sure if this option is enabled, you can check in the UI or you can use the following T-SQL:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT\r\n\t&#x5B;name] &#x5B;DatabaseName],\r\n\tCASE\r\n\t\tWHEN &#x5B;is_auto_update_stats_on] = 1 THEN 'Enabled'\r\n\t\tELSE 'Disabled'\r\n\tEND &#x5B;AutoUpdateStats]\r\nFROM &#x5B;sys].&#x5B;databases]\r\nORDER BY &#x5B;name];\r\nGO\r\n<\/pre>\n<p>If you want to enable the option, you can run:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nUSE &#x5B;master];\r\nGO\r\nALTER DATABASE &#x5B;&lt;database_name_here] SET AUTO_UPDATE_STATISTICS ON WITH NO_WAIT;\r\nGO\r\n<\/pre>\n<p>With the option enabled, SQL Server marks statistics as out of date based on internal thresholds.<\/p>\n<p>For SQL Server 2014 and earlier, the threshold was 500 rows plus 20% of the total rows in a table. For example, if I have a table with 10,000 rows in it, when 2500 rows have changed, then SQL Server marks the statistic as out of date. There are exceptions to this (e.g. when a table has less than 500 rows, or if the table is temporary), but in general this threshold is what you need to remember.<\/p>\n<p>A new trace flag, 2371, was introduced in SQL Server 2008R2 SP1 to lower this threshold. This change was designed to target large tables. Imagine a table with 10 million rows; over 2 million rows would need to change before statistics would be marked as out of date. With trace flag 2371, the threshold is lower.<\/p>\n<p>In SQL Server 2016, the threshold introduced by trace flag 2371 is used if you have the compatibility mode for a database set to 130. This means that in SQL Server 2016, you only need to use trace flag 2371 to get that lower threshold if you have the database compatibility mode set to 120 or lower.<br \/>\nIf statistics have been marked as out of date, then they will be updated by SQL Server automatically the next time they are used in a query. Understand that they are not updated the moment they are out of date\u2026they are not updated until they are needed. Imagine the following scenarios using the original threshold:<\/p>\n<p><strong>Example 1 &#8211; PhysicianData<\/strong><\/p>\n<table width=\"618\">\n<tbody>\n<tr>\n<td width=\"216\"><strong>Date\/Time<\/strong><\/td>\n<td width=\"402\"><strong>Action<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"216\">Sunday, March 19, 2017 2:00 AM<\/td>\n<td width=\"402\">Statistics updated for table PhysicianData, which has 500,000 rows in it<\/td>\n<\/tr>\n<tr>\n<td width=\"216\">Monday, March 21, 6:00 AM<\/td>\n<td width=\"402\">Processing job runs, and 50,000 new rows are added to the PhysicianData table<\/td>\n<\/tr>\n<tr>\n<td width=\"216\">Tuesday, March 21, 6:00 AM<\/td>\n<td width=\"402\">Processing job runs, and 50,500 new rows are added to the PhysicianData table; statistics for PhysicianData are marked as out of date<\/td>\n<\/tr>\n<tr>\n<td width=\"216\">Tuesday, March 21, 7:35 AM<\/td>\n<td width=\"402\">A user queries PhysicianData for the first time since processing ran at 6:00 AM; statistics for PhysicianData are updated<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>Example 2 &#8211; PatientData<\/strong><\/p>\n<table width=\"618\">\n<tbody>\n<tr>\n<td width=\"222\"><strong>Date\/Time<\/strong><\/td>\n<td width=\"396\"><strong>Action<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"222\">Sunday, March 19, 2017 2:00 AM<\/td>\n<td width=\"396\">Statistics updated for table PatientData, which has 2,000,000 rows in it<\/td>\n<\/tr>\n<tr>\n<td width=\"222\">Monday, March 20, all day<\/td>\n<td width=\"396\">Different processes and user activities access PatientData, adding new rows, changing existing rows.\u00a0 By the end of day 100,000 rows have changed or been added.<\/td>\n<\/tr>\n<tr>\n<td width=\"222\">Tuesday, March 21, all day<\/td>\n<td width=\"396\">Different processes and user activities access PatientData, adding new rows, changing existing rows.\u00a0 By the end of day 250,000 rows have changed or been added.<\/td>\n<\/tr>\n<tr>\n<td width=\"222\">Wednesday, March 22, all day<\/td>\n<td width=\"396\">Different processes and user activities access PatientData, adding new rows, changing existing rows.\u00a0 At 8:15PM, 400,500 rows have changed or been added.<\/td>\n<\/tr>\n<tr>\n<td width=\"222\">Wednesday, March 22, 8:16 PM<\/td>\n<td width=\"396\">A user queries PatientData; statistics for PatientData are updated<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>I\u2019ve given two very contrived example to help you understand that statistics are not always updated the exact moment they are marked as out of date.\u00a0 They might be \u2013 if the table has a lot of activity, but they might not be.<\/p>\n<p>As I stated originally, it is recommended to leave this option enabled for a database.\u00a0 However, we do not want to rely on SQL Server for our statistics updates.\u00a0 In fact, think of this option as a safety net for statistics.\u00a0 <em>We<\/em> want to control when statistics are updated, not SQL Server.\u00a0 Consider of the first scenario I described, where statistics updated at 7:35AM.\u00a0 If that\u2019s a busy time of day and this is a large table, it could affect performance in the system.\u00a0 It\u2019s preferable to have statistics updated when the system has less activity, so that resource use doesn\u2019t contend with user activity, but we always want to leave Auto Update Statistics enabled for a database&#8230;just in case.<\/p>\n<p>Additional Resources:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.sqlskills.com\/blogs\/erin\/understanding-when-statistics-will-automatically-update\/\">Understanding When Statistics Will Automatically Update<\/a><\/li>\n<li><a href=\"https:\/\/www.sqlskills.com\/blogs\/erin\/testing-automatic-updates-statistics\/\">Further Testing with Automatic Updates to Statistics<\/a><\/li>\n<li><a href=\"https:\/\/support.microsoft.com\/en-us\/help\/2754171\/controlling-autostat-auto-update-statistics-behavior-in-sql-server\">Controlling Autostat (AUTO_UPDATE_STATISTICS) behavior in SQL Server<\/a><\/li>\n<li><a href=\"https:\/\/blogs.msdn.microsoft.com\/saponsqlserver\/2011\/09\/07\/changes-to-automatic-update-statistics-in-sql-server-traceflag-2371\/\">Changes to automatic update statistics in SQL Server &#8211; traceflag 2371<\/a><\/li>\n<li><a href=\"https:\/\/support.microsoft.com\/en-us\/help\/195565\/statistical-maintenance-functionality-autostats-in-sql-server\" class=\"broken_link\">Statistical maintenance functionality (autostats) in SQL Server<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>One of my favorite topics in SQL Server is statistics, and in my next two posts I want to cover how they are updated: either by SQL Server or by you. We\u2019ll start with updates by SQL Server, and these happen automatically. In order for automatic updates of statistics to occur, the AUTO UPDATE STATISTICS [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47,17],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SQLskills SQL101: Updating SQL Server Statistics Part I \u2013 Automatic Updates - Erin Stellato<\/title>\n<meta name=\"description\" content=\"Updating SQL Server statistics can happen automatically if the Auto Update Statistics option is enabled for a database; leaving this enabled is recommended.\" \/>\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\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQLskills SQL101: Updating SQL Server Statistics Part I \u2013 Automatic Updates - Erin Stellato\" \/>\n<meta property=\"og:description\" content=\"Updating SQL Server statistics can happen automatically if the Auto Update Statistics option is enabled for a database; leaving this enabled is recommended.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/\" \/>\n<meta property=\"og:site_name\" content=\"Erin Stellato\" \/>\n<meta property=\"article:published_time\" content=\"2017-03-23T13:00:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-03-20T19:02:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2017\/03\/autoupdate.jpg\" \/>\n<meta name=\"author\" content=\"Erin Stellato\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Erin Stellato\" \/>\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\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/\",\"name\":\"SQLskills SQL101: Updating SQL Server Statistics Part I \u2013 Automatic Updates - Erin Stellato\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#website\"},\"datePublished\":\"2017-03-23T13:00:48+00:00\",\"dateModified\":\"2017-03-20T19:02:32+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158\"},\"description\":\"Updating SQL Server statistics can happen automatically if the Auto Update Statistics option is enabled for a database; leaving this enabled is recommended.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQLskills SQL101: Updating SQL Server Statistics Part I \u2013 Automatic Updates\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/\",\"name\":\"Erin Stellato\",\"description\":\"The SQL Sequel\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158\",\"name\":\"Erin Stellato\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g\",\"caption\":\"Erin Stellato\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/erin\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/author\/erin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQLskills SQL101: Updating SQL Server Statistics Part I \u2013 Automatic Updates - Erin Stellato","description":"Updating SQL Server statistics can happen automatically if the Auto Update Statistics option is enabled for a database; leaving this enabled is recommended.","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\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/","og_locale":"en_US","og_type":"article","og_title":"SQLskills SQL101: Updating SQL Server Statistics Part I \u2013 Automatic Updates - Erin Stellato","og_description":"Updating SQL Server statistics can happen automatically if the Auto Update Statistics option is enabled for a database; leaving this enabled is recommended.","og_url":"https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/","og_site_name":"Erin Stellato","article_published_time":"2017-03-23T13:00:48+00:00","article_modified_time":"2017-03-20T19:02:32+00:00","og_image":[{"url":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2017\/03\/autoupdate.jpg"}],"author":"Erin Stellato","twitter_misc":{"Written by":"Erin Stellato","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/","url":"https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/","name":"SQLskills SQL101: Updating SQL Server Statistics Part I \u2013 Automatic Updates - Erin Stellato","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#website"},"datePublished":"2017-03-23T13:00:48+00:00","dateModified":"2017-03-20T19:02:32+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158"},"description":"Updating SQL Server statistics can happen automatically if the Auto Update Statistics option is enabled for a database; leaving this enabled is recommended.","breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/sqlskills-sql101-updating-sql-server-statistics-part-i-automatic-updates\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/erin\/"},{"@type":"ListItem","position":2,"name":"SQLskills SQL101: Updating SQL Server Statistics Part I \u2013 Automatic Updates"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/erin\/","name":"Erin Stellato","description":"The SQL Sequel","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/erin\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158","name":"Erin Stellato","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g","caption":"Erin Stellato"},"sameAs":["http:\/\/3.209.169.194\/blogs\/erin"],"url":"https:\/\/www.sqlskills.com\/blogs\/erin\/author\/erin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts\/822"}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/comments?post=822"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts\/822\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/media?parent=822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/categories?post=822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/tags?post=822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}