Home > SQL Server Tips > SQL Server for the 'Reluctant' DBA > Optimizing SQL Server indexes –- even when they're not your indexes
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

SQL SERVER FOR THE 'RELUCTANT' DBA

Optimizing SQL Server indexes –- even when they're not your indexes


Don Jones, Contributor
08.12.2009
Rating: -3.98- (out of 5)


Expert advice on database administration
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


Indexes are critical to how fast SQL Server retrieves data, though at the same time they reduce the speed that SQL Server can write new and changed data to the database.

Properly creating and maintaining indexes will ensure that they work for -- not against -- your SQL Server environment.

Clustered and non-clustered indexes
SQL Server supports two types of indexes: clustered and non-clustered. Every database table has exactly one clustered index, and this index determines the order that data is physically stored.

More on performance tuning and SQL Server indexes

Top SQL Server indexing tips to improve performance

For example, if you create a clustered index on a "name" column, the rows are physically ordered alphabetically by name. However, if a clustered index is not explicitly created, data is stored by the row's ordinal number, which is assigned by the order data is added to the database.

Typically, a clustered index is created on the most commonly-queried column -- often a unique ID number.

A non-clustered index works a bit differently. It is a separate structure that contains a copy of the indexed data as well as pointers back to the rest of the data.

For example, a non-clustered index on a "telephone number" column would create a copy of every number and place them in numeric order. Pointers would then relate each item in the index back to the actual row of data.

When SQL Server needs to look something up, it first finds the telephone number and then follows the pointer back to the actual data.

While this two-step process is slightly slower than using a clustered index, there is no limit to the number of non-clustered indexes you can have.

When indexes go bad
To understand how indexes can go wrong, consider what happens when changes and insertions need to be made to a physical phone book.

When a phone book is published, its pages are 100% full. If information needs to be added or amended, then supplement pages are issued and inserted in the back of the book. In order to scan through the phone book alphabetically, you need to jump between the main book and the supplement pages, which is not very efficient.

In SQL Server, this is known as index fragmentation.

If too many supplement pages are added, SQL Server might decide that the index cannot be used quickly enough to improve query performance. As a result, it will stop using it.

This is where you come in.

You need to maintain these indexes -- even if you are dealing with third-party applications where the vendor has determined what indexes to use.

Maintaining indexes
You can check the fragmentation level of an index, and Microsoft provides details for doing so. Indexes that are between 5% and 30% fragmented should be reorganized, or in other words, defragmented.

For fragmentation levels greater than 30%, you need to rebuild -- a one-step process that drops the existing index and builds a fresh one from scratch. Rebuilding is more labor-intensive than reorganizing and may impact some applications' performance.

With both steps, you can set how full you want each page in the new index to be with the fill factor key option.

For example, a fill factor of 50% would leave each page half-empty. In this scenario, although the index would take up more space on the disk, it would also have more free space for insertions and changes. As a result, the index would last longer.

Keep in mind that the larger the index, the longer it will take SQL Server to process. This is because the server has to read all the pages off of the disk, even if they are mostly empty. Therefore, specifying a fill factor of 1% would be counterproductive in many databases. In general, write-heavy databases should have a lower fill factor, while database that support mostly read traffic can have a higher fill factor.

One last key point to remember: if you rebuild the clustered index, you are physically rearranging the database rows. This means every non-clustered index will also have to be modified because the row pointers used in those indexes will change. This a very labor-intensive operation, and should only be done as part of a scheduled maintenance window.

You can also disable non-clustered indexes during the clustered index rebuild. This prevents SQL Server from attempting to update at every moment and speeds up the rebuilding operation.


SQL SERVER FOR THE RELUCTANT DBA
Part 1: How SQL Server really works
Part 2: Understanding backup and recovery
Part 3: Optimizing indexes
Part 4: SQL language crash course
Part 5: SQL Server security made simple
Part 6: High-availability options and caveats

ABOUT THE AUTHOR:   
Don Jones is a co-founder of Concentrated Technology LLC, the author of more than 30 IT books and a speaker at technical conferences worldwide. Contact him through his website at www.ConcentratedTech.com.

Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
Microsoft SQL Server Performance Monitoring and Tuning
Using traces in SQL Server Profiler
SQL Server Mailbag: CALs, witnesses and unwanted changes
SQL Server Mailbag: Data restoration and DB property management
Working with IntelliSense in SQL Server 2008 Management Studio
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Troubleshooting Distributed Transaction Coordinator errors in SQL Server
Clearing the Windows page file and its effect on server performance
Performance implications of transaction log autogrowth in SQL Server
The short course on how SQL Server really works
Determining the source of full transaction logs in SQL Server

SQL Server Database Modeling and Design
Top tips and tricks for SQL Server database development
Managing the development lifecycle with Visual Studio Team System 2008
A first look at Visual Studio Team System 2008 Database Edition
Testing transaction log autogrowth behavior in SQL Server
Top 10 SQL Server Tips of 2008
Tutorial: SQL Server indexing tips to improve performance
Tutorial: Learn SQL Server basics from A-Z
SQL Server database design disasters: How it all starts
Can you shrink your SQL Server database to death?
Physical data storage in SQL Server 2005 and 2008

SQL Server for the 'Reluctant' DBA
SQL Server high availability: Options and caveats
SQL Server security made simple and sensible
SQL language crash course (just enough to be dangerous)
How to 'do' SQL Server disaster recovery
The short course on how SQL Server really works

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
contiguity  (SearchSQLServer.com)
contiguous  (SearchSQLServer.com)
drilldown  (SearchSQLServer.com)
hashing  (SearchSQLServer.com)
hybrid online analytical processing  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SQL Server Development - .NET, C#, T-SQL, Visual Basic
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts