{"id":95204,"date":"2022-12-19T17:00:57","date_gmt":"2022-12-19T17:00:57","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=95204"},"modified":"2022-12-27T19:28:19","modified_gmt":"2022-12-27T19:28:19","slug":"sql-server-2022-how-auto-drop-statistics-work","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/sql-server-2022-how-auto-drop-statistics-work\/","title":{"rendered":"SQL Server 2022: How Auto-Drop Statistics Work"},"content":{"rendered":"<p>Auto-Create statistics is a well-known feature critical for the database performance. When one statistic is auto created, changing the table structure is not blocked by the presence of the statistic. An auto-created statistics is also dropped automatically when a schema change happens.<\/p>\n<p>On the other hand, if the statistic is created by the user, any schema change will be blocked by the presence of the statistic.<\/p>\n<p>The Auto-Drop setting on a statistic is a new <strong>SQL Server 2022<\/strong> feature to change this behaviour. If a user created statistic is set with the auto-drop option, it will behave as an auto-created statistics: It will be automatically dropped if a schema change happens.<\/p>\n<p>Let&#8217;s make an example using <em>AdventureWorks2019<\/em>. You can download it on <a href=\"https:\/\/learn.microsoft.com\/en-us\/sql\/samples\/adventureworks-install-configure?view=sql-server-ver16&amp;tabs=ssms\">https:\/\/learn.microsoft.com\/en-us\/sql\/samples\/adventureworks-install-configure?view=sql-server-ver16&amp;tabs=ssms<\/a> . Restore the backup in a <strong>SQL Server 2022<\/strong>.<\/p>\n<h2>Demonstration<\/h2>\n<p>Our example will use the table <em>Production.Product<\/em> and focus on the field <em>ListPrice<\/em>. Let&#8217;s see a sequence of steps for this demonstration.<\/p>\n<p>1) Drop the constraint <em>CK_product_listprice<\/em>, otherwise it will block the demonstration<\/p>\n<div><span style=\"font-family: Courier New; font-size: 10pt;\"> <span style=\"color: blue;\">ALTER<\/span>\u00a0<span style=\"color: blue;\">TABLE<\/span>\u00a0<span style=\"color: maroon;\">production<\/span><span style=\"color: silver;\">.<\/span><span style=\"color: maroon;\">product<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue;\">DROP<\/span>\u00a0<span style=\"color: blue;\">CONSTRAINT<\/span>\u00a0<span style=\"color: maroon;\">ck_product_listprice<\/span>\u00a0 <\/span><\/div>\n<p>2) Check the existing statistics. You will not find anyone related to ListPrice field.<\/p>\n<p>3) Execute the following query:<\/p>\n<div><span style=\"font-family: Courier New; font-size: 10pt;\"> <span style=\"color: blue;\">SELECT<\/span>\u00a0<span style=\"color: silver;\">*<\/span> <br \/>\n<span style=\"color: blue;\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon;\">production<\/span><span style=\"color: silver;\">.<\/span><span style=\"color: maroon;\">product<\/span> <br \/>\n<span style=\"color: blue;\">WHERE<\/span>\u00a0\u00a0<span style=\"color: maroon;\">listprice<\/span>\u00a0<span style=\"color: silver;\">=<\/span>\u00a0<span style=\"color: black;\">10<\/span>\u00a0 <\/span><\/div>\n<p>3) Check the statistics again. A new statistic was automatically created for the ListPrice field.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"658\" class=\"wp-image-95205\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/12\/interface-grafica-do-usuario-texto-aplicativo-d-1.png\" alt=\"Interface gr\u00e1fica do usu\u00e1rio, Texto, Aplicativo\n\nDescri\u00e7\u00e3o gerada automaticamente\" \/><\/p>\n<p>4) Alter the column <em>ListPrice<\/em>.<\/p>\n<div><span style=\"font-family: Courier New; font-size: 10pt;\"> <span style=\"color: blue;\">ALTER<\/span>\u00a0<span style=\"color: blue;\">TABLE<\/span>\u00a0<span style=\"color: maroon;\">production<\/span><span style=\"color: silver;\">.<\/span><span style=\"color: maroon;\">product<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue;\">ALTER<\/span>\u00a0<span style=\"color: blue;\">COLUMN<\/span>\u00a0<span style=\"color: maroon;\">listprice<\/span>\u00a0<span style=\"color: black; font-style: italic;\">NUMERIC<\/span><span style=\"color: maroon;\">(<\/span><span style=\"color: black;\">18<\/span><span style=\"color: silver;\">,<\/span>\u00a0<span style=\"color: black;\">2<\/span><span style=\"color: maroon;\">)<\/span>\u00a0 <\/span><\/div>\n<p>5) Check the statistics again. SQL Server will drop the auto-created statistic automatically.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"277\" height=\"164\" class=\"wp-image-95206\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/12\/texto-descricao-gerada-automaticamente.png\" alt=\"Texto\n\nDescri\u00e7\u00e3o gerada automaticamente\" \/><\/p>\n<p>6) Create a new statistics on the field <em>ListPrice<\/em>. It&#8217;s a user created statistics.<\/p>\n<div><span style=\"font-family: Courier New; font-size: 10pt;\"> <span style=\"color: blue;\">CREATE<\/span>\u00a0<span style=\"color: blue;\">STATISTICS<\/span>\u00a0<span style=\"color: maroon;\">[mystats]<\/span>\u00a0<span style=\"color: blue;\">ON<\/span>\u00a0<span style=\"color: maroon;\">production<\/span><span style=\"color: silver;\">.<\/span><span style=\"color: maroon;\">product<\/span><span style=\"color: maroon;\">(<\/span><span style=\"color: maroon;\">listprice<\/span><span style=\"color: maroon;\">)<\/span>\u00a0 <\/span><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"265\" height=\"218\" class=\"wp-image-95207\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/12\/texto-descricao-gerada-automaticamente-1.png\" alt=\"Texto\n\nDescri\u00e7\u00e3o gerada automaticamente\" \/><\/p>\n<p>7) Try to change the schema again. An error will happen.<\/p>\n<div><span style=\"font-family: Courier New; font-size: 10pt;\"> <span style=\"color: blue;\">ALTER<\/span>\u00a0<span style=\"color: blue;\">TABLE<\/span>\u00a0<span style=\"color: maroon;\">production<\/span><span style=\"color: silver;\">.<\/span><span style=\"color: maroon;\">product<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue;\">ALTER<\/span>\u00a0<span style=\"color: blue;\">COLUMN<\/span>\u00a0<span style=\"color: maroon;\">listprice<\/span>\u00a0<span style=\"color: black; font-style: italic;\">MONEY<\/span>\u00a0 <\/span><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1181\" height=\"344\" class=\"wp-image-95208\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/12\/interface-grafica-do-usuario-texto-aplicativo-d-2.png\" alt=\"Interface gr\u00e1fica do usu\u00e1rio, Texto, Aplicativo\n\nDescri\u00e7\u00e3o gerada automaticamente com confian\u00e7a m\u00e9dia\" \/><\/p>\n<p>8) Drop the user created statistic<\/p>\n<div><span style=\"font-family: Courier New; font-size: 10pt;\"> <span style=\"color: blue;\">DROP<\/span>\u00a0<span style=\"color: maroon;\">statistic<\/span>\u00a0<span style=\"color: maroon;\">production<\/span><span style=\"color: silver;\">.<\/span><span style=\"color: maroon;\">product<\/span><span style=\"color: silver;\">.<\/span><span style=\"color: maroon;\">mystats<\/span>\u00a0 <\/span><\/div>\n<p>9) Create the statistics again, this time using the auto-drop option.<\/p>\n<div><span style=\"font-family: Courier New; font-size: 10pt;\"> <span style=\"color: blue;\">CREATE<\/span>\u00a0<span style=\"color: maroon;\">statistics<\/span>\u00a0<span style=\"color: maroon;\">[mystats]<\/span>\u00a0<span style=\"color: blue;\">ON<\/span>\u00a0<span style=\"color: maroon;\">production<\/span><span style=\"color: silver;\">.<\/span><span style=\"color: maroon;\">product<\/span><span style=\"color: maroon;\">(<\/span><span style=\"color: maroon;\">listprice<\/span><span style=\"color: maroon;\">)<\/span>\u00a0<span style=\"color: blue;\">WITH<\/span>\u00a0<span style=\"color: maroon;\">auto_drop<\/span><span style=\"color: silver;\">=<\/span><span style=\"color: blue;\">ON<\/span>\u00a0 <\/span><\/div>\n<p>10) Try to change the schema again. This time it will work and the statistics mystats will be dropped<\/p>\n<div><span style=\"font-family: Courier New; font-size: 10pt;\"> <span style=\"color: blue;\">ALTER<\/span>\u00a0<span style=\"color: blue;\">TABLE<\/span>\u00a0<span style=\"color: maroon;\">production<\/span><span style=\"color: silver;\">.<\/span><span style=\"color: maroon;\">product<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue;\">ALTER<\/span>\u00a0<span style=\"color: blue;\">COLUMN<\/span>\u00a0<span style=\"color: maroon;\">listprice<\/span>\u00a0<span style=\"color: black; font-style: italic;\">MONEY<\/span>\u00a0 <\/span><\/div>\n<h2>Checking which statistics have the auto-drop option<\/h2>\n<p>A simple query can help identify which statistics have the auto-drop option and which ones doesn&#8217;t:<\/p>\n<div><span style=\"font-family: Courier New; font-size: 10pt;\"> <span style=\"color: blue;\">SELECT<\/span>\u00a0<span style=\"color: maroon;\">object_id<\/span><span style=\"color: silver;\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue;\">NAME<\/span><span style=\"color: silver;\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon;\">auto_drop<\/span> <br \/>\n<span style=\"color: blue;\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon;\">sys<\/span><span style=\"color: silver;\">.<\/span><span style=\"color: maroon;\">stats<\/span>\u00a0 <\/span><\/div>\n<h2>Conclusion<\/h2>\n<p>This is a simple new feature, but it can help in some scenarios of version control and automated deployment for database schemas.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Auto-Create statistics is a well-known feature critical for the database performance. When one statistic is auto created, changing the table structure is not blocked by the presence of the statistic. An auto-created statistics is also dropped automatically when a schema change happens. On the other hand, if the statistic is created by the user, any&#8230;&hellip;<\/p>\n","protected":false},"author":50808,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[147170,5296],"coauthors":[6810],"class_list":["post-95204","post","type-post","status-publish","format-standard","hentry","category-blogs","tag-sql-server-2022","tag-statistics"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/95204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/50808"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=95204"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/95204\/revisions"}],"predecessor-version":[{"id":95223,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/95204\/revisions\/95223"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=95204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=95204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=95204"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=95204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}