{"id":192,"date":"2023-12-12T08:55:00","date_gmt":"2023-12-12T13:55:00","guid":{"rendered":"https:\/\/www.sqltabletalk.com\/?p=192"},"modified":"2023-12-11T16:38:43","modified_gmt":"2023-12-11T21:38:43","slug":"sql-server-ags-tackling-missing-and-stale-statistics-for-readonly-secondaries","status":"publish","type":"post","link":"https:\/\/www.sqltabletalk.com\/?p=192","title":{"rendered":"SQL Server AGs: Tackling Missing and Stale Statistics for Readonly Secondaries"},"content":{"rendered":"\n<p>In SQL Server&#8217;s Always On Availability Groups, understanding the behavior of statistics on secondary replicas is key to maintaining query performance. This post explores the challenges and solutions for managing statistics in read-only secondary databases and snapshots. We delve into two critical scenarios: the creation of statistics in secondary replicas when they are missing on the primary, and the handling of stale statistics due to differences in primary and secondary workloads. <\/p>\n\n\n\n<p>The secondary replica is a strict copy of primary the primarydatabase. So, the stats are created in tempdb linked with the readonly database. SQL server will maintain statistics of read-only secondary databases in tempdb. Your secondary workload (readonly) will be different from the primary workload (will be mostly writes (more) and reads).<\/p>\n\n\n\n<p>Any statistics that are created on the primary replica is automatically available on the secondary replicas, however the queries that you will run on the secondary replica are, in all likelihood, very different than the ones you run on the primary replica. For this reason, the statistics may either be missing or possibly stale when a query is run on the secondary replica.<\/p>\n\n\n\n<p>To overcome the performance problems that might result from missing or stale stats. Lets review the following scenerios:<\/p>\n\n\n\n<p>Example-1:&nbsp;Missing statistics:&nbsp;Key scenario here is that you execute a query on the secondary replica that requires statistics on a column but the statistics are missing because the same query or any other query that requires the statistics on the same column was never run on the primary replica.<\/p>\n\n\n\n<p>create&nbsp;table T1 (c1 int, c2 int, c3 int)<\/p>\n\n\n\n<p>go<\/p>\n\n\n\n<p>&#8212; insert into T1 1 row<\/p>\n\n\n\n<p>insert&nbsp;into T1 values (-1, -1, -1)<\/p>\n\n\n\n<p>&#8212; query T1 and show that stats got created automatically on T1\/c1<\/p>\n\n\n\n<p>select&nbsp;* from T1 where c1 = 1<\/p>\n\n\n\n<p>&#8212; check the stats on table T1<\/p>\n\n\n\n<p>select&nbsp;* from sys.stats where OBJECT_ID = object_id(&#8216;T1&#8217;)<\/p>\n\n\n\n<p>Here is the output. Note, that it shows that statistics was created automatically on table with object_id as 59757167 which happen to represent table \u2018T1\u2019. Other interesting thing to note here is that there is a new column \u2018is_temporary\u2019 representing that this statistics is permanent implying that it was created on the primary replica. This statistics will survive the failover (assuming no data-loss) and restart.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"79\" src=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1024x79.jpeg\" alt=\"\" class=\"wp-image-193\" srcset=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1024x79.jpeg 1024w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-300x23.jpeg 300w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-768x59.jpeg 768w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-850x65.jpeg 850w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image.jpeg 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>If you query sys.stats table on the secondary replica, you will see the same results because any logged operation is automatically available on the secondary replica. If you want to know more details, you can execute the following:<\/p>\n\n\n\n<p>dbcc&nbsp;show_statistics(&#8216;t1&#8217;, &#8216;c1&#8217;)<\/p>\n\n\n\n<p>The output shows the row count, column on which the statistics is available along with other information.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"264\" src=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1-1024x264.jpeg\" alt=\"\" class=\"wp-image-194\" srcset=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1-1024x264.jpeg 1024w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1-300x77.jpeg 300w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1-768x198.jpeg 768w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1-850x219.jpeg 850w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1.jpeg 1081w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now, let us execute the following query on the secondary replica and check the statistics:<\/p>\n\n\n\n<p>&#8212; query on column C2 to show TEMPSTATS<\/p>\n\n\n\n<p>select&nbsp;* from t1 where c2= -1<\/p>\n\n\n\n<p>&#8211;show the stats on the secondary<\/p>\n\n\n\n<p>select&nbsp;* from sys.stats WHERE OBJECT_ID = OBJECT_ID(&#8216;t1&#8217;)<\/p>\n\n\n\n<p>Here is the output. Note, a new temporary statistic got created with the name \u2018_WA_Sys_00000002_239E4DCF_readonly_database_statistics\u2019 with the \u2018is_temporary\u2019 flag indicating that it is temporary. The statistics was automatically created because the optimizer needed it. The name of the statistics is appended with the suffix \u2018_readonly_database_statistics\u2019. This is done so that this name does not clash with other automatically created statistics on the primary replica. SQL Server 2012 prevents creating any statistics with this suffix. There is an outside chance that a permanent statistics already exists with this name (assuming someone explicitly created a statistics with this name) before database was upgraded to SQL2012. In that case, the temporary statistics creation will fail but your query will still succeed but the optimizer will not have the statistics as needed for optimization.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"106\" src=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-2-1024x106.jpeg\" alt=\"\" class=\"wp-image-195\" srcset=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-2-1024x106.jpeg 1024w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-2-300x31.jpeg 300w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-2-768x80.jpeg 768w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-2-850x88.jpeg 850w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-2.jpeg 1079w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The temporary statistics are stored in TempDB and each statistics typically takes 8K (1 page) of storage. You can always query sys.stats table to find out all temporary statistics and estimate the storage space taken in the temporary database. Other point to note is that statistics created as part of auto-stats use data sampling so the creation of these statistics is fast and does not depend on the size of the table. One drawback is that if the data for the column is skewed then the statistics based on the sampled data are not very accurate. In such cases, if you want, you can create the statistics on the primary replica without sampling and then it will be available on the secondary replica. The temporary statistics are lost when the secondary replica is either restarted or when primary replica fails over.<\/p>\n\n\n\n<p>&nbsp;Example-2:&nbsp;Stale Statistics:&nbsp;Key scenario is that the statistics on a column was either explicitly or automatically created but has become stale due to DML operations. The statistics stay stale because there was no query run on the primary that required this statistics since the last update. Now, if we run a query on the secondary replica, this statistics will get updated automatically so that the optimizer can use it for optimization. Let us take the previous example where we have a table T1 with 1 row and permanent statistics on column C1 and temporary statistics on C2.<\/p>\n\n\n\n<p>&#8212; insert 10000 rows on the primary to make the statistics stale<\/p>\n\n\n\n<p>declare @i int = 0<\/p>\n\n\n\n<p>while (@i &lt; 10000)<\/p>\n\n\n\n<p>begin<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insert into t1 values (@i, @i + 1000, @i + 10000)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set @i = @i + 1<\/p>\n\n\n\n<p>end<\/p>\n\n\n\n<p>&nbsp;Now, we execute the following query on the secondary<\/p>\n\n\n\n<p><em>&#8212; Query on the secondary so that stats are updated with TEMPSTATS<\/em><\/p>\n\n\n\n<p><em>select<\/em>&nbsp;<em>c2 from t1 where c1 = 100<\/em><\/p>\n\n\n\n<p>&nbsp;Here is the output of the statistics. Note that the statistics \u2018_WA_Sys_00000001_239E4DCF\u2019 got updated and is now marked as \u2018is_temporary\u2019 being 1. This implies that SQL Server has created a temporary statistics. This does not mean that the permanent statistics got lost. It is still available but SQL Server knows that there is an updated version on this statistics in TempDB and it will use that.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"102\" src=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-3-1024x102.jpeg\" alt=\"\" class=\"wp-image-196\" srcset=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-3-1024x102.jpeg 1024w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-3-300x30.jpeg 300w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-3-768x77.jpeg 768w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-3-850x85.jpeg 850w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-3.jpeg 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Let us see the full details of this statistics<\/p>\n\n\n\n<p>dbcc&nbsp;show_statistics(&#8216;t1&#8217;, &#8216;_WA_Sys_00000001_239E4DCF&#8217;)<\/p>\n\n\n\n<p>The output is as follows showing that there are now 10001 rows and this statistics in on column C1<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"260\" src=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-5-1024x260.jpeg\" alt=\"\" class=\"wp-image-198\" srcset=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-5-1024x260.jpeg 1024w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-5-300x76.jpeg 300w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-5-768x195.jpeg 768w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-5-850x216.jpeg 850w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-5.jpeg 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>If you restart your secondary replica, the temporary statistics will be lost and you will see the following output. In fact, if you query the statistics on the primary, you will see the same output as well.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"79\" src=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-4-1024x79.jpeg\" alt=\"\" class=\"wp-image-197\" srcset=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-4-1024x79.jpeg 1024w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-4-300x23.jpeg 300w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-4-768x59.jpeg 768w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-4-850x65.jpeg 850w, https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-4.jpeg 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You may wonder what happens if the statistics on C1 get updated on the primary replica. Let us repeat this experiment by running the following query on the primary<\/p>\n\n\n\n<p>&#8212; query t1 and show that stats got created created automatically on t1\/c1<\/p>\n\n\n\n<p>select&nbsp;* from t1 where c1 &gt; 1000<\/p>\n\n\n\n<p>This query will cause the statistics on column C1 to get updated and when it flows to secondary replica, the statistics will be marked as permanent and now the optimizer will use the permanent statistics as it is the latest statistics available on column C1.<\/p>\n\n\n\n<p>In summary, to illustrate various interactions, let us take a table T1 with three columns C1, C2, and C3. For this discussion, it does not matter what the column type is. We will use C1prim&nbsp;and C1sec&nbsp;to represent statistics on column C1 that got created on primary and secondary replica respectively<\/p>\n\n\n\n<p>The following table summarizes various interactions<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Action<\/td><td>Primary Replica<\/td><td>Secondary Replica<\/td><\/tr><tr><td>Query on Secondary with predicate on C1<\/td><td>&nbsp;<\/td><td>C1sec&nbsp;gets created<\/td><\/tr><tr><td>Query on Primary with predicate on C1<\/td><td>C1prim&nbsp;gets created<\/td><td>C1prim&nbsp;is created on when the log for the statistics is processed. At this time, both C1sec&nbsp;and C1prim&nbsp;exist on secondary replica but the C1prim&nbsp;is latest and optimizer will use it. At this time C1sec&nbsp;is not useful and user can explicitly drop it.<\/td><\/tr><tr><td>Memory pressure forces T1 out of cache<\/td><td>&nbsp;<\/td><td>C1sec&nbsp;is removed from the cache but it still persists in TempDB<\/td><\/tr><tr><td>Insert bunch of rows in T1 such that auto-stat threshold is crossed. Now query on the secondary with predicate on C1<\/td><td>&nbsp;<\/td><td>C1sec&nbsp;gets refreshed.<\/td><\/tr><tr><td>Query on Primary with predicate on C2<\/td><td>C2prim&nbsp;gets created<\/td><td>C2prim&nbsp;is created on when the log for the statistics is processed.<\/td><\/tr><tr><td>Insert bunch of rows in T1 such that auto-stat threshold is crossed. Now query on the secondary with predicate on C2<\/td><td>&nbsp;<\/td><td>C2sec&nbsp;gets created. At this time, both C2sec&nbsp;and C2prim&nbsp;exist on secondary replica but the C2sec&nbsp;is latest and optimizer will use it.<\/td><\/tr><tr><td>Do a DDL operation on table T1<\/td><td>&nbsp;<\/td><td>Cached metadata for T1 is deleted and as part of it C1sec&nbsp;and C2sec&nbsp;gets dropped.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The same mechanism works on read-only database and database snapshot.<\/p>\n\n\n\n<p>Managing statistics in SQL Server Availability Groups is important for optimal query performance, especially in read-only secondary replicas. We highlighted the complexities of dealing with missing and stale statistics in such environments. We saw how SQL Server adeptly addresses these challenges by creating temporary statistics in tempdb and updating them as needed. These insights are invaluable for database professionals striving to ensure efficient and reliable query performance in high-availability SQL Server environments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SQL Server&#8217;s Always On Availability Groups, understanding the behavior of statistics on secondary replicas is key to maintaining query performance. This post explores the challenges and solutions for managing statistics in read-only secondary databases and snapshots. We delve into two critical scenarios: the creation of statistics in secondary replicas when they are missing on the primary, and the handling of stale statistics due to differences in primary and secondary workloads. <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[36,5,44],"tags":[50,52,51],"class_list":["post-192","post","type-post","status-publish","format-standard","hentry","category-availability-groups","category-performance","category-statistics","tag-missing-statistics","tag-sql-server-performance","tag-stale-statistics"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server AGs: Tackling Missing and Stale Statistics for Readonly Secondaries - SQL Table Talk<\/title>\n<meta name=\"description\" content=\"In SQL Server&#039;s Always On Availability Groups, understanding the behavior of statistics on secondary replicas is key to maintaining query performance. This post explores the challenges and solutions for managing statistics in read-only secondary databases and snapshots. We delve into two critical scenarios: the creation of statistics in secondary replicas when they are missing on the primary, and the handling of stale statistics due to differences in primary and secondary workloads.\" \/>\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.sqltabletalk.com\/?p=192\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server AGs: Tackling Missing and Stale Statistics for Readonly Secondaries - SQL Table Talk\" \/>\n<meta property=\"og:description\" content=\"In SQL Server&#039;s Always On Availability Groups, understanding the behavior of statistics on secondary replicas is key to maintaining query performance. This post explores the challenges and solutions for managing statistics in read-only secondary databases and snapshots. We delve into two critical scenarios: the creation of statistics in secondary replicas when they are missing on the primary, and the handling of stale statistics due to differences in primary and secondary workloads.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqltabletalk.com\/?p=192\" \/>\n<meta property=\"og:site_name\" content=\"SQL Table Talk\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-12T13:55:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1024x79.jpeg\" \/>\n<meta name=\"author\" content=\"Yvonne Vanslageren\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yvonne Vanslageren\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.sqltabletalk.com\/?p=192#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltabletalk.com\/?p=192\"},\"author\":{\"name\":\"Yvonne Vanslageren\",\"@id\":\"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/68bb31b454bafe9e139183ed4f3e9082\"},\"headline\":\"SQL Server AGs: Tackling Missing and Stale Statistics for Readonly Secondaries\",\"datePublished\":\"2023-12-12T13:55:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.sqltabletalk.com\/?p=192\"},\"wordCount\":1601,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/1947e42a9438bccd91691d8b791888e0\"},\"image\":{\"@id\":\"https:\/\/www.sqltabletalk.com\/?p=192#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1024x79.jpeg\",\"keywords\":[\"missing statistics\",\"SQL Server performance\",\"stale statistics\"],\"articleSection\":[\"Availability Groups\",\"Performance\",\"Statistics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.sqltabletalk.com\/?p=192#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqltabletalk.com\/?p=192\",\"url\":\"https:\/\/www.sqltabletalk.com\/?p=192\",\"name\":\"SQL Server AGs: Tackling Missing and Stale Statistics for Readonly Secondaries - SQL Table Talk\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltabletalk.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.sqltabletalk.com\/?p=192#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.sqltabletalk.com\/?p=192#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1024x79.jpeg\",\"datePublished\":\"2023-12-12T13:55:00+00:00\",\"description\":\"In SQL Server's Always On Availability Groups, understanding the behavior of statistics on secondary replicas is key to maintaining query performance. This post explores the challenges and solutions for managing statistics in read-only secondary databases and snapshots. We delve into two critical scenarios: the creation of statistics in secondary replicas when they are missing on the primary, and the handling of stale statistics due to differences in primary and secondary workloads.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqltabletalk.com\/?p=192#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqltabletalk.com\/?p=192\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqltabletalk.com\/?p=192#primaryimage\",\"url\":\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image.jpeg\",\"contentUrl\":\"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image.jpeg\",\"width\":1080,\"height\":83},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqltabletalk.com\/?p=192#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqltabletalk.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server AGs: Tackling Missing and Stale Statistics for Readonly Secondaries\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqltabletalk.com\/#website\",\"url\":\"https:\/\/www.sqltabletalk.com\/\",\"name\":\"SQL Table Talk\",\"description\":\"Breaking Down SQL Server, One Post at a Time.\",\"publisher\":{\"@id\":\"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/1947e42a9438bccd91691d8b791888e0\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqltabletalk.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/1947e42a9438bccd91691d8b791888e0\",\"name\":\"Stephen Planck\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/64181114edc3de3d99072c049bcec024f025c9536dc89fc8ff1bac58976ca81e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/64181114edc3de3d99072c049bcec024f025c9536dc89fc8ff1bac58976ca81e?s=96&d=mm&r=g\",\"caption\":\"Stephen Planck\"},\"logo\":{\"@id\":\"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/dexterwiki.com\",\"https:\/\/www.linkedin.com\/in\/stephen-planck-4611b692?trk=people-guest_people_search-card&challengeId=AQErf8gbBmcVMwAAAYsyIsxO-0UvU8z7cHrBpZoo_n3xt9qEKpRN5B_jd_LmAMu-OfeArkQ7GDjobJ2uRoQQV35EQdh_rR6kxA&submissionId=09de7067-c335-8e17-40b8-8dc32b60ed6c&challengeSource=AgEcUCw35zpPmAAAAYsyI4vAWhJTV7Nt4vZYKc3V1qiDBpCkKgUvtlOBgYXcE84&challegeType=AgE_wZiTT09IAQAAAYsyI4vDmNvbZIYe6XHju5V2bXVvM3IVxnJslgY&memberId=AgESFTkUShzs_gAAAYsyI4vGYk0Gic1uc5kB6cKOABA26Gw&recognizeDevice=AgHdSZyUSI5CEwAAAYsyI4vKd_koF9JgpsCJShT8QfbK1QMiv8SI\",\"https:\/\/www.youtube.com\/linuxmate\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/68bb31b454bafe9e139183ed4f3e9082\",\"name\":\"Yvonne Vanslageren\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/22c274a7a354b81a0a8dc5b23e8e0be9bdd81a6bc0541474cfb6b954d6bb2089?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/22c274a7a354b81a0a8dc5b23e8e0be9bdd81a6bc0541474cfb6b954d6bb2089?s=96&d=mm&r=g\",\"caption\":\"Yvonne Vanslageren\"},\"url\":\"https:\/\/www.sqltabletalk.com\/?author=2\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server AGs: Tackling Missing and Stale Statistics for Readonly Secondaries - SQL Table Talk","description":"In SQL Server's Always On Availability Groups, understanding the behavior of statistics on secondary replicas is key to maintaining query performance. This post explores the challenges and solutions for managing statistics in read-only secondary databases and snapshots. We delve into two critical scenarios: the creation of statistics in secondary replicas when they are missing on the primary, and the handling of stale statistics due to differences in primary and secondary workloads.","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.sqltabletalk.com\/?p=192","og_locale":"en_US","og_type":"article","og_title":"SQL Server AGs: Tackling Missing and Stale Statistics for Readonly Secondaries - SQL Table Talk","og_description":"In SQL Server's Always On Availability Groups, understanding the behavior of statistics on secondary replicas is key to maintaining query performance. This post explores the challenges and solutions for managing statistics in read-only secondary databases and snapshots. We delve into two critical scenarios: the creation of statistics in secondary replicas when they are missing on the primary, and the handling of stale statistics due to differences in primary and secondary workloads.","og_url":"https:\/\/www.sqltabletalk.com\/?p=192","og_site_name":"SQL Table Talk","article_published_time":"2023-12-12T13:55:00+00:00","og_image":[{"url":"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1024x79.jpeg","type":"","width":"","height":""}],"author":"Yvonne Vanslageren","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Yvonne Vanslageren","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.sqltabletalk.com\/?p=192#article","isPartOf":{"@id":"https:\/\/www.sqltabletalk.com\/?p=192"},"author":{"name":"Yvonne Vanslageren","@id":"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/68bb31b454bafe9e139183ed4f3e9082"},"headline":"SQL Server AGs: Tackling Missing and Stale Statistics for Readonly Secondaries","datePublished":"2023-12-12T13:55:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.sqltabletalk.com\/?p=192"},"wordCount":1601,"commentCount":0,"publisher":{"@id":"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/1947e42a9438bccd91691d8b791888e0"},"image":{"@id":"https:\/\/www.sqltabletalk.com\/?p=192#primaryimage"},"thumbnailUrl":"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1024x79.jpeg","keywords":["missing statistics","SQL Server performance","stale statistics"],"articleSection":["Availability Groups","Performance","Statistics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.sqltabletalk.com\/?p=192#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.sqltabletalk.com\/?p=192","url":"https:\/\/www.sqltabletalk.com\/?p=192","name":"SQL Server AGs: Tackling Missing and Stale Statistics for Readonly Secondaries - SQL Table Talk","isPartOf":{"@id":"https:\/\/www.sqltabletalk.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqltabletalk.com\/?p=192#primaryimage"},"image":{"@id":"https:\/\/www.sqltabletalk.com\/?p=192#primaryimage"},"thumbnailUrl":"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image-1024x79.jpeg","datePublished":"2023-12-12T13:55:00+00:00","description":"In SQL Server's Always On Availability Groups, understanding the behavior of statistics on secondary replicas is key to maintaining query performance. This post explores the challenges and solutions for managing statistics in read-only secondary databases and snapshots. We delve into two critical scenarios: the creation of statistics in secondary replicas when they are missing on the primary, and the handling of stale statistics due to differences in primary and secondary workloads.","breadcrumb":{"@id":"https:\/\/www.sqltabletalk.com\/?p=192#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqltabletalk.com\/?p=192"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqltabletalk.com\/?p=192#primaryimage","url":"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image.jpeg","contentUrl":"https:\/\/www.sqltabletalk.com\/wp-content\/uploads\/2023\/12\/image.jpeg","width":1080,"height":83},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqltabletalk.com\/?p=192#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqltabletalk.com\/"},{"@type":"ListItem","position":2,"name":"SQL Server AGs: Tackling Missing and Stale Statistics for Readonly Secondaries"}]},{"@type":"WebSite","@id":"https:\/\/www.sqltabletalk.com\/#website","url":"https:\/\/www.sqltabletalk.com\/","name":"SQL Table Talk","description":"Breaking Down SQL Server, One Post at a Time.","publisher":{"@id":"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/1947e42a9438bccd91691d8b791888e0"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqltabletalk.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/1947e42a9438bccd91691d8b791888e0","name":"Stephen Planck","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/64181114edc3de3d99072c049bcec024f025c9536dc89fc8ff1bac58976ca81e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/64181114edc3de3d99072c049bcec024f025c9536dc89fc8ff1bac58976ca81e?s=96&d=mm&r=g","caption":"Stephen Planck"},"logo":{"@id":"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/dexterwiki.com","https:\/\/www.linkedin.com\/in\/stephen-planck-4611b692?trk=people-guest_people_search-card&challengeId=AQErf8gbBmcVMwAAAYsyIsxO-0UvU8z7cHrBpZoo_n3xt9qEKpRN5B_jd_LmAMu-OfeArkQ7GDjobJ2uRoQQV35EQdh_rR6kxA&submissionId=09de7067-c335-8e17-40b8-8dc32b60ed6c&challengeSource=AgEcUCw35zpPmAAAAYsyI4vAWhJTV7Nt4vZYKc3V1qiDBpCkKgUvtlOBgYXcE84&challegeType=AgE_wZiTT09IAQAAAYsyI4vDmNvbZIYe6XHju5V2bXVvM3IVxnJslgY&memberId=AgESFTkUShzs_gAAAYsyI4vGYk0Gic1uc5kB6cKOABA26Gw&recognizeDevice=AgHdSZyUSI5CEwAAAYsyI4vKd_koF9JgpsCJShT8QfbK1QMiv8SI","https:\/\/www.youtube.com\/linuxmate"]},{"@type":"Person","@id":"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/68bb31b454bafe9e139183ed4f3e9082","name":"Yvonne Vanslageren","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqltabletalk.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/22c274a7a354b81a0a8dc5b23e8e0be9bdd81a6bc0541474cfb6b954d6bb2089?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/22c274a7a354b81a0a8dc5b23e8e0be9bdd81a6bc0541474cfb6b954d6bb2089?s=96&d=mm&r=g","caption":"Yvonne Vanslageren"},"url":"https:\/\/www.sqltabletalk.com\/?author=2"}]}},"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqltabletalk.com\/index.php?rest_route=\/wp\/v2\/posts\/192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqltabletalk.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqltabletalk.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqltabletalk.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqltabletalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=192"}],"version-history":[{"count":2,"href":"https:\/\/www.sqltabletalk.com\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":211,"href":"https:\/\/www.sqltabletalk.com\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions\/211"}],"wp:attachment":[{"href":"https:\/\/www.sqltabletalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqltabletalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqltabletalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}