So I have this structure on a user table:
But if I run the following code:
select count(*)
from WH.dbo.tb_DimUserAccount
It seems to go for the Non-Unique Non-Clustered index ix_DimUserAccount_UserType
:
This is the Index Scan:
Why doesn't it go for a scan of ix_DimUserAccount_Unique
? Should I change my code to somehow use a different index?
Because the non clustered index is probably narrower than the clustered index and it is cheaper to scan than the clustered index (fewer pages to read).
The NCI leaf pages just contain values for the index keys and any included columns. The clustered index leaf pages need to contain values (or pointers to the values) for all columns in the table.
Thus the clustered index will (assuming equal fill factors) generally fit fewer rows per page than an NCI (except for the case where an NCI includes all columns in the table)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.