{"id":1130,"date":"2007-11-13T04:28:41","date_gmt":"2007-11-13T04:28:41","guid":{"rendered":"\/blogs\/paul\/post\/Auto-shrink-e28093-turn-it-OFF!.aspx"},"modified":"2017-06-03T08:22:01","modified_gmt":"2017-06-03T15:22:01","slug":"auto-shrink-turn-it-off","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/","title":{"rendered":"Auto-shrink \u2013 turn it OFF!"},"content":{"rendered":"<p style=\"text-align: justify;\"><em>I\u2019m in the middle of a flight from Washington D.C. to Zurich on the way to Barcelona for <a href=\"http:\/\/www.microsoft.com\/en-us\/\">TechEd IT Forum<\/a> and I can\u2019t sleep \u2013 Kimberly\u2019s out like a light so what else is there to do except write another blog post?<\/em> :-)<em>OK &#8211; actually posting this from Barcelona on Tuesday before our first of 12 sessions here!<\/em><\/p>\n<p style=\"text-align: justify;\">In the Database Maintenance workshop we did at SQL Connections last week I promised to blog a script I used to show how data file shrink operations cause massive fragmentation of indexes. The reason is that data file shrink starts at the end of the data file and moves a single page at a time to a free space below the shrink threshold. In the process of doing so, it perfectly reverses the physical order of the pages comprising the leaf level of an index \u2013 thus perfectly fragmenting it!!<\/p>\n<p style=\"text-align: justify;\">Let\u2019s try out my simple script that demonstrates this. First thing I\u2019m going to do is create a new database and create a 10MB \u2018filler\u2019 table, which I\u2019m going to then drop later to create a space that shrink can use.<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nUSE MASTER;\r\nGO\r\n \r\nIF DATABASEPROPERTYEX ('shrinktest', 'Version') &gt; 0\r\n      DROP DATABASE shrinktest;\r\n \r\nCREATE DATABASE shrinktest;\r\nGO\r\nUSE shrinktest;\r\nGO\r\n \r\nSET NOCOUNT ON;\r\nGO\r\n \r\n-- Create and fill the filler table\r\nCREATE TABLE filler (c1 INT IDENTITY, c2 VARCHAR(8000))\r\nGO\r\nDECLARE @a INT;\r\nSELECT @a = 1;\r\nWHILE (@a &lt; 1280) -- insert 10MB\r\nBEGIN\r\n      INSERT INTO filler VALUES (REPLICATE ('a', 5000));\r\n      SELECT @a = @a + 1;\r\nEND;\r\nGO\r\n<\/pre>\n<p>Next I\u2019ll create the \u2018production\u2019 table that we\u2019d really like to keep in optimal shape for performance:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\n-- Create and fill the production table\r\nCREATE TABLE production (c1 INT IDENTITY, c2 VARCHAR (8000));\r\nCREATE CLUSTERED INDEX prod_cl ON production (c1);\r\nGO\r\nDECLARE @a INT;\r\nSELECT @a = 1;\r\nWHILE (@a &lt; 1280) -- insert 10MB\r\nBEGIN\r\n      INSERT INTO production VALUES (REPLICATE ('a', 5000));\r\n      SELECT @a = @a + 1;\r\nEND;\r\nGO \r\n<\/pre>\n<p style=\"text-align: justify;\">Now I\u2019ll use the <em>sys.dm_db_index_physical_stats<\/em> DMV to check the fragmentation of the production table\u2019s clustered index \u2013 it should be almost zero:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\n-- check the fragmentation of the production table\r\nSELECT avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats (\r\n      DB_ID ('shrinktest'), OBJECT_ID ('production'), 1, NULL, 'LIMITED');\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\navg_fragmentation_in_percent\r\n----------------------------\r\n0.390930414386239\r\n<\/pre>\n<p style=\"text-align: justify;\">This is what I expected. Now I\u2019m going to drop the filler table, run a shrink operation (auto-shrink, <em>DBCC SHRINKDATABASE<\/em> and <em>DBCC SHRINKFILE<\/em> all use the exact same code under the covers) and then check the fragmentation again:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\n-- drop the filler table and shrink the database\r\nDROP TABLE filler;\r\nGO\r\n \r\n-- shrink the database\r\nDBCC SHRINKDATABASE (shrinktest);\r\nGO\r\n \r\n-- check the index fragmentation again\r\nSELECT avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats (\r\n      DB_ID ('shrinktest'), OBJECT_ID ('production'), 1, NULL, 'LIMITED');\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\navg_fragmentation_in_percent\r\n\r\n----------------------------\r\n99.7654417513683\r\n<\/pre>\n<p style=\"text-align: justify;\">Wow! The index went from almost 0% fragmented to almost 100% fragmented \u2013 the shrink operation totally reversed the physical ordering of the leaf level of the clustered index \u2013 nasty.<\/p>\n<p style=\"text-align: justify;\">One of the common maintenance operations I see at customer sites is to run a database shrink at some interval, and I always advise against it \u2013 now you can see why. Running a regular shrink operation can cause horrible fragmentation problems. The worst problems I see are those customers with maintenance plans that rebuild all indexes and then run a shrink to remove the extra space necessary for the index rebuilds \u2013 completely undoing the effects of the index rebuild!<\/p>\n<p style=\"text-align: justify;\">One other common thing I see is to have auto-shrink set on for one or databases. This is bad for several reasons:<\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"text-align: justify;\">Shrink causes index fragmentation, as I\u2019ve just demonstrated above.<\/li>\n<li style=\"text-align: justify;\">You can&#8217;t control when it kicks in. Although it doesn&#8217;t have any effect like long-term blocking, it does take up a lot of resources, both IO and CPU. It also moves a lot of data through the buffer pool and so can cause hot pages to be pushed out to disk, slowing things down further. If the server is already pushing the limits of the IO subsystem, running shrink may push it over, causing long disk queue lengths and possibly IO timeouts.<\/li>\n<li style=\"text-align: justify;\">You&#8217;re likely to get into a death-spiral of auto-grow then auto-shrink then auto-grow then auto-shrink&#8230; (in my experience, if someone is using auto-shrink, they&#8217;re most likely using and relying on auto-grow too). An active database usually requires free space for normal operations &#8211; so if you take that free space away then the database just has to grow again. This is bad for several reasons:\n<ul>\n<li style=\"text-align: justify;\">Repeatedly shrinking and growing the data files will cause file-system level fragmentation, which can slow down performance<\/li>\n<li style=\"text-align: justify;\">It wastes a huge amount of resources, basically running the shrink algorithm for no reason<\/li>\n<li style=\"text-align: justify;\">Auto-grow itself can be bad, especially if you don&#8217;t have Instant File Initialization turned on &#8211; see\u00a0<a href=\"http:\/\/www.sqlskills.com\/blogs\/kimberly\/instant-initialization-what-why-and-how\/\">this post<\/a><span style=\"color: #000000;\">\u00a0from Kimberly&#8217;s blog, where all allocations to the file being grown are blocked while the new portion of the file is being zero-initialized.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Bottom-line: auto-shrink should *NEVER* be turned on&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019m in the middle of a flight from Washington D.C. to Zurich on the way to Barcelona for TechEd IT Forum and I can\u2019t sleep \u2013 Kimberly\u2019s out like a light so what else is there to do except write another blog post? :-)OK &#8211; actually posting this from Barcelona on Tuesday before our first [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,34,38,47,66,81],"tags":[],"class_list":["post-1130","post","type-post","status-publish","format-standard","hentry","category-database-maintenance","category-dbcc","category-example-scripts","category-indexes-from-every-angle","category-performance-tuning","category-shrink"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Auto-shrink \u2013 turn it OFF! - Paul S. Randal<\/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\/paul\/auto-shrink-turn-it-off\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Auto-shrink \u2013 turn it OFF! - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"I\u2019m in the middle of a flight from Washington D.C. to Zurich on the way to Barcelona for TechEd IT Forum and I can\u2019t sleep \u2013 Kimberly\u2019s out like a light so what else is there to do except write another blog post? :-)OK &#8211; actually posting this from Barcelona on Tuesday before our first [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2007-11-13T04:28:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-03T15:22:01+00:00\" \/>\n<meta name=\"author\" content=\"Paul Randal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paul Randal\" \/>\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\/paul\/auto-shrink-turn-it-off\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/\",\"name\":\"Auto-shrink \u2013 turn it OFF! - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2007-11-13T04:28:41+00:00\",\"dateModified\":\"2017-06-03T15:22:01+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Auto-shrink \u2013 turn it OFF!\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\",\"name\":\"Paul S. Randal\",\"description\":\"In Recovery...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/?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\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\",\"name\":\"Paul Randal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"caption\":\"Paul Randal\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/paul\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Auto-shrink \u2013 turn it OFF! - Paul S. Randal","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\/paul\/auto-shrink-turn-it-off\/","og_locale":"en_US","og_type":"article","og_title":"Auto-shrink \u2013 turn it OFF! - Paul S. Randal","og_description":"I\u2019m in the middle of a flight from Washington D.C. to Zurich on the way to Barcelona for TechEd IT Forum and I can\u2019t sleep \u2013 Kimberly\u2019s out like a light so what else is there to do except write another blog post? :-)OK &#8211; actually posting this from Barcelona on Tuesday before our first [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/","og_site_name":"Paul S. Randal","article_published_time":"2007-11-13T04:28:41+00:00","article_modified_time":"2017-06-03T15:22:01+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/","name":"Auto-shrink \u2013 turn it OFF! - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2007-11-13T04:28:41+00:00","dateModified":"2017-06-03T15:22:01+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/auto-shrink-turn-it-off\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"Auto-shrink \u2013 turn it OFF!"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/","name":"Paul S. Randal","description":"In Recovery...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/paul\/?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\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce","name":"Paul Randal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","caption":"Paul Randal"},"sameAs":["http:\/\/3.209.169.194\/blogs\/paul"],"url":"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/1130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/comments?post=1130"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/1130\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=1130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=1130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=1130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}