What is Fragmentation:
- Fragmentation may be defined as a state when data is scattered and non
contiguous. Fragmentation can be divided into two broad categories, which are
following.
- Physical fragmentation of data files on Disk
- Index Level fragmentation
Reason for Physical
fragmentation: -
- Physical fragmentation happens in those scenarios where database files are kept in the same disk space where other application’s files are kept.
- When database file growth is frequent and in smaller extent.
How we can avoid
physical fragmentation: - To minimize the chances of physical fragmentation
we can pay attention on below points.
- Keeping database files on separate disk from the other applications and files.
- Choosing appropriate growth rate for databases while creating the database.
- Internal Fragmentation
- External Fragmentation
Let’s try to
visualize internal fragmentation by taking an example.
I have taken a clustered table as an example, where there
are three Indexes pages and each index page is containing 100 keys values. At
the leaf level we have 3 data pages which are fully filled by 100 records per
page. Now, if we delete 50 records starting from (3 to 52) from the first page,
(104 to 153) records from the second page and (204 to 253) records from the
third page respectively. Earlier we had 300 records on three pages, now we have
150 records on 3 pages those can be stored on only two data pages. Due to
deletion of the records, lot of unused space has been created. Due to
fragmentation, same amount of I/O cost will be occurred even we have lesser no of
records on the data pages but SQL optimizer has to read all 3 data pages in
case of reading all the records.
External
Fragmentation:-When physical storage of data pages and extents are
non-contiguous on the disk then it is called External fragmentation. In other
words, if a table has been allocated space on non-contiguous extents then it is
called External Fragmentation. Or
when logical order of the pages doesn’t match with the physical order of the
pages, these pages are called out of order pages.
Let’s take an example to understand, what the out of order
pages are. In the below screen shot, I have shown three pages where first two
pages are completely full and third page is not completely full. In below image
physical ordering of the pages is same with logical ordering of the pages.
Page Split Example: -
Now we will insert a record value “5”, that will go to the first page and
since the first page is already full, hence page split event will occur. As a
result of page split half of the records will reside on the first page and half
of the records will be moved to a new page (page 4). We can see in the below
image that logical ordering of the pages are not similar to the physical
ordering of the pages.
Impact on
performance: - If we fetch individual records, fragmentation has no impact
on those cases, as it goes directly to the page containing the matching records
and fetch the data, External fragmentation will impact the performance of the
query where we are going to read records in sequential order, In those cases
disk drive head has to jump back and forth order since the pages are not in
contiguous order.
In my next post we will learn how we can find fragmentation details using SQL scripts in different versions of SQL servers and how we can do de-fragmentation to improve performance.
No comments:
Post a Comment