Thursday, October 16, 2008

Indexes in Sql server

Indexes in SQL server are similar to the indexes in Books.
They help SQL server retrieve the data quickly.

There are clustered and nonmclustered indexes.

A clustered indexes is a special type of index that records the way in which records in the tableare physically stored.Therefore , table can have only one clustered index.The leaf nodes of a clustered index contain the data pages.

A nonClustered index is a special type of index in which the logical order of the indexdoes not match the physical stored order of the rows on disk.The leaf node of a non clustered index dows not consist of the data pages .Instead the leaf nodes contain index rows.
The syntax for creating indexes

CREATE INDEX idxModelON Product (Model)

Let's assume that we have the following table,

TABLE Customer
(
First_Name char(50),
Last_Name char(50),
Address char(50),
City char(50),
Country char(25),
Birth_Date date)


and we want to create an index on the column Last_Name, we would type in,

CREATE INDEX IDX_CUSTOMER_LAST_NAMEon CUSTOMER (Last_Name)[/COED]

If we want to create an index on both City and Country, we would type in,

CREATE INDEX IDX_CUSTOMER_LOCATIONon CUSTOMER (City, Country)

No comments: