Subscribe to
Posts
Comments

It has been a long while since last time I wrote about SQL Server. One of the idea in my mind when I set up this blog was to write about the tricks that I know

or want to know as an information technology professional. When I am writing I can refresh my memory on something that I have already known and find out the answer on something that I want to know. When I write about something and share it with the public I was compelled to provide the information as accurate as possible. So to me writing technical article is also a very good learn experience.

As you know I am looking for jobs recently. I have been to many interviews and I have learned a lot from the interviews. Whenever I was not sure about the answer of an interview question I would remember the question and did some research on it. It is in that way I pick up the knowledge I needed as an IT professional all these yeas. Whenever there is uncertainties and questions put them down on a note and look it up when time is available. If you can be persistent like this you will be an expert someday.

Anyway one of the interview question I didn’t handle very well is what is fragmentation, how fragmentation occur and how to defragment a able being used 24/7. In fact I feel one of my interview was failing on the last part of the question. So I did some research and the following is the answer in my own language.

Fragmentation regards to data scattering around on the disk instead of being stored contiguously on the disk. When SQL server reads data with more fragmentation the disk head will move more frequently from sector to sector. Since disk head movement is time consuming operation fragmentation will degrade read performance.

There are different type of fragmentation that will affect SQL server read performance. One is file fragmentation. That has nothing to do with SQL server. It has to do with the OS. If OS can not find a contiguous disk space for the data file you want to create then it will break down the data file into several pieces on the disk. Logically it is still a single contiguous data file to SQL server. This kind of fragmentation can be defragmented using the disk defragmenter that comes with window server. However SQL server has to be stopped in order to run the disk defragmenter on SQL server data file.

Another type is SQL server fragmentation. It occurs when data is being inserted, updated or deleted. When a data page (SQL server 2000 uses 8K bytes as one data page) has no room for new data inserted or updated it will do a page split. One data page splits into two so that new data can be fit in and at the same time leaving gaps in the new data pages. The page split operation can also make the originally contiguous data pages now became not contiguous. When data is deleted from the data page it also leave gap within the data page. Eliminating this fragmentation can also improve read performance.

Now the last part. How to defragment a table being used 24/7? A simple answer is to use DBCC INDEXDEFRAG command. It is basically an online operation that will not block update, insert or delete. However on indexes with excessive fragmentation this command will be slow compare to other ways of degramentation. If there is a maintenance window, DBCC DBREINDEX can be used or you can simple drop and recreate the index. Another thing is that DBCC SHOWCONTIG command can be used to show how much fragmentation a table or index has.


Related Posts:

  • New Answer on How to Defragment a Table Being Used 24/7
  • How to Prevent Innodb Data File Keep Growing
  • MySQL vs SQL Server in Data File Management
  • Join to Parametrized Table Function - Cross Apply and Outer Apply in SQL Server 2005
  • Grant Truncate?


  • 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading ... Loading ...

    RSS feed | Trackback URI

    3 Comments »

    2008-06-22 22:29:49
    MyAvatars 0.2

    […] my previous post “What is Fragmentation and How to Defragment a Table Being Used Consistently?” I told you that DBCC INDEXDEFRAG command was the solution to defragment in a 24/7 […]

     
    Comment by tong
    2008-09-15 11:41:00
    MyAvatars 0.2

    how often should we run DBCC degragmention?

     
    Comment by Andrew Chen
    2008-09-16 21:55:46
    MyAvatars 0.2

    It really depends on the type application running against the database and your environment. If your application is mostly doing single record update and look up then you probably don’t want to run defragmention at all because it will not boost performance. If the application is a reporting type of application like data warehouse then defragmention will help performance. How often to run depends on the size your database, how often your data get changed, how critical your application is and the availability of maintenance windows. I think weekly schedule will work for most business.

     
    Name (required)
    E-mail (required - never shown publicly)
    URI
    Subscribe to comments via email
    Your Comment (smaller size | larger size)
    You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.