您可以创建带有或不带有 INCLUDE 的索引:如果PrimaryKeyCol 是聚集索引,SQL Server 将忽略它。也就是说,它不会将聚集索引值存储两次
为了完整起见,我可能会在我更改聚集索引的情况下
编辑:
我通过size观察到SQL Server 可以智能地处理这个问题
这不像Kalen Delaney 的 More About Nonclustered Index Keys那样科学
DROP TABLE IncludeTest;
GO
CREATE TABLE IncludeTest (
BadClusteredKey uniqueidentifier DEFAULT NEWID() PRIMARY KEY,
OtherCol AS CHECKSUM(BadClusteredKey) % 10000,
Filler char(200) NOT NULL DEFAULT 'a and lots of spaces'
);
GO
INSERT IncludeTest (Filler) VALUES (DEFAULT);
GO
INSERT IncludeTest (Filler) SELECT Filler FROM IncludeTest
GO 20
SELECT COUNT(*) FROM IncludeTest;
EXEC sp_spaceused 'IncludeTest', 'true'
GO -- 400680 KB, 1920 KB
CREATE INDEX IX_OtherCol1 ON IncludeTest (OtherCol);
GO
EXEC sp_spaceused 'IncludeTest', 'true'
GO -- 400680 KB, 29024 KB KB
DROP INDEX IncludeTest.IX_OtherCol1
GO
EXEC sp_spaceused 'IncludeTest', 'true'
GO -- 400680 KB, 1920 KB
CREATE INDEX IX_OtherCol2 ON IncludeTest (OtherCol) INCLUDE (BadClusteredKey);
EXEC sp_spaceused 'IncludeTest', 'true'
GO -- 400680 KB, 29024 KB
DROP INDEX IncludeTest.IX_OtherCol2
GO
EXEC sp_spaceused 'IncludeTest', 'true'
GO -- 400680 KB, 1920 KB