![]() | ![]() |
Home |
|
|
Transact-SQL User's Guide |
|
| Chapter 11 Creating Indexes on Tables |
|
| How indexes work |
Indexes help Adaptive Server locate data. They speed up data retrieval by pointing to the location of a table column's data on disk. For example, suppose you need to run frequent queries using the identification numbers of stores in the stores table. To prevent Adaptive Server from having to search through each row in the table--which can be time-consuming if the stores table contains millions of rows--you could create the following index, entitled stor_id_ind:
create index stor_id_ind on stores (stor_id)
The stor_id_ind index goes into effect automatically the next time you query the stor_id column in stores. In other words, indexes are transparent to users. SQL includes no syntax for referring to an index in a query. You can only create or drop indexes from a table; Adaptive Server decides whether to use the indexes for each query submitted for that table. As the data in a table changes over time, Adaptive Server may change the table's indexes to reflect those changes. Again, these changes are transparent to users; Adaptive Server handles this task on its own.
Adaptive Server supports the following types of indexes:
Composite indexes - these indexes involve more than one column. Use this type of index when two or more columns are best searched as a unit because of their logical relationship.
Unique indexes - these indexes do not permit any two rows in the specified columns to have the same value. Adaptive Server checks for duplicate values when the index is created (if data already exists) and each time data is added.
Clustered or nonclustered indexes - clustered indexes force Adaptive Server to continually sort and re-sort the rows of a table so that their physical order is always the same as their logical (or indexed) order. You can have only one clustered index per table. Nonclustered indexes do not require the physical order of rows to be the same as their indexed order. Each nonclustered index can provide access to the data in a different sort order.
These types of indexes are described in more detail later in this chapter.
You can create indexes on tables either by using the create index statement (described in this chapter) or by using the unique or primary key integrity constraints of the create table command. However, integrity constraints are limited in the following ways:
You cannot create nonunique indexes.
You cannot use the options provided by the create index command to tailor how indexes work.
You can only drop these indexes as a constraint using the alter table statement.
If your application requires these features, you should create your indexes using create index. Otherwise, the unique or primary key integrity constraints offer a simpler way to define an index for a table. For information about the unique and primary key constraints, see Chapter 7, "Creating Databases and Tables."
Indexes speed the retrieval of data. Putting an index on a column often makes the difference between a quick response to a query and a long wait.
However, building an index takes time and storage space. For example, nonclustered indexes are automatically re-created when a clustered index is rebuilt.
Additionally, inserting, deleting, or updating data in indexed columns takes longer than in unindexed columns. However, this cost is usually outweighed by the extent to which indexes improve retrieval performance.
When to indexUse the following general guidelines:
If you plan to do manual insertions into the IDENTITY column, create a unique index to ensure that the inserts do not assign a value that has already been used.
A column that is often accessed in sorted order, that is, specified in the order by clause, probably should be indexed so that Adaptive Server can take advantage of the indexed order.
Columns that are regularly used in joins should always be indexed, since the system can perform the join faster if the columns are in sorted order.
The column that stores the primary key of the table often has a clustered index, especially if it is frequently joined to columns in other tables. Remember, there can be only one clustered index per table.
A column that is often searched for ranges of values might be a good choice for a clustered index. Once the row with the first value in the range is found, rows with subsequent values are guaranteed to be physically adjacent. A clustered index does not offer as much of an advantage for searches on single values.
In some cases, indexes are not useful:
Columns that are seldom or never referenced in queries do not benefit from indexes, since the system seldom has to search for rows on the basis of values in these columns.
Columns that can have only two or three values, for example, "male" and "female" or "yes" and "no", get no real advantage from indexes.
If the system does have to search an unindexed column, it does so by looking at the rows one by one. The length of time it takes to perform this kind of scan is directly proportional to the number of rows in the table.
|
|