Whenever you insert, update or delete data on a table with an associated index, that index will fragment slightly. Over time, this fragmentation gets worse and worse and eventually you have a counterproductive index on your table. If the indexes are fragmented then not only will the query response be very slow, but the data storage will also require more disk space. DBAs should be monitoring the performance of indexes on the servers they maintain, and defrag them when necessary to optimise database performance. This monitoring task can also be automated but that’s a talk for another time.
We’ll be following these steps in the code, I’ll go through each item one at a time and I’ll make all of this available on my blog and the aucklandsql website, details at the end. What we’re going to do is create a new database and table that we can play around with, load some data into the table from AdventureWorks2008 and then we’ll add an index. We will then fragment the index and will try two options to fix that index, Reorganize and Rebuild Index. By the end of the presentation we will be able to see which option of defragging is more effective.
Rebuild Index will defragment better than Reorganize, however it is not always practical to rebuild indexes. MSDN has these guidelines in it’s page about defragging indexes.Reorganize an index when the index is not heavily fragmented. If the index is heavily fragmented, you will achieve better results by rebuilding the index.Rebuilding an index can be executed online or offline. Reorganizing an index is always executed online. To achieve availability similar to the reorganize option, you should rebuild indexes online.
SQL Defrag Manager $1000+ per license.Useful script on BOL that checks all indexes on a database and defrags if above a declared threshold – easily adaptable to use ALTER INDEX REORGANIZE or REBUILD instead of DBCC INDEXDEFRAG . Link to script available at the end – though you may want to write your own from scratch if you have an aversion to cursors.Another thing to mention is that dropping/recreating indexes can be used to move existing tables from one filegroup to another without needing to rebuild scheme / move data.
When creating a database, assign the largest value possible to your data files.If using automatic growth on your data files, specify a maximum growth size.If you have a lot of files in the same filegroup or disk partition, do not let them grow automatically, or consider splitting them across several partitions.Set up an index maintenance schedule so that the indexes are regularly evaluated (this can be automated)
Defragging Indexes For Beginners<br />
Why we need to defrag indexes<br />Fragmented indexes are ineffective<br />Ineffective indexes are slooooooow<br />
Over to the code….<br />We are going to: <br />Create a database<br />Create a table <br />Load some data<br />Add an index<br />Fragment the index<br />Fix the index<br />
Which option is best?<br />> 5% and < = 30% fragmentation<br />ALTER INDEX REORGANIZE<br />> 30% fragmentation<br />ALTER INDEX REBUILD WITH (ONLINE = ON)<br />
Keep your indexes sweet<br />Perform regular maintenance on your database, include index checks/defrags as part of the maintenance task.<br />A tool such as Idera’s SQL Defrag Manager could be useful, but it is pricey.<br />Write a script and set up a scheduled task to automate your index maintenance.<br />
Conclusion<br />Reorganize Index<br />Peformed online<br />Does not lock for long periods<br />Does not block updates or other queries<br />Best option for fragmentation between 5% and 30%.<br />Rebuild Index<br />Drops and recreates the index<br />Will give the best defragmentation results<br />Can be performed online or offline<br />Best option for fragmentation greater than 30%<br />Maintenance<br />Monitor your index fragmentation<br />Fix fragmented indexes before they become a problem<br />
Resources<br />Click on the Word icon to the right to access the SQL code and notes used in the practical element of this session. <br />Download AdventureWorks2008 DBs for free here: http://tinyurl.com/c6y6a3<br />More info on fill factors over at the MSDN site: http://msdn.microsoft.com/en-us/library/aa933139(SQL.80).aspx<br />Detailed information on Reorganize and Rebuild: http://msdn.microsoft.com/en-us/library/ms189858(SQL.90).aspx<br />Great information on Indexes from Brad McGehee<br />http://tinyurl.com/mfx8h7<br />Maintenance Options:<br />http://www.idera.com/Products/SQL-Server/SQL-defrag-manager/<br />http://msdn.microsoft.com/en-us/library/aa258803(SQL.80).aspx<br />