Create Index Syntax
-
Posted on July 25, 2009 by Derek Dieter
-
2
To add an index in SQL Server use the CREATE INDEX statements. When adding indexes remember there can only be one clustered index per table. The main options when creating an index are clutered or nonclustered or unique vs non unique. Using SQL 2005+ you can also specify columns to include at the leaf level of the index.
Create a single nonclustered index
CREATE UNIQUE NONCLUSTERED INDEX IX_NC_PresidentNumber -- specify index name
ON dbo.Presidents (PresidentNumber) -- specify table and column name
ON dbo.Presidents (PresidentNumber) -- specify table and column name
Create a multi-column (composite) nonclustered index
CREATE UNIQUE NONCLUSTERED INDEX IX_NC_PresidentNumber_PresidentName -- specify index name
ON dbo.Presidents (PresidentNumber,PresidentName) -- specify table and column names
ON dbo.Presidents (PresidentNumber,PresidentName) -- specify table and column names
Create a multi-column (composite) clustered index
CREATE UNIQUE CLUSTERED INDEX IX_C_PresidentNumber -- specify index name
ON dbo.Presidents (PresidentNumber,PresidentName) -- specify table and column names
ON dbo.Presidents (PresidentNumber,PresidentName) -- specify table and column names
Create a non clustered index with included columns
CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber -- specify index name
ON dbo.Presidents (PresidentNumber) -- specify table and column name
INCLUDE (President,YearsInOffice,RatingPoints) -- specify included columns
ON dbo.Presidents (PresidentNumber) -- specify table and column name
INCLUDE (President,YearsInOffice,RatingPoints) -- specify included columns
Create index with fill factor
CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber -- specify index name
ON dbo.Presidents (PresidentNumber) -- specify table and column name
WITH (FILLFACTOR = 80) -- specify the fill factor
ON dbo.Presidents (PresidentNumber) -- specify table and column name
WITH (FILLFACTOR = 80) -- specify the fill factor
SQL Server 2008 options
SQL Server 2008 provides additional index options. Most notably it provides the ability to filter an index. This can help decrease the size of the index for very large or partitioned table to only include the records that are normally queried.
CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber
ON dbo.Presidents (PresidentNumber)
INCLUDE (President,YearsInOffice,RatingPoints)
WHERE ElectoralVotes IS NOT NULL
ON dbo.Presidents (PresidentNumber)
INCLUDE (President,YearsInOffice,RatingPoints)
WHERE ElectoralVotes IS NOT NULL
Another 2008 option is the ability to compress the contents of the index based on the row or the page.
CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber
ON dbo.Presidents (PresidentNumber)
WITH ( DATA_COMPRESSION = ROW ) ;
ON dbo.Presidents (PresidentNumber)
WITH ( DATA_COMPRESSION = ROW ) ;
Compress based on page:
CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber
ON dbo.Presidents (PresidentNumber)
WITH ( DATA_COMPRESSION = PAGE ) ;
ON dbo.Presidents (PresidentNumber)
WITH ( DATA_COMPRESSION = PAGE ) ;
Post a comment
- Comments (RSS)
- Trackback
- Permalink





