DATABASE

Clustered Indexes in SQL Server 2008

9/4/2010 12:20:26 PM

An index can be defined using one or more columns in the table, the index key, with the following restrictions:

  • You can define an index with up to 16 columns

  • The maximum size of the index key is 900 bytes

The column(s) defined for the clustered index are referred to as the clustering key. A clustered index is special because it causes SQL Server to order the data in the table according to the clustering key. Because a table cannot be sorted more than one way, you can define only one clustered index on a table.

Note

A table without a clustered index is referred to as a heap. As a general rule of thumb, every table should have a clustered index. One of the primary reasons for creating a clustered index on each table is to eliminate forwarding pointers. Forwarding pointers are well beyond the scope of this book. To learn more about forwarding pointers and the detrimental effects on query performance, please refer to Inside SQL Server 2005: Query Tuning and Optimization by Kalen Delaney.


The general syntax for creating a relational index is as follows:

CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name
ON ( column [ ASC | DESC ] [ ,...n ] )
[ INCLUDE ( column_name [ ,...n ] ) ]
[ WHERE ]
[ WITH ( [ ,...n ] ) ]
[ ON { partition_scheme_name ( column_name )
| filegroup_name
| default
}
]
[ FILESTREAM_ON { filestream_filegroup_name | partition_scheme_name | "NULL" } ][ ; ]



You may recall the table creation scripts that we used in Article, “Designing Tables,” had a keyword of “clustered” in the specification of a primary key. Although a primary key is a constraint, SQL Server physically implements a primary key as an index. Because the default option for a primary key is clustered, SQL Server creates a clustered index for a primary key. Likewise, a unique constraint is physically implemented as a unique index. Since a primary key is also unique, by default, SQL Server physically implements each primary key as a unique, clustered index.

Note

Partition schemes will be discussed in article about Partitioning.


In the practice that follows, you will create clustered indexes on the OrderHeader and OrderDetail tables previously created in article "Designing Tables". Since we have already created primary keys on the OrderHeader and OrderDetail tables using the default options, you will first drop and re-create the primary keys as nonclustered in order to create the single, allowed clustered index on the table.

Create a Clustered Index

1.
Execute the following code against the SQL2008SBS database:

-Must drop foreign key first, because it depends on the primary keyALTER TABLE Orders.
OrderDetail
DROP CONSTRAINT fk_orderheadertoorderdeatils
GO

-Change the existing primary keys to nonclustered
ALTER TABLE Orders.OrderHeader
DROP CONSTRAINT pk_orderheader
GO
ALTER TABLE Orders.OrderHeader
ADD CONSTRAINT pk_orderheader PRIMARY KEY NONCLUSTERED (OrderID)
GO
ALTER TABLE Orders.OrderDetail
DROP CONSTRAINT pk_orderdetail
GO
ALTER TABLE Orders.OrderDetail
ADD CONSTRAINT pk_orderdetail PRIMARY KEY NONCLUSTERED (OrderDetailID)
GO

-Recreate the foreign key (fix the misspelling from article "Designing Tables")
ALTER TABLE Orders.OrderDetail
ADD CONSTRAINT fk_orderheadertoorderdetails FOREIGN KEY (OrderID)
REFERENCES Orders.OrderHeader (OrderID)
GO
-Create new clustered indexes on the ShipDate/FinalShipDate columns
CREATE CLUSTERED INDEX icx_finalshipdate ON Orders.OrderHeader (FinalShipDate)
GO
CREATE CLUSTERED INDEX icx_shipdate ON Orders.OrderDetail (ShipDate)
GO



2.
In the Object Explorer, expand the Indexes node for the OrderHeader and OrderDetail tables. Observe that you now have a clustered index along with a unique, nonclustered index created against the table.
Other  
 
Video
Top 10
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
The latest Audi TT : New angles for TT
Era of million-dollar luxury cars
Game Review : Hearthstone - Blackrock Mountain
Game Review : Battlefield Hardline
Google Chromecast
Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
Keyboards for Apple iPad Air 2 (part 2) - Zagg Slim Book for iPad Air 2
Keyboards for Apple iPad Air 2 (part 1) - Belkin Qode Ultimate Pro Keyboard Case for iPad Air 2
Michael Kors Designs Stylish Tech Products for Women
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
Popular Tags
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone