Best Practices for Creating Indexes

From SQLServerPedia

Jump to: navigation, search

See Also: Main_Page - Database Administration - Database Design - Indexing

SQL Server does not come with hard and fast rules for indexing certain columns and not others. Many opinions exists as to which indexing strategy is best. Perhaps the best advice that can be offered is that there is not a single best way to assure optimal performance through indexes. However, there are some guidelines to help with indexing decisions you need to make.

  • Keep indexes lean. Try to build indexes on one or few columns at most. Wide indexes take longer to scan than narrow indexes.
  • Create the clustered index on every table. However, choose the column(s) for the clustered index judiciously. Try to create the clustered index on the column which is used most frequently for retrieving data.
  • Try to create the clustered index on the column with high selectivity; that is the column that does not have many duplicate values.
  • Try to create the clustered index on the column that will never be updated or will be updated infrequently. Every time the clustered index key is updated, SQL Server must maintain not just the clustered index but also the non-clustered indexes since non-clustered indexes contain a pointer to the clustered index. This is yet another reason why you shouldn't create the clustered index on multiple columns.
  • By default, SQL Server creates the clustered index on the PRIMARY KEY column(s) unless the clustered index is created prior to creating the primary key. It is often beneficial to have the clustered index on the primary key, but sometimes you're better off saving the clustered index for other column(s). Feel free to override the default behavior if your testing shows that clustered index on a non-key column will help your queries perform better.
  • SQL Server has to perform some maintenance when index keys are updated, so each additional index could impose a slight performance penalty on the performance of INSERT, UPDATE and DELETE statements. Nevertheless the cost of index maintenance could be far less than the performance improvement you'll reap for your SELECT statements. Therefore you can be fairly liberal with the number of non-clustered indexes.
  • Be sure to eliminate duplicate indexes, that is, multiple indexes created on the same set of columns. Such indexes provide no additional benefit but use up extra disk space and might hinder performance of INSERT, UPDATE and DELETE statements.
  • Check the default fill factor level configured at the server level. If you don't specify the FILLFACTOR option with CREATE INDEX statement, every new index will be created using the default fill factor. This may or may not be what you intend.
  • Peruse the built-in tools for analyzing and recommending indexes but do not rely on them solely. Index tuning wizard (available with SQL Server 2000) is a good tool for starting the index analysis. However, the wizard only recommends indexes that are relevant for a particular workload you supply. The wizard cannot reasonably estimate how frequently any particular query will be executed and certainly doesn't know which queries are most important to your users. SQL Server 2005 has a more mature tool called Database Engine Tuning Advisor (DETA). You can use DETA for analyzing performance of SQL Server 2000 or 2005 instances. DETA allows you to choose from a number of tuning options and provides excellent recommendations for improving performance by creating indexes and statistics. Even so the DBA still has to decide which queries are most important to optimize.

More SQLServerPedia Articles on Indexes

How SQL Server Indexes Work

Types of Indexes in SQL Server

  • Included Columns - starting with SQL Server 2005, you can add fields to indexes with a smaller performance hit.
  • Indexed Views - how to build indexes on top of views for more speed.
  • XML Indexes Overview - how to index XML documents and fragments.
  • XML Index Rules - some additional considerations and requirements for XML indexes.

Best Practices on How to Design Database Indexes

  • Indexing Strategies - choosing between clustered/nonclustered, long/short, selectivity, and more.
  • Best Practices for Creating Indexes - tips to start with when designing a new index strategy.
  • Choosing Index Keys - how to pick which fields to use for a primary key.
  • Index Selectivity and Column Order - how to pick which columns to put first, and how it affects different kinds of queries.
  • Wide Index Performance - does adding more fields to an index cause it to run slower?
  • Tuning Indexes - including articles on the Index Tuning Wizard and Database Tuning Advisor.

Maintaining Indexes for Top Performance

Indexes need regular maintenance in order to perform well.