SQL server 将根据成本评估是否可以使用索引,并根据您的价值的选择性来估算成本。如果您过滤的列不是和索引的第一列,则可以进行索引扫描,但不能进行索引查找。但是当您使用 SELECT * 时,您可能最终会进行聚集索引扫描。
CREATE TABLE dbo.x
(
a int,
b int,
c int,
d int
)
CREATE CLUSTERED INDEX ix1 ON dbo.X (a);
CREATE NONCLUSTERED INDEX ix2 ON dbo.X (b,c);
INSERT INTO dbo.X (a,b,c) VALUES(1,1,1);
SELECT * FROM dbo.X WHERE b=1 --will probably use a clustered index scan
SELECT a FROM dbo.X WHERE b=1 --can use a non-clustered index seek (a is in the index as it's part of the clustered index key
SELECT a FROM dbo.X WHERE c=1 --will probably use a nonclustered index scan as c is not the first column
SELECT a,d FROM dbo.X WHERE b=1 --will probably use a clustered index scan as d is not part of the index