From the Blogosphere
SQL Server Index Fragmentation In-Depth.
Fragmentation is a common term that describes numerous effects that can occur because of data modifications
By: Jordan Sanders
Nov. 20, 2015 09:03 AM
There is no way to avoid index fragmentation in any SQL Server environment. It does not depend on your SQL Server version or I/O subsystem you have, or your hardware. In this article, we will drill down into SQL Server index fragmentation issue. We will figure out why index fragmentation is a problem and how it affect on overall performance, discuss how to detect and avoid it.
Let's insert a new row into the index and see what happens. SQL Server inserts a new row on the data page in case there is enough free space on that page, otherwise the following happens:
As a consequence, we have two types of index fragmentation:
Logical fragmentation (also called external fragmentation or extent fragmentation) - the logical order of the pages does not correspond their physical order. As a result, SQL Server increases the number of physical (random) reads from the hard drive, making the read-ahead mechanism less efficient. This directly impacts to the query execution time, because random reading from the hard drive is far less efficient comparing to sequential reading.
Internal fragmentation - the data pages in the index contain free space. This lead to an increase in the number of logical reads during the query execution, because the index utilizes more data pages to store data.
avg_page_space_used_in_percent shows the average percentage of the data storage space used on the page. This value allows you to see the internal index fragmentation.
avg_fragmentation_in_percent provides you with information about external index fragmentation. For tables with clustered indexes, it indicates the percent of out-of-order pages when the next physical page allocated in the index is different from the page referenced by the next-page pointer of the current page. For heap tables, it indicates the percent of out-of-order extents, when extents are not residing continuously in data files.
fragment_count indicates how many continuous data fragments the index has. Every fragment constitutes the group of extents adjacent to each other. Adjacent data increases the chances that SQL Server will use sequential I/O and Read-Ahead while accessing the data.
Avoiding Index Fragmentation
Utilizing Index Fill Factor
Therefore, you need to figure out how much space you want to leave. Amount of space to use is 100% minus fill factor value (e.g., fill factor of 70 means 30% free space).
SQL Server only uses the fill factor when an index is created, rebuild, or reorganized. The index fill factor is not used during regular inserts, updates and deletes. In fact, that does not make any sense, because the whole point is to allow inserts and updated to happen and to add more records without filling up the page.
It is possible to set the instance fill factor using sp_configure, but not recommended. The reason is when you set the fill factor for the entire instance, there are probably some indexes that do not need fill factor. If you find that you've got fragmentation problems on non-leaf level of the index (rare), you can use the PAD_INDEX option. It takes fill factor that has been specified and puts it up into the non-leaf level.
Setting a Fill Factor
An obvious question arises: "What fill factor do I use?" Well, actually, there is no any magic number. You just need to pick an initial fill factor and implement it. Put it into production and then monitor how quickly fragmentation occurs. Then choose to do one or both of the following: increase/decrease the fill factor or change the frequency of index maintenance.
Removing Index Fragmentation
There is no correct answer in regards: "What to use REBUILD or REORGANIZE?" One of Microsoft Books Online provides the following guidance:
0 to 5-10% - do nothing
5-10% to 30% - do REORGANIZE
30% to 100% - do REBUILD
Eventually, you have several basic options to remove index fragmentation:
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
SYS-CON Featured Whitepapers
Most Read This Week