主键是否自动成为索引?

如果我运行Profiler,则会建议许多类似于此的索引

CREATE CLUSTERED INDEX [_dta_index_Users_c_9_292912115__K1] ON [dbo].[Users] 
(
 [UserId] ASC
)WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, 
ONLINE = OFF) ON [PRIMARY]

userid是表USERS的主键。此索引是否比表中已有的索引更好:

ALTER TABLE [dbo].[Users] ADD  CONSTRAINT [PK_Users] PRIMARY KEY NONCLUSTERED 
(
 [UserId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, 
IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, 
ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

解决方案

每个表都需要一个聚集索引和一个主键。默认情况下,主键是集群化的,如果您愿意,也可以不集群化。

您指定了非聚集主键,因此探查器建议使用聚集索引.

注意:没有聚集索引的表称为"堆",因为它是一堆非结构化数据.

相关文章