BlackWaspTM
SQL Server
SQL 2005+

SQL Server Indexes Part 1

by Richard Carr, published at http://www.blackwasp.co.uk/SqlIndexes.aspx

The fifteenth part of the SQL Server Programming Fundamentals tutorial describes indexes. Indexes provide a lookup facility for a table, allowing rows to be found more quickly and without the need to scan the data in every row when querying a table.

What is an Index?

In this article I will describe the purpose of indexes in a SQL Server database and some of the types of index that are supported by SQL Server 2005. Indexes are a complex topic so we will not look at any examples in this instalment, this will be saved until the next part of the tutorial.

Table Scans and Index Lookups

When you perform a query, SQL Server can identify the rows that should be returned using one of two methods. The most basic method is called a table scan. As the name suggests, when a table scan is performed, the querying engine scans through every row in the tables that are included in the query. When a row matches the query's WHERE clause, the relevant information is added to the results that will be returned. This process is generally only acceptable for small tables or those where all of the rows are usually returned unfiltered. In almost all other scenarios, reading every table row is very inefficient.

The second method that a query can use to find information is an index lookup. This type of query uses the contents of an index that has previously been defined for a table. An index on a database table performs a similar function to an index in a book. Rather than reading through the entire book to find the word or phrase that you are looking for, you can examine the book's index to find all of the important references to that item. A database index contains the contents of one or more columns in the table. If the query filters its results based only upon columns within the index, the correct database rows can be found much more quickly.

Indexes are stored as sorted, balanced tree structures, also known as b-trees. This type of tree is very fast to access. Several of these index structures can be added to a single table to improve the performance of queries that use that table. When a query is executed, SQL Server's query optimiser determines which of the indexes are suitable for the query and which of these will provide the best performance. Once selected, the b-tree is searched to find the first entry where the data that matches the query. The initial search is fast because the data in the index is sorted. Furthermore, the sorting minimises the range of index entries that must be scanned.

Index lookups are almost always quicker and much more efficient than table scans when retrieving information. However, this improvement in reading speed has an associated impact upon the performance when creating and updating information. In addition to changing the data in the table, each index must also be modified. These two factors should be considered together when designing your tables. If a table is often updated and seldom read, there may not be an overall benefit in adding multiple indexes. However, when certain columns are queried often, such as with columns included in foreign keys, it is often beneficial to index them.

NB: Foreign keys are not indexed by default in SQL Server so these are often ideal candidates for indexing.

Index Types

SQL Server 2005 permits the creation of several types of index. In this article, we will examine four of the more common types. These are:

  • Clustered Indexes
  • Non-Clustered Indexes
  • Unique Indexes
  • Indexes with Included Columns
5 November 2008