{"id":702,"date":"2004-08-23T05:54:31","date_gmt":"2004-08-23T05:54:31","guid":{"rendered":"\/blogs\/kimberly\/post\/MSDN-Webcast-QA-Index-Defrag-Best-Practices-Fragmentation-Questions-Part-II.aspx"},"modified":"2013-01-02T07:07:14","modified_gmt":"2013-01-02T15:07:14","slug":"msdn-webcast-qa-index-defrag-best-practices-fragmentation-questions-part-ii","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/msdn-webcast-qa-index-defrag-best-practices-fragmentation-questions-part-ii\/","title":{"rendered":"MSDN Webcast Q&#038;A: Index Defrag Best Practices &#8211; Fragmentation Questions, Part II"},"content":{"rendered":"<p><P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: Arial; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA\"><B style=\"mso-bidi-font-weight: normal\"><FONT face=Arial><FONT color=#000000><FONT size=3>Q: Can I automate DBCC DBREINDEX to rebuild all tables on all databases on a server?<?xml:namespace prefix = o ns = \"urn:schemas-microsoft-com:office:office\" \/><o:p><\/o:p><\/FONT><\/FONT><\/FONT><\/B><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><FONT color=#000000><FONT size=3><FONT face=Arial>Actually, yes and no&#8230; With stored procedures you&#8217;ll have a hard time changing database context without having to use dynamic string execution&#8230;. And so this can add a bit of frustration. BUT &#8211; it&#8217;s your lucky day (and really, it&#8217;s mainly because I&#8217;m on a ferry going to Victoria Canada right now and the Washington State Ferry system doesn&#8217;t currently have wireless internet access&#8230; and well, I have a few spare minutes <\/FONT><SPAN style=\"FONT-FAMILY: Wingdings; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-char-type: symbol; mso-symbol-font-family: Wingdings\"><SPAN style=\"mso-char-type: symbol; mso-symbol-font-family: Wingdings\">J<\/SPAN><\/SPAN><FONT face=Arial>).<\/FONT><\/FONT><\/FONT><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><o:p><FONT face=Arial color=#000000 size=3>&nbsp;<\/FONT><\/o:p><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><FONT face=Arial color=#000000 size=3>Anyway, I&#8217;ve put together a stored procedure which allows you to enter a command that will be processed in all databases. It&#8217;s similar to the undocumented sp_MSforeachdb but it&#8217;s got a simpler format. The basic idea is to create a cursor which has the list of all of the databases in it and then while walking the cursor build a string which includes a change in database context switch followed by the command to execute.<\/FONT><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><o:p><FONT face=Arial color=#000000 size=3>&nbsp;<\/FONT><\/o:p><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><FONT color=#000000><FONT size=3><SPAN style=\"FONT-FAMILY: 'Lucida Sans Unicode'\"><SPAN style=\"mso-tab-count: 2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/SPAN><\/SPAN><SPAN lang=PT-BR style=\"FONT-FAMILY: 'Lucida Sans Unicode'; mso-ansi-language: PT-BR\">SELECT @ExecString = N&#8217;USE &#8216; + QUOTENAME(@DBToProcessName, &#8216;]&#8217;) + N&#8217; &#8216; + @CommandToExecute<o:p><\/o:p><\/SPAN><\/FONT><\/FONT><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><FONT color=#000000><FONT size=3><SPAN lang=PT-BR style=\"FONT-FAMILY: 'Lucida Sans Unicode'; mso-ansi-language: PT-BR\"><SPAN style=\"mso-tab-count: 2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/SPAN><\/SPAN><SPAN style=\"FONT-FAMILY: 'Lucida Sans Unicode'\">SELECT @ExecString AS &#8216;Command to be executed&#8217;<o:p><\/o:p><\/SPAN><\/FONT><\/FONT><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><o:p><FONT face=Arial color=#000000 size=3>&nbsp;<\/FONT><\/o:p><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><FONT face=Arial color=#000000 size=3>Please note that I have NOT done a lot of error handling nor have I done a lot of testing but this works pretty well for what I&#8217;ve used it for and in response to this question it certainly works. It&#8217;s a great start for you to use\/learn from and build upon for your own procedures. Here&#8217;s the script: <SPAN style=\"BACKGROUND: yellow; mso-highlight: yellow\"><a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/content\/binary\/sp_executeindatabase.sql\"><FONT style=\"BACKGROUND-COLOR: #ffffff\" face=Verdana size=2>sp_ExecuteInDatabase.sql (3.76 KB)<\/FONT><\/a><\/SPAN>. And here&#8217;s how you use it:<\/FONT><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><o:p><FONT face=Arial color=#000000 size=3>&nbsp;<\/FONT><\/o:p><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><FONT color=#000000><FONT size=3><SPAN style=\"mso-tab-count: 1\"><FONT face=Arial>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/FONT><\/SPAN><SPAN style=\"FONT-FAMILY: 'Lucida Sans Unicode'\">exec sp_ExecuteInDatabase N&#8217;exec sp_RebuildIndexes&#8217;<\/SPAN><\/FONT><\/FONT><\/P><FONT color=#000000><FONT size=3><SPAN style=\"FONT-FAMILY: 'Lucida Sans Unicode'\"><SPAN style=\"mso-tab-count: 1\"><FONT face=Arial><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><SPAN style=\"FONT-FAMILY: 'Lucida Sans Unicode'\"><o:p>&nbsp;<\/o:p><\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\">or if you want to execute ONLY in one database:<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><o:p>&nbsp;<\/o:p><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/FONT><\/SPAN><SPAN style=\"FONT-FAMILY: 'Lucida Sans Unicode'\">exec sp_ExecuteInDatabase N&#8217;exec sp_RebuildIndexes&#8217;, &#8216;pubs&#8217;<o:p><\/o:p><\/SPAN><\/P><o:p><\/o:p><\/SPAN><\/FONT><\/FONT><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><o:p><FONT face=Arial color=#000000 size=3>&nbsp;<\/FONT><\/o:p><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><FONT face=Arial color=#000000 size=3>Let me know if you find anything interesting and\/or make changes to this one! Have fun. <\/FONT><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><o:p><FONT face=Arial color=#000000 size=3>&nbsp;<\/FONT><\/o:p><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><B style=\"mso-bidi-font-weight: normal\"><FONT color=#000000><FONT size=3><FONT face=Arial>Q: Is there a baseline amount of improvement for INDEXDEFRAG or DBREINDEX to be worthwhile? Please express your answer looking at % change for logical fragmentation and Scan Density.<\/FONT><\/FONT><\/FONT><\/B><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><FONT size=3><FONT face=Arial><FONT color=#000000>Actually, the associated whitepaper does a nice job of correlating table size with defragmentation best practices. I&#8217;ll defer to that BUT I will say that IF you have the maintenance window then there&#8217;s never a negative in doing this &#8211; even for small tables. However, (<I style=\"mso-bidi-font-style: normal\">and as the whitepaper also states<\/I>) the larger and more active the table the more rewarding (in terms of performance as well as disk\/cache savings) a defrag\/rebuild is! Here&#8217;s the link to the whitepaper: <\/FONT><SPAN style=\"COLOR: black; mso-bidi-font-family: Arial\"><a href=\"http:\/\/technet.microsoft.com\/library\/Cc966523\">Microsoft SQL Server 2000 Index Defragmentation Best Practices<\/a>.<o:p><\/o:p><\/SPAN><\/FONT><\/FONT><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in\"><SPAN style=\"COLOR: black; mso-bidi-font-family: Arial\"><o:p><FONT face=Arial size=3>&nbsp;<\/FONT><\/o:p><\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><B style=\"mso-bidi-font-weight: normal\"><FONT color=#000000><FONT size=3><FONT face=Arial>Q: In a *typical* scenario (500 tables, 15 large ones) how often would you recommend to go between rebuilding indexes? Ex. lower the fill factor until you go X days before your scan density is below the threshold.<o:p><\/o:p><\/FONT><\/FONT><\/FONT><\/B><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><FONT size=3><FONT face=Arial><FONT color=#000000>Yep! I think I got this one during the webcast as well BUT &#8211; this is exactly my suggestion! If you can find the stable point where fragmentation only begins to occur when you&#8217;re about to rebuild then that&#8217;s the best choice. In that case you will keep your table the most compact (i.e. at the highest fillfactor) without wasting space ; at the same time you will minimize fragmentation and keep the DML operations fast! Again, the defrag whitepaper has a few additional ideas for you. Here&#8217;s the link to the whitepaper: <\/FONT><SPAN style=\"COLOR: black; mso-bidi-font-family: Arial\"><a href=\"http:\/\/technet.microsoft.com\/library\/Cc966523\">Microsoft SQL Server 2000 Index Defragmentation Best Practices<\/a>.<\/SPAN><\/FONT><\/FONT><\/P><\/SPAN><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q: Can I automate DBCC DBREINDEX to rebuild all tables on all databases on a server? Actually, yes and no&#8230; With stored procedures you&#8217;ll have a hard time changing database context without having to use dynamic string execution&#8230;. And so this can add a bit of frustration. BUT &#8211; it&#8217;s your lucky day (and really, [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,36,58,78],"tags":[],"class_list":["post-702","post","type-post","status-publish","format-standard","hentry","category-events","category-indexes","category-resources","category-tips"],"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/comments?post=702"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/702\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/media?parent=702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/categories?post=702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/tags?post=702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}