We might have heard that always we need to create our CLUSTERED index first then NONCLUSTERED indexes.
Why is that? What would happen if NONCLUSTERED indexes are created first and then we create the CLUSTERED index?
If you create NONCLUSTERED indexes first and then CLUSTERED index internally ALL NONCLUSTERED indexes on that table would get recreated. On a big table this might take for ever to create the CLUSTERED Index itself.
Example:
In the sample shown in blog post titled "Query tuning using SET STATISTICS IO and SET STATISTICS TIME" we had created couple of NONCLUSTERED indexes alone. Now, let us assume we need to create a CLUSTERED index for that table on ProductID column.
First enable SET STATISTICS PROFILE ON so that we can see the profile information of the scripts we are going to execute. Then execute the below script:
--Script to create CLUSTERED index on ProductID column
CREATE CLUSTERED INDEX [ix_productId] ON [dbo].[tblTest]
(
[ProductID] ASC
)
GO
During this process it would have recreated both of these NONCLUSTERED indexes
Why is that? What would happen if NONCLUSTERED indexes are created first and then we create the CLUSTERED index?
If you create NONCLUSTERED indexes first and then CLUSTERED index internally ALL NONCLUSTERED indexes on that table would get recreated. On a big table this might take for ever to create the CLUSTERED Index itself.
Example:
In the sample shown in blog post titled "Query tuning using SET STATISTICS IO and SET STATISTICS TIME" we had created couple of NONCLUSTERED indexes alone. Now, let us assume we need to create a CLUSTERED index for that table on ProductID column.
First enable SET STATISTICS PROFILE ON so that we can see the profile information of the scripts we are going to execute. Then execute the below script:
--Script to create CLUSTERED index on ProductID column
CREATE CLUSTERED INDEX [ix_productId] ON [dbo].[tblTest]
(
[ProductID] ASC
)
GO
During this process it would have recreated both of these NONCLUSTERED indexes
- [nc_ix_manufacturing_dt] &
- [nc_ix_productName]
Comments