What is Fragmentation
Logical/Physical Disk Fragmentation
- Placing database file in the same disk where other files( like OS files and other application files)are kept.
- Frequent growth of the database file in smaller chunks.
- Keep the database files in a separate disk isolated from other application files and log files.
- While creating new database,estimate the size of database file and allocate enough space to avoid the frequent growth of data files.
- Specify the database growth option to allocate larger chunks rather than small chunks frequently.
Index Level Fragmentation
Internal Fragmentation
Let us assume that we are deleting the half of the entries randomly of this table which reduce the total number of entries in this index to 400.Now the pages will look like as given in Fig 2 with total of 40600 bytes free space across 10 pages. If you calculate the the average fullness as Total data size*100/Total page size = 4000*100/80600= 49.62% . It clearly says that, half of the spaces are empty and the index has internal fragmentation.
Fig 2 |
How Internal Fragmentation will affect the performance of the SQL server ?
- Internal Fragmentation will increase the I/O. When you run queries that scan part or complete table/index, if you have internal fragmentation on that table/index, it causes additional page reads. In our example, the entire data can be stored in 5 pages. When the query needs to do index scan it has to read 10 pages instead of 5 pages. Which means 50% more I/O.
- Internal Fragmentation reduce the efficiency of buffer cache.When indexes has internal fragmentation, it need more space to fit in the buffer.In our case this single index will use 5 additional pages to fit into the buffer which should have used to store other index pages. This will reduce the cache hit ratio. In turn it will increase the physical I/O. It also increase the logical reads.
- This also increase the size of the database file. It need more disk space to store the additional pages and reduce the performance of Backup and Restore.
External Fragmentation
Fig 3 |
Let us see what will happen if we insert the value 4 to the underlying table in the Fig 4.
Fig 4 |
While inserting the value 4 into the table it has to place in the Page 1 between value 3 and 5 but unfortunately Page 1 does not have any free space to occupy one more record. The only option is perform a page split by dividing the Page 1 evenly by leaving half of the data in Page 1 and moving half of the data to new page (Page 4). From Fig 4 we can understand that the logical order of the Page 4 is not matching with the physical order. External Fragmentation can happen due to various reasons:
- While allocating pages for new table , SQL server allocate pages from mixed extend till it reaches the 8 pages. There is possibility of having the first 8 pages from 8 different extents.
- When all records are deleted from a page, the page will be de-allocated from the index(The de-allocation of pages will not happen immediately) which create gap and increase the fragmentation.
- Once object reached 8 pages size, SQL server will start allocating uniform extent to the objects.While allocation uniform extent to an index, next sequential extent to the current extent might be already allocated to other objects/indexes.
How External Fragmentation will affect the performance of the SQL server ?
While reading individual rows, external fragmentation will not affect the performance as it directly go to the page and fetch the data.Unordered scans also will not affected by the external fragmentation as it use the IAM pages to find which extents need to be fetched. In the case of ordered index scan ,external fragmentation might become a degrading factor for performance. The degradation of the performance is because the disk drive's heads have to jump around on the physical disk, rather than performing just contiguous read operations.Also note that external fragmentation will not affect the performance once the pages are loaded into the buffer pool.
I will explain how to detect/measure the fragmentation in the next post.
If you liked this post, do like my page on FaceBook at http://www.facebook.com/practicalSqlDba