HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Nonclustered index is faster than clustered index?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
clusteredthannonclusteredfasterindex

Problem

Both tables have same structure and 19972 rows in each table.
for practicing indexing, i created both tables having same structure and created

clustered index on persontb(BusinessEntityID)


and

nonclustered index on Persontb_NC(BusinessEntityId)


and table structure

BusinessEntityID int
FirstName varchar(100)
LastName  varchar(100)                                                                                                                       

 -- Nonclusted key on businessentityid takes 38%
SELECT  BusinessEntityId from Persontb_NC
WHERE businessentityid BETWEEN 400 AND 4000

-- CLustered key businessentityid takes 62%
SELECT BusinessEntityId  from persontb 
WHERE businessentityid BETWEEN 400 AND 4000


Why clustered index takes 62% and non clustered 38%?

Solution

Yes the clustered index has fewer rows per page than the non clustered index as the leaf pages of the clustered index must store the values for the other two columns (FirstName and LastName).

The leaf pages of the NCI store only the BusinessEntityId values and a row locator (RID if the table is a heap or the CI key otherwise).

So the estimated costs reflect the greater number of reads and IO requirement.

If you were to declare the NCI as

nonclustered index on Persontb_NC(BusinessEntityId) INCLUDE (FirstName, LastName)


then it would be similar to the clustered index.

Code Snippets

nonclustered index on Persontb_NC(BusinessEntityId) INCLUDE (FirstName, LastName)

Context

StackExchange Database Administrators Q#44808, answer score: 10

Revisions (0)

No revisions yet.