Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Indexes on Tables An index provides quick access to data in a table, based on the values in specified columns. A table can have more than one.

Similar presentations


Presentation on theme: "Creating Indexes on Tables An index provides quick access to data in a table, based on the values in specified columns. A table can have more than one."— Presentation transcript:

1 Creating Indexes on Tables An index provides quick access to data in a table, based on the values in specified columns. A table can have more than one index. Indexes are transparent to users accessing data from that table. Server automatically decides when to use the indexes created for tables.

2 How indexes work 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 Server from having to search through each row in the table could create the following index, entitled stor_id_ind: create index stor_id_ind on stores (stor_id)

3 How indexes work The stor_id_ind index goes into effect automatically the next time you query the stor_id column in stores table. You can only create or drop indexes from a table, Server decides whether to use the indexes for each query submitted for that table. Server manages an index after it is created and usage of indexes are transparent to users.

4 Index Types Clustered indexes: Clustered indexes force 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. There can be only one clustered index per table Clustered indexes are implemented in the following ways: PRIMARY KEY When you create a PRIMARY KEY constraint, a unique clustered index on the column or columns is automatically created.

5 Index Types Nonclustered indexes: With a nonclustered index, the physical order of the rows is not the same as their indexed order. The leaf level of a nonclustered index contains pointers to rows on data pages. More precisely, each leaf page contains an indexed value and a pointer to the row with that value. Nonclustered indexes can often help you find data more quickly than searching the underlying table You can create multiple nonclustered indexes on a table. When you create a UNIQUE constraint, a unique nonclustered index is created to enforce a UNIQUE constraint by default.

6 Index Properties 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.

7 Index Properties Unique Indexes: A unique index permits no two rows to have the same index value. Multicolumn unique indexes guarantee that each combination of values in the index key is unique. The system checks for duplicate values when the index is created, if data already exists, and checks each time data is added or modified with an insert or update.

8 Index Properties Unique indexes ensure the data integrity of the defined columns. Unique indexes provide additional information helpful to the query optimizer that can produce more efficient execution plans.

9 When to index Use 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 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.

10 When not to index 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

11 Creating indexes The simplest form of the create index command is: create index index_name on table_name (column_name) To create an index on the au_id column of the authors table, the command is: create index au_id_ind on authors(au_id) You must be the owner of a table to create or drop an index. The owner of a table can create or drop an index at any time, whether or not there is data in the table

12 Creating indexes The complete syntax of the create index command is: create [unique] [clustered | nonclustered] index index_name on [[database.]owner.]table_name (column_name [, column_name]...) [with {{fillfactor | max_rows_per_page} = x, ignore_dup_key, sorted_data, [ignore_dup_row | allow_dup_row]}] If you don’t mention any index type, Server use nonclustered index type as default

13 Index Options Fillfactor: Specifies the percent of space on an index page that can be filled when the index is created. A fillfactor under 100% leaves space for inserts into a page without immediately causing page splits. Applies only to a clustered index on a data-only-locked table The fillfactor percentage is used only when an index is created on a table with existing data. max_rows_per_page : Specifies the maximum number of rows allowed per page. ignore_dup_key: If you try to insert a duplicate value into a column that has a unique index, the command is canceled. You can avoid this situation by including the ignore_dup_key option with a unique index.

14 Index Options ignore_dup_row and allow_dup_row: These options are for creating a nonunique, clustered index. These options are not relevant when creating a nonunique, nonclustered index. A nonunique clustered index allows duplicate keys, but does not allow duplicate rows unless you specify allow_dup_row. sorted_data : This option of create index speeds creation of an index when the data in the table is already in sorted order. ignore_dup_key: If you try to insert a duplicate value into a column that has a unique index, the command is canceled. You can avoid this situation by including the ignore_dup_key option with a unique index.

15 Dropping indexes The drop index command removes an index from the database. Its syntax is: drop index table_name.index_name [, table_name.index_name]... To drop the index phone_ind in the friends_etc table, the command is: drop index friends_etc.phone_ind To see the indexes that exist on a table, you can use sp_helpindex. sp_helpindex friends_etc


Download ppt "Creating Indexes on Tables An index provides quick access to data in a table, based on the values in specified columns. A table can have more than one."

Similar presentations


Ads by Google