As the data in Microsoft SQL Server tables changes their indexes change. Over time these indexes become fragmented. This fragmentation will adversely affect performance. This post provides information to help you understand the detailed mechanics behind fragmentation. It will also help you understand the methods and approaches for performing defragmentation so you can improve your SQL Server’s performance.
The following is a summary of the key topics covered:
- The difference between disk and SQL Server internal and external fragmentation
- How fragmentation affects performance.
- The mechanics behind performance robbing data voids
- The pros and cons of various approaches to managing fragmentation
- How to judge the improvements gained by defragmenting your server.
Q. What is SQL fragmentation? Is it different that disk fragmentation?
A. SQL fragmentation is not physical like disk fragmenation. Not all fragmentation is equal!
Physical disk fragmentation is likely what comes to mind when fragmentation is first discussed. Physical fragmentation is a side effect of how hard drives and Windows work. It is common knowledge that regular disk defragmentation is required to achieve optimal performance from your PC. Windows even includes a basic defragmentation utility.
Physical fragmentation slows down your PC because reading data is interrupted by head seek delay. Windows fits files into free space, often breaking the file into segments stored apart from one another. A hard drive’s head relocates to read each individual segment. As it moves to each segment the head ‘seeks’ – often at a cost of 3-4 times the time it takes to read the segment itself. Physical fragmentation primarily affects desktop or laptop PCs containing one hard drive. The single drive must sequentially gather data – so on a fragmented disk it seeks, reads, seeks, reads – these 4 operations are performed one after another. Defragmented, the operation ends up as seek, read, read. We reduce the total cost of 24ms to 15ms in our simple example.
Figure 1 – Sequential operations with on a typical PC
Shown above, in Figure 1, we see physical fragmentation forcing the drive to read the file as two segments. The seek costs us 18ms, while the read costs 6ms. Considering the average file is made up of hundreds of segments, the seek latency multiplies and becomes quickly evident as your system slows down over time.
Physical defragmentation products such as Windows defrag, Power Defrag™, Page Defrag™ (another Microsoft tool), or the granddaddy of them all, Diskeeper 2007™ work very well when repairing segmented files. Diskeeper’s technology is licensed to Microsoft as the defragmentation tool internal to Windows. In fact, Diskeeper’s latest innovations bring physical defragmentation capabilities to a completely new level. All of these products reorder the data on your disk, consolidating files into fewer segments to minimize “head seeks” – providing faster boot times, quicker file reads, and a more responsive system overall.
However, physical disk fragmentation is not the same as SQL Server defragmentation! SQL Server is different. SQL Servers use advanced storage systems with multiple drives working in tandem, changing the way files are read. Physical fragmentation is something solved with hardware – not with defragmentation scripts or tools.
Figure 2 – Parallel operation found in arrays
The fault-tolerance in database storage overcomes the vast majority of physical disk fragmentations’ impact. Best practices universally prescribe multi-drive storage subsystems for production SQL Servers. Most SQL Servers use multi-drive storage such as RAID arrays, SANs, and NAS devices; there are always multiple drives acting in tandem. Hard disk controllers supporting drive arrays are aware of the alternate seek/read dynamic and tailor communications with the array for maximum I/O.
As a result, files are distributed across many drives inherently becoming segmented. Working in tandem, however, allows one drive to seek while the others read. With the common configuration of 5 drives, a seek delay of 9ms per drive allows 2 drives reading for 3ms with no seek delay impact at all. Data storage drives are generally much faster than workstation drives, so seek times of 4ms and read times of 1.5ms are not unusual.
There are many DBAs who run a traditional physical defragmentation program in tandem with their intelligent drive controller which results in limited improvement. Physically defragmenting a file in an array implicitly leaves the file segmented across the virtual unison of tandem drives. It’s by design. The goal is to gain the most performance while incurring the least overhead – so don’t run physical defrags if they slow the storage by 50% while running, and ultimately improve read speeds 1-2%.
The most important concept to understand is that the controller, physical defragmentation programs, and multi-drive arrays are unaware of what SQL Server is doing with the file data internally. By focusing on SQL Server’s representation of data – how SQL Server has laid out the database itself, how full each page is, and how effectively we’re utilizing available SQL Server resources, we can optimize to the ‘next level’ of SQL Server performance, solidly trumping any benefit to physical defragmentation by orders of magnitude. In a nutshell, SQL Server’s performance can be most improved by focusing on its internals. In fact, once you start focusing on defragmentation at the SQL Server level – whether with manual defragmentation or with the automated defragmentation provided with SQL defrag manager, you may decide that physical defragmentation is no longer needed!
Q. How is SQL Server’s fragmentation affecting my Server?
A. Fragmentation of SQL Server indexes mainly creates wasted space that affects your server performance much more than one might expect.
Fragmentation of your SQL Server’s internal allocations and page structures result in ‘gaps’ or ‘void’ space that is dead weight carried along with valid data. Your backups, storage, I/O channels, buffer memory, cached data, logs, tempdb, CPUs and query plans are impacted by these unnecessary voids. SQL’s fragmentation continually eats away at these resources with nearly every update, delete, insert, and table/index change. If ignored, fragmentation can be the proverbial ‘death by a thousand cuts’ to a server’s performance and scalability.
Figure 3 – How areas are affected by fragmentation (Click to enlarge)
Shown above is a detailed diagram of how SQL Server fragmentation can affect your SQL Server performance and overview of the affected areas. As you identify how the fragmentation affects your server, you’ll see that fragmentation effects are cumulative and nearly impossible to predict. SQL defrag manager, however, uses sophisticated algorithms to predict and detect SQL Server fragmentation “hot spots” and to defragment indexes on a continuous basis.
Q. What creates the voids and other adverse effects and how do I get a handle on them?
A. Typical, day to day activity causes SQL Servers to fragment over time. Changes to your data – inserts, updates, deletes, and even changing varchar values contribute to fragmentation. The full list of actions that cause fragmentation is long and the rate of fragmentation varies across different indexes and tables. Sometimes there is a pattern resulting from seasonal or annual peaks (e.g., when financials run). But more often than not, it is difficult to find, predict and proactively manage fragmentation manually.
Let’s dive into the details of where these voids sit, how they are created, and how they propagate throughout your server:
Figure 4 – SQL Server’s page structure
SQL Server stores all data, objects, and internal structures in 8192 byte data pages shown in Figure 4. These pages are known only to SQL Server and may be stored in one or more physical files on disk. Data gets a maximum of 8096 bytes per page – the rest of the page contains the page header and row locations. When creating a table or index, SQL Server pages fill according to the fill factor you specify (or the closest approximation.)
Over time, insert, deletes, and modifications (such as widening the value in varchar fields) fill the page and ultimately overflow the page creating a ‘page split’. Splitting divides the full page evenly, putting half of its data on a newly allocated page, and may negate any fill factors you designate. For example, if you designate a fill factor of 80%, over time, due to splitting , your pages may reach a fill factor of 50% or less.
The more that heavy, spiked, or continuous changes occur on a table, the faster and further it and its indexes drift. Since the indexes are based on variants of data in the table, they have their own unique drift profile. The net result of drifts is waste – lots of it – waste of your disk, I/O channels, server’s caches and buffers, and CPU utilization. The waste may also skew your query plans.
The void/waste space is known as “internal fragmentation.” Internal fragmentation lowers page density and as a result our server resources trickle slowly away now being increasingly consumed by empty space. SQL does try to fill the voids in split pages – however there is rarely the Tetris™-like fit necessary to reach optimal population post split. The common practice of using an identity column as your clustered index, forces inserts into new pages at the bottom of the table, preventing recovery of the voided space.
Figure 5 – Page splitting due to fragmentation
The space used by actual data is reflected in a metric called “page density.” The denser a page, the more data vs. void it contains. A page density of 100% would mean the data page is completely full. Even if the pages had no void, Figure 3 illustrates how the split has introduced other inefficiency in contiguously accessing the pages after the split. Interestingly this parallels physical fragmentation – although it is a completely isolated variant in SQL Server’s management of data vs. the way files are segmented on disk. This type of fragmentation is called ‘external fragmentation’.
The vast majority of the time, the void grows, instead of filling up neatly. When void space becomes too much (your page density becomes too low), SQL Server will discard the index due to excessive overhead. At this point, fragmentation becomes very evident as very few systems will tolerate discarding indexes in favor of table scans.
For example, shown in Figure 5, the four pages require four logical reads. Defragmentation would condense the data by reorganizing it into two pages and two reads. A 42% reduction in void space is gained by defragmenting the data. This concretely reflects in improved performance and increased scalability. In the example, two times the data could fit in the defragmented space vs. the fragmented space. By reclaiming the voids, we return capacity to our server.
Figure 6 – Comparing fragmented and defragmented size
While it may seem trivial on such a small scale, when your average page density is low, you are wasting disk space, incurring more physical I/O, increased logical reads, wasting precious server memory while computing and comparing data unnecessarily. Further, if you are fortunate enough to have an intelligent I/O controller, you are also wasting the benefit of its optimization strategies. It becomes obvious that this process of splitting, voids, progressive order, and rates of decay requires non-stop attention to insure the server is running with as much free resource as it can.
Q. Ok, so clearly the maintenance benefits are real, and we want to obtain them, now what?
A. There are two approaches most commonly used for fragmentation today and they have disadvantages
If, for a moment, we ignore SQL defrag manager, there are two existing methods for managing SQL Server fragmentation. Neither is ideal, or gives you the information you need to stay informed and on top of the fragmentation challenge. Both leave you completely blind — you won’t know if they helped, hurt, stepped on, or blocked your busiest table.
First method: plain old damage control. The server performance degrades slowly and is ignored. All of a sudden, a spot in the database reaches critical mass, performance craters, and is eventually addressed. This is how the majority of DBAs are introduced to the fragmentation problem. They just fix it and wait for the next hotspot or for SQL Server performance to run down again and again. Unfortunately, you will never know when your server is going to act up or how severe the impact will be. Furthermore, there may be cascade effects caused by inadvertent query plan disruption due to fragmentation.
Second: Run a blind maintenance script. The best all-purpose script I’ve found is Omar Kloeten’s Index Defragmentation Script. These scripts are often quite complex with unpredictable results. They usually work, but may often cause after effects such as blocking or locking and can generate considerable overhead. You have no idea how long it will take the script to find every index – and it must query each one, every time. The script does not track performance benefits, does not track how defragmentation varies each time, and offers no notification of potential problems.
All-purpose SQL Server defragmentation scripts:
- Request information that can cause long blocks or deadlocks.
- Rarely have internal logic to know when to defragment– instead they just steamroll your servers every day whether they need it or not (perhaps many times a day.)
- If you make a minor change to a defragmentation script, you will be required to re-deploy to the new script to all of the servers in your enterprise.
- Should be tailored to each database – but to do this would require near constant “hand-tuning.” A very time-consuming and practically impossible process.
- Aren’t able to report when the script was run, what performance enhancements were gained, or how many resources they’ve reclaimed on your server since you started running them.
A new third option: Idera SQL defrag manager. SQL defrag manager offers a totally new way to identify, optimize, manage and automate SQL Server defragmentation. It is designed specifically to overcome the compromises DBAs have to make regarding the important task of fragmentation maintenance.
Consider this: If you are able to eliminate void space, every page of void reclaimed is money back in your corporation’s pocket. Those reclaimed resources are regained server capacity that had been lost unnecessarily. SQL defrag manager will reclaim these resources and track the total improvement on every object in your enterprise daily or over a year. You can even produce an annual report showing how much money has been saved through the use of defragmentation technology – and we guarantee that it will be impressive!
SQL defrag manager not only tracks the improvement achieved on each object, it maintains dozens of statistics on each table and index. This information guides SQL defrag manager to determine how often it should check for fragmentation, and if you wish, the method it will use to correct the fragmentation. SQL defrag manager eliminates defragmentation overhead and risk on your servers – there is no agent required on any managed server. There is no job scheduled or script deployed. SQL defrag manager simply runs as a service, quietly in the background with no affect to your production servers.
Unlike scripts, SQL defrag manager’s fragmentation detection routines are non-blocking. Defragmentation is also non-blocking, given the DBA has not chosen to rebuild the fragmented object. Rebuilt objects are often not needed. SQL defrag was invented by two DBAs frustrated with the scripts and the handholding their 99.999% available enterprises required. SQL defrag manager will shed light on the fragmentation levels across your entire SQL Server environment — allowing you to quickly detect and manage fragmentation with ease. It will also give you assurance that defragmentation is being handled in exactly the way it should be for that particular database – no more guessing!
Fragmentation affects performance through the introduction of wasted page space which erodes the resources of a server. Through proactive use of scripts via maintenance jobs or tools like SQL defrag manager or SQL Sentry’s Fragmentaion Mananger can considerably improve performance where fragmentation exists.
I recommend small shops with downtime windows leverage a script which defragments indexes based on levels of fragmentation. If you are enterprise-class running 24×7 the features available in the tools allow you to gate defragmentation based on resources available, which justifies their cost.
Keeping track of density before and after defragmentation helps illustrate buyback you gain. When translated to dollars one finds a compelling arguement for automated solutions. Below is the report I created for SQL defrag: