We know data in SQL server data are stored on 8kb pages. When we are UPDATE or INSERT data on a page that is already full, SQL Server creates a new page. The information from the original page will be split 50/50 with half being added to the new page, along with the new record. Doing this causes empty space at the end of the page(s). This empty space is known as Internal Fragmentation, it also occurs when we delete data from a page.
This newly created page won’t sit
in sequence with the original page(s) meaning the related data is now stored in
a different area of the disk. This separated data is called External
Fragmentation. External Fragmentation is caused by pages that are out of order.
Check Index Fragmentation
Below query is used to get the
list of index and fragmentation percentage.
SELECT s.NAME + '.' + t.NAME AS table_name, |
avg_page_space_used_in_percent shows the average percentage of the
data storage space used on the page. This value allows us to see the internal
index fragmentation.
avg_fragmentation_in_percent provides us 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.
After the degree of fragmentation
is known, use the following table to determine the best method to correct the
fragmentation.
avg_fragmentation_in_percent
value |
Corrective
statement |
> 5% and < = 30% |
ALTER INDEX REORGANIZE |
> 30% |
ALTER INDEX REBUILD WITH (ONLINE = ON)* |
No comments:
Post a Comment
If you have any doubt, please let me know.